Solved

jQuery plugin isInViewport

Posted on 2016-09-30
7
90 Views
Last Modified: 2016-09-30
Hello Experts,

I am suing the jQuery plugin isInViewport  from isInViewport
While in view it is playing the videos on the page.

My question:
I am trying to find the way to to play the video while inView when scrolling down the page and not when again when scrolling up. So on up just do nothing or pause the video.

$(window).bind('scroll',function() {

var $video = $("video");

$video.each(function() {  
   if ( $video.is( ':in-viewport')) {
      $(this)[0].play();
   } else {
      $(this)[0].pause();
   }   
});

Open in new window

0
Comment
Question by:Refael
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 57

Expert Comment

by:Julian Hansen
ID: 41823611
$video.each(function() {  
   if ( $video.is( ':in-viewport')) {
      $(this)[0].play();
   } else {
      $(this)[0].pause();
   }   
});

Open in new window

You don't need the index when inside .each - this refers to the current element in the array. Play and Pause are methods on the control - so you don't need to wrap this in a jQuery object to access them.
Try this.
$video.each(function() {  
   if ( $video.is( ':in-viewport')) {
      this.play();
   } else {
      this.pause();
   }   
});

Open in new window

0
 
LVL 57

Expert Comment

by:Julian Hansen
ID: 41823616
Correction
$video.each(function() {  
   if ( $(this).is( ':in-viewport')) {
      this.play();
   } else {
      this.pause();
   }   
});

Open in new window

0
 

Author Comment

by:Refael
ID: 41823685
Hello Julian
Thank you for seeing that!
Can you help me resolve this question?
How to execute this only when scrolling down and not when scrolling up?
When the videos already got played (after scrolling down) don;t play it again when scrolling up.
0
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
LVL 57

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 41823720
$video.each(function() {  
   if ( $(this).is( ':in-viewport')) {
      // only play if this has not already been played
      if (!this.ended) {
         this.play();
     }
   } else {
      this.pauseVideo();
   }   
});

Open in new window

0
 

Author Comment

by:Refael
ID: 41823752
Julian you are a magic :-) Thank you so much!

P.S. "pauseVideo" does not work only "pause" works.
0
 

Author Closing Comment

by:Refael
ID: 41823753
Julian you are a magic :-) Thank you so much!
0
 
LVL 57

Expert Comment

by:Julian Hansen
ID: 41823806
oops - editing mistake - should be pause() - I was heading down a different path and did not go back and undo.

Glad it is working for you.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question