Solved

jQuery plugin isInViewport

Posted on 2016-09-30
7
72 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 56

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 56

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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 56

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 56

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

Industry Leaders: 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

Suggested Solutions

Title # Comments Views Activity
Date on a table 16 37
Prevent / reduce email harvesting using an image – javascript approach ? 14 74
Html date format 9 20
How do I post more than 1 item to php backend 24 32
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…
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
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…

730 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