Home > Net >  How to get onclick event functionality without javascript?
How to get onclick event functionality without javascript?

Time:05-29

I want that when I click this [watch trailer] button, I will see the trailer coming on the screen but without using on click (without javascript) using only CSS.

Also, When the video is seen there is a cross button. It should close the video on clicking but with CSS only. I tried to do this using the checkbox CSS hack but it doesn't work.

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 ><img src="Images/play.png">Watch Trailer</a>
<div >
  <video src="Images/Mulan-Official-Trailer.mp4" controls="true" autoplay="true"></video>
  <img src="Images/close.png" >
</div>

CodePudding user response:

Here is the onclick event functionality without javascript by using the checkbox hack, Look at the code:

#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 ><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>

CodePudding user response:

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 >
  <input id="trailer"  type="checkbox" data-title="Watch Trailer!" />
  <div >
    <div >
      <video src="./trailer.mp4" poster="https://picsum.photos/350/200?random=1" controls="true"></video>
      <label for="trailer" ></label>
    </div>
  </div>
</div>
<div >
  <input id="trailer2"  type="checkbox" data-title="Watch Trailer!" />
  <div >
    <div >
      <video src="./trailer.mp4" poster="https://picsum.photos/350/200?random=2" controls="true"></video>
      <label for="trailer2" ></label>
    </div>
  </div>
</div>
<div >
  <input id="trailer3"  type="checkbox" data-title="Watch Trailer!" />
  <div >
    <div >
      <video src="./trailer.mp4" poster="https://picsum.photos/350/200?random=3" controls="true"></video>
      <label for="trailer3" ></label>
    </div>
  </div>
</div>

  • Related