Grant
Grant

Reputation: 217

Play video only when scrolled into view

On this side scrolling page https://happyprime01.worldsecuresystems.com/video-test.html there is a video on the third and fourth page/panel. I want the video in the third panel to only start playing once the panel is in view. I have the following which uses the jquery.appear plugin mentioned here

But I can not get it to work. Below is what I am trying. Can anybody assist with this please?

    <div id="item3" class="item">                   

            <!--video fills page section -->
            <div class="fullscreen-bg">
                <video id="vid-P" muted poster="img/bg-dynamically.jpg" class="fullscreen-bg__video  video-powder">
                    <source src="img/PowderPuff.mp4" type="video/mp4">
                </video>
            </div>                                             

        </div>
    </div>
    <!--end-->

    <script type="text/javascript" src="js/jquery.appear.js"></script>

    <script>

        var myVideo = document.getElementById('vid-P');

    //when div with id item3 is in view play video
   $('#item3').on('appear', function(event, $all_appeared_elements) {


        if (appear) {
              // element is now visible in the viewport
             $("#item3").on('appear', myVideo.play);
            } else {
              // element has gone out of viewport
             $("#item3").on('disappear', myVideo.pause);
            }
  });


    </script>

Upvotes: 1

Views: 3373

Answers (2)

Grant
Grant

Reputation: 217

The approach shown on this site worked for me https://cdnjs.com/libraries/vissense/tutorials/autoplay-video Similar to the above, but this worked straight away for me.

    var myVideo = document.getElementById('myVideo');

    VisSense.VisMon.Builder(VisSense(myVideo, { fullyvisible: 0.75 }))
    .on('fullyvisible', function(monitor) {
      myVideo.play();
    })
    .on('hidden', function(monitor) {
      myVideo.pause();

    })
    .build()
    .start();

Upvotes: 1

Phiter
Phiter

Reputation: 14992

You didn't define appear anywhere in the code, so if (appear) { will fail because your condition appear = undefined.

You can use the appear and disappear events instead:

 $('#item3').on('appear', myVideo.play)
            .on('disappear', myVideo.pause);

Upvotes: 2

Related Questions