Solved

jQuery plugin isInViewport

Posted on 2016-09-30
7
39 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
  • 4
  • 3
7 Comments
 
LVL 52

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 52

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 52

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 52

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JQuery serialize and unserialize 8 50
JS Plugin Chaining 2 32
jquery easyautocomplete for mutiple text boxes 9 55
Angular - break the dataset in chunks? 7 39
Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

910 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now