Solved

jQuery plugin isInViewport

Posted on 2016-09-30
7
31 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 51

Expert Comment

by:Julian Hansen
Comment Utility
$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 51

Expert Comment

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

Open in new window

0
 

Author Comment

by:Refael
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 51

Accepted Solution

by:
Julian Hansen earned 500 total points
Comment Utility
$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
Comment Utility
Julian you are a magic :-) Thank you so much!

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

Author Closing Comment

by:Refael
Comment Utility
Julian you are a magic :-) Thank you so much!
0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
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)

772 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

11 Experts available now in Live!

Get 1:1 Help Now