如何在没有 javascript 的情况下获得 onclick 事件功能?
How to get onclick event functionality without javascript?
我希望当我点击这个 [watch trailer] 按钮时,我会看到预告片出现在屏幕上但不使用点击(没有 javascript)仅使用 CSS.
此外,当看到视频时有一个十字按钮。它应该在点击时关闭视频,但只有 CSS。我尝试使用复选框 CSS hack 来做到这一点,但它不起作用。
body {
background: #000;
}
.play {
position: absolute;
bottom: 50px;
left: 100px;
display: inline-flex;
justify-content: flex-start;
align-items: center;
color: #fff;
text-transform: uppercase;
font-weight: 500;
text-decoration: none;
letter-spacing: 1px;
font-size: 1.2em;
cursor: pointer;
}
.play img {
margin-right: 10px;
max-width: 50px;
}
.trailer {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
z-index: 100000;
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
backdrop-filter: blur(20px);
visibility: hidden;
opacity: 0;
}
.trailer video {
max-width: 900px;
outline: none;
}
.close {
position: absolute;
top: 30px;
right: 30px;
cursor: pointer;
filter: invert(1);
max-width: 32px;
}
<a class="play"><img src="Images/play.png">Watch Trailer</a>
<div class="trailer">
<video src="Images/Mulan-Official-Trailer.mp4" controls="true" autoplay="true"></video>
<img src="Images/close.png" class="close">
</div>
这是 onclick
事件功能,没有 javascript
通过使用复选框 hack,查看代码:
#btn{
display: none;
}
label:hover {
cursor: pointer;
}
label::after {
content: 'Close Trailer';
display: none;
}
label > iframe {
display: none;
}
#btn:checked + label > iframe {
display: block !important;
}
#btn:checked + label > .play {
display: none;
}
#btn:checked + label::after {
margin-top: 5px;
display: block;
}
<input type="checkbox" id="btn">
<label for="btn">
<a class="play"><img src="Images/play.png">Watch Trailer</a>
<iframe width="560" height="315" src="https://www.youtube.com/embed/YQ1vN_91KO0" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</label>
Make sure IDs are unique for each item, otherwise this won't work
body {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
margin: 0;
height: 100vh;
font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", Helvetica, Arial, sans-serif;
}
:root {
--close-size: 12px;
--close-thickness: 2px;
--close-color: #eeeeee;
--close-hoverColor: #00adb5;
--close-background: rgba(0, 0, 0, 0.25);
--close-hoverBackground: rgba(255, 255, 255, 1);
}
@keyframes fadeIn {
from {
opacity: 0;
transform: scale(0);
}
to {
opacity: 1;
transform: scale(1);
}
}
.play {
appearance: none;
outline: none;
user-select: none;
}
.play::after {
display: block;
content: attr(data-title);
padding: 10px;
border-radius: 5px;
border: 1px solid #00adb5;
color: #00adb5;
cursor: pointer;
transition: all 0.5s ease;
}
.trailer {
position: fixed;
display: none;
align-items: center;
justify-content: center;
left: 0;
top: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.5);
backdrop-filter: blur(5px);
overflow: hidden;
transition: all 0.5s ease;
}
.container {
position: relative;
width: 80vw;
border-radius: 5px;
animation: fadeIn 0.3s ease-in-out;
transform-origin: center;
}
.play:checked+.trailer {
display: flex;
}
/* Close Icon */
.close {
position: absolute;
right: 0;
top: 0;
cursor: pointer;
background: var(--close-background);
border-radius: 0 5px;
padding: 15px;
z-index: 10;
transition: all 0.5s ease;
}
.close:hover {
background: var(--close-hoverBackground);
}
.close:before,
.close:after {
position: absolute;
content: "";
height: var(--close-size);
width: var(--close-thickness);
background: var(--close-color);
left: 45%;
top: 30%;
border-radius: 25px;
transition: all 0.5s ease;
}
.close:hover:before,
.close:hover:after {
background: var(--close-hoverColor);
}
.close:before {
transform: rotate(45deg);
}
.close:after {
transform: rotate(-45deg);
}
/* Close Icon */
video {
box-shadow: rgba(0, 0, 0, 0.24) 0px 3px 8px;
width: 100%;
border-radius: 5px;
}
<div class="item">
<input id="trailer" class="play" type="checkbox" data-title="Watch Trailer!" />
<div class="trailer">
<div class="container">
<video src="./trailer.mp4" poster="https://picsum.photos/350/200?random=1" controls="true"></video>
<label for="trailer" class="close"></label>
</div>
</div>
</div>
<div class="item">
<input id="trailer2" class="play" type="checkbox" data-title="Watch Trailer!" />
<div class="trailer">
<div class="container">
<video src="./trailer.mp4" poster="https://picsum.photos/350/200?random=2" controls="true"></video>
<label for="trailer2" class="close"></label>
</div>
</div>
</div>
<div class="item">
<input id="trailer3" class="play" type="checkbox" data-title="Watch Trailer!" />
<div class="trailer">
<div class="container">
<video src="./trailer.mp4" poster="https://picsum.photos/350/200?random=3" controls="true"></video>
<label for="trailer3" class="close"></label>
</div>
</div>
</div>
我希望当我点击这个 [watch trailer] 按钮时,我会看到预告片出现在屏幕上但不使用点击(没有 javascript)仅使用 CSS.
此外,当看到视频时有一个十字按钮。它应该在点击时关闭视频,但只有 CSS。我尝试使用复选框 CSS hack 来做到这一点,但它不起作用。
body {
background: #000;
}
.play {
position: absolute;
bottom: 50px;
left: 100px;
display: inline-flex;
justify-content: flex-start;
align-items: center;
color: #fff;
text-transform: uppercase;
font-weight: 500;
text-decoration: none;
letter-spacing: 1px;
font-size: 1.2em;
cursor: pointer;
}
.play img {
margin-right: 10px;
max-width: 50px;
}
.trailer {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
z-index: 100000;
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
backdrop-filter: blur(20px);
visibility: hidden;
opacity: 0;
}
.trailer video {
max-width: 900px;
outline: none;
}
.close {
position: absolute;
top: 30px;
right: 30px;
cursor: pointer;
filter: invert(1);
max-width: 32px;
}
<a class="play"><img src="Images/play.png">Watch Trailer</a>
<div class="trailer">
<video src="Images/Mulan-Official-Trailer.mp4" controls="true" autoplay="true"></video>
<img src="Images/close.png" class="close">
</div>
这是 onclick
事件功能,没有 javascript
通过使用复选框 hack,查看代码:
#btn{
display: none;
}
label:hover {
cursor: pointer;
}
label::after {
content: 'Close Trailer';
display: none;
}
label > iframe {
display: none;
}
#btn:checked + label > iframe {
display: block !important;
}
#btn:checked + label > .play {
display: none;
}
#btn:checked + label::after {
margin-top: 5px;
display: block;
}
<input type="checkbox" id="btn">
<label for="btn">
<a class="play"><img src="Images/play.png">Watch Trailer</a>
<iframe width="560" height="315" src="https://www.youtube.com/embed/YQ1vN_91KO0" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</label>
Make sure IDs are unique for each item, otherwise this won't work
body {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
margin: 0;
height: 100vh;
font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", Helvetica, Arial, sans-serif;
}
:root {
--close-size: 12px;
--close-thickness: 2px;
--close-color: #eeeeee;
--close-hoverColor: #00adb5;
--close-background: rgba(0, 0, 0, 0.25);
--close-hoverBackground: rgba(255, 255, 255, 1);
}
@keyframes fadeIn {
from {
opacity: 0;
transform: scale(0);
}
to {
opacity: 1;
transform: scale(1);
}
}
.play {
appearance: none;
outline: none;
user-select: none;
}
.play::after {
display: block;
content: attr(data-title);
padding: 10px;
border-radius: 5px;
border: 1px solid #00adb5;
color: #00adb5;
cursor: pointer;
transition: all 0.5s ease;
}
.trailer {
position: fixed;
display: none;
align-items: center;
justify-content: center;
left: 0;
top: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.5);
backdrop-filter: blur(5px);
overflow: hidden;
transition: all 0.5s ease;
}
.container {
position: relative;
width: 80vw;
border-radius: 5px;
animation: fadeIn 0.3s ease-in-out;
transform-origin: center;
}
.play:checked+.trailer {
display: flex;
}
/* Close Icon */
.close {
position: absolute;
right: 0;
top: 0;
cursor: pointer;
background: var(--close-background);
border-radius: 0 5px;
padding: 15px;
z-index: 10;
transition: all 0.5s ease;
}
.close:hover {
background: var(--close-hoverBackground);
}
.close:before,
.close:after {
position: absolute;
content: "";
height: var(--close-size);
width: var(--close-thickness);
background: var(--close-color);
left: 45%;
top: 30%;
border-radius: 25px;
transition: all 0.5s ease;
}
.close:hover:before,
.close:hover:after {
background: var(--close-hoverColor);
}
.close:before {
transform: rotate(45deg);
}
.close:after {
transform: rotate(-45deg);
}
/* Close Icon */
video {
box-shadow: rgba(0, 0, 0, 0.24) 0px 3px 8px;
width: 100%;
border-radius: 5px;
}
<div class="item">
<input id="trailer" class="play" type="checkbox" data-title="Watch Trailer!" />
<div class="trailer">
<div class="container">
<video src="./trailer.mp4" poster="https://picsum.photos/350/200?random=1" controls="true"></video>
<label for="trailer" class="close"></label>
</div>
</div>
</div>
<div class="item">
<input id="trailer2" class="play" type="checkbox" data-title="Watch Trailer!" />
<div class="trailer">
<div class="container">
<video src="./trailer.mp4" poster="https://picsum.photos/350/200?random=2" controls="true"></video>
<label for="trailer2" class="close"></label>
</div>
</div>
</div>
<div class="item">
<input id="trailer3" class="play" type="checkbox" data-title="Watch Trailer!" />
<div class="trailer">
<div class="container">
<video src="./trailer.mp4" poster="https://picsum.photos/350/200?random=3" controls="true"></video>
<label for="trailer3" class="close"></label>
</div>
</div>
</div>