Two sets of video controllers - complexity with getElementById / querySelectorAll

I am trying to create two sets of play / pause controls for a video using jquery and css. The first set is located on the first screen of a large size, and the second set is in a fixed place in the viewport when the user scrolls down, which means that the user can listen to audio and control the controls when viewing the website without having to scroll up. When users press the pause button, the video pauses and disappears, the pause button disappears, and the play button becomes fully opaque, and vice versa, occurs when the user presses the play button. This works for me for the first set in jsfiddle and the code below, however, I have been trying for the last few hours to find a way to make these changes happen with both sets of controls,therefore, both sets remain in sync (that is: when one pause button disappears, the second in the second set also) and could not make significant progress.

Here is the jsfiddle (edited, missing some css) of the first working set and jquery:

Object.defineProperty(HTMLMediaElement.prototype, 'playing', {
  get: function() {
    return !!(this.currentTime > 0 && !this.paused && !this.ended && this.readyState > 2);
  }
})

var vid = document.getElementById("myVideo"),
  pauseButton = document.getElementById("vidpause"),
  playButton = document.getElementById("vidplay");
var vid = document.getElementById("myVideo");
function vidFade() {
  vid.classList.add("stopfade");
}


pauseButton.addEventListener("click", function() {
  vid.classList.toggle("stopfade");
  if (vid.paused) {
    vid.play();
    vidpause.classList.add("full-button");
    vidpause.classList.remove("fade-button");
    vidplay.classList.add("fade-button");
    vidplay.classList.remove("full-button");
  } else {
    vid.pause();
    vidpause.classList.add("fade-button");
    vidpause.classList.remove("full-button")
    vidplay.classList.add("full-button");
    vidplay.classList.remove("fade-button")
  }
})

playButton.addEventListener("click", function() {
  vid.classList.toggle("stopfade");
  if (vid.playing) {
    vid.pause();
    vidplay.classList.add("full-button");
    vidplay.classList.remove("fade-button");
    vidpause.classList.add("fade-button");
    vidpause.classList.remove("full-button");
  } else {
    vid.play();
    vidplay.classList.add("fade-button");
    vidplay.classList.remove("full-button")
    vidpause.classList.add("full-button");
    vidpause.classList.remove("fade-button")
  }
})

I know that getElementById will work with only one element, so that the second set works with the first, I tried to use getElementByClassName (not shown) or querySelectorAll (below).

Object.defineProperty(HTMLMediaElement.prototype, 'playing', {
  get: function() {
    return !!(this.currentTime > 0 && !this.paused && !this.ended && this.readyState > 2);
  }
})

 var vid = document.getElementById("myVideo");
              function vidFade() {
                  vid.classList.add("stopfade");
              }

              var pauseButton = document.querySelectorAll(".vidpause");
              var i;
              for (i = 0; i < pauseButton.length; i++) {
              pauseButton[i].addEventListener("click", function() {
                  vid.classList.toggle("stopfade");
                if (vid.paused) {
                    vid.play();
                        vidpause.classList.toggleClass("full-button");
                        vidpause.classList.remove("fade-button");
                        vidplay.classList.add("fade-button");
                        vidplay.classList.remove("full-button");
                                } 
                else {
                      vid.pause();
                          vidpause.classList.add("fade-button");
                          vidpause.classList.remove("full-button")
                          vidplay.classList.add("full-button");
                          vidplay.classList.remove("fade-button")
                        }
                                                                })
                    }


                var playButton = document.querySelectorAll(".vidplay");
                var i;
                for (i = 0; i < pauseButton.length; i++) {
                playButton[i].addEventListener("click", function() {
                    vid.classList.toggle("stopfade");
                    if (vid.playing) {
                        vid.pause();
                            vidplay.classList.add("full-button");
                            vidplay.classList.remove("fade-button");
                            vidpause.classList.add("fade-button");
                            vidpause.classList.remove("full-button");
                    } else {
                        vid.play();
                            vidplay.classList.add("fade-button");
                            vidplay.classList.remove("full-button")
                            vidpause.classList.add("full-button");
                            vidpause.classList.remove("fade-button")
                            }
                                                                })
                }

When I try to use the above and its variants, the chrome console gives me the error "vidpause is undefined". I thought maybe there is a problem with the order in which I call my elements, or try to loop an array (?) Or try to change classes when calling classes, but ... I'm at a dead end.

Any advice on this would be very helpful, I'm pretty new to jquery.

+4
source share
2 answers

querySelectorAll a NodeList, classList, .

. , video-play video-pause

Object.defineProperty(HTMLMediaElement.prototype, 'playing', {
  get: function() {
    return !!(this.currentTime > 0 && !this.paused && !this.ended && this.readyState > 2);
  }
})

var vid = document.getElementById("myVideo");

