?
Solved

jQuery plugin isInViewport

Posted on 2016-09-30
7
Medium Priority
?
110 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 58

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 58

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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 58

Accepted Solution

by:
Julian Hansen earned 2000 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 58

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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)
Suggested Courses

762 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