function vidFade() {
  vid.classList.add("stopfade");
}

function fadeInOut(fadeIn, fadeOut) {
  [].forEach.call(fadeIn, function(el) {
    el.classList.add('full-button');
    el.classList.remove('fade-button');
  });
  [].forEach.call(fadeOut, function(el) {
    el.classList.remove('full-button');
    el.classList.add('fade-button');
  });
};

var pauseButtons = document.querySelectorAll(".video-pause");
var playButtons = document.querySelectorAll(".video-play");
[].forEach.call(document.querySelectorAll(".video-play, .video-pause"), function(pauseButton) {
  pauseButton.addEventListener("click", function(e) {
    e.preventDefault();
    vid.classList.toggle("stopfade");
    if (vid.paused) {
      vid.play();
      fadeInOut(pauseButtons, playButtons);
    } else {
      vid.pause();
      fadeInOut(playButtons, pauseButtons);
    }
  })
});
video#myVideo {
  transition: 1s opacity;
}
.stopfade {
  opacity: .5;
}
.vidplay {
  opacity: 0.50;
}
.vidpause {
  opacity: 1;
}
/* Play/Pause Main */

.icon-play-main {
  position: absolute;
  top: 165px;
  left: 50px;
  width: 0;
  height: 0;
  border-width: 23px 35px;
  border-style: solid;
  border-color: transparent transparent transparent #ff0;
  /* #666 */
}
.icon-pause-main,
.icon-pause-main:after {
  position: absolute;
  width: 14px;
  height: 45px;
  background-color: #ff0;
  /* #666 */
}
.icon-pause-main {
  top: 165px;
  left: 3.5px;
}
.icon-pause-main:after {
  content: "";
  top: 0;
  left: 20px;
}
/* Play/Pause bottom left */

.playpause {
  top: 230px;
  left: 0px;
  position: fixed;
  x-index: 510;
}
.icon-play {
  position: absolute;
  top: 10px;
  left: 30px;
  width: 0;
  height: 0;
  border-width: 10px 15px;
  border-style: solid;
  border-color: transparent transparent transparent #0cf;
  /* #666 */
}
.icon-pause,
.icon-pause:after {
  position: absolute;
  width: 6px;
  height: 20px;
  background-color: #0cf;
  /* #666 */
}
.icon-pause {
  top: 10px;
  left: 6px;
}
.icon-pause:after {
  content: "";
  top: 0;
  left: 10px;
}
.fade-button {
  opacity: 0.50;
}
.full-button {
  opacity: 1;
}
<video autoplay loop muted id="myVideo">
  <source src="http://hushhushandsecret.com/hhs/jquery/fullpagejs/imgs/flowers.mp4" type="video/mp4">Your browser does not support the video element.
</video>

<a class="video-pause vidpause full-button" href="#"><span class="icon-pause-main"></span></a>
<a class="video-play vidplay fade-button" href="#"><span class="icon-play-main"></span></a>

<div class="playpause">
  <a class="full-button" href="#"><span class="video-pause icon-pause"></span></a>
  <a class="fade-button" href="#"><span class="video-play icon-play"></span></a>
</div>
Hide result
+1

jQuery, :

, play pause:

<a id="vidpause" class="full-button" href="#"><span class="icon-pause-main pause"></span></a>
<a id="vidplay" class="fade-button" href="#"><span class="icon-play-main play"></span></a>

<div class="playpause">
  <a class="full-button" href="#"><span class="icon-pause pause"></span></a>
  <a class="fade-button" href="#"><span class="icon-play play"></span></a>
</div>

jQuery $(this), :

// create a variable for the video element
var vid = $('#myVideo');

// Function to re-uyse repeated code
// Takes a bool playing - is the video playing
// takes element clicked - the video control element clicked
function vidControls(playing, clicked){
// if the video is playing pause it
if(!playing){
    vid.get(0).pause();
    clicked.parent('a').removeClass('fade-button').addClass('full-button'); // toggle the classes
    clicked.parent('a').prev().removeClass('full-button').addClass('fade-button'); // assume pause always before play
  }else{ // otherwise play it
  vid.get(0).play();
    clicked.parent('a').removeClass('full-button').addClass('fade-button'); // toggle the classes
    clicked.parent('a').prev().removeClass('fade-button').addClass('full-button'); // assume pause always before play
  }
}

// Play button click event
$('.play').click(function(){
    // fade the video in/out as necessary
    vid.toggleClass('stopfade');
  // Re-use repeating code
  vidControls(vid.get(0).paused, $(this));
});

// Pause button click event
$('.pause').click(function(){
    // fade the video in/out as necessary
    vid.toggleClass('stopfade');
    // Re-use repeating code:
  vidControls(vid.get(0).paused, $(this)); 
});

JSFiddle

0

All Articles