javascript - jQuery play next video on scroll -


im building page has dynamically loaded sections stacked upon 1 another, each section can contain either image or video. want build functionality when user scrolls past video browser play 1 in view.

a page may like:

----------------- [     header    ] ----------------- [ image section ]   | [ image section ]   | [ image section ]   | [ video section ]   <---- scroll height (play first video not second) [ image section ]   | [ image section ]   | [ video section ]   <---- scroll height b (play second video not first) [ image section ]   |  ----------------- [     footer    ] ----------------- 

my problem is, each video section identified same class '.section_video', i'm aware attribute unique identifiers each section, if there way handle without doing great.

currently, detect whether in view use code:

 // checks whether our top offset finds video container  function isscrolledintoview(elem){      var docviewtop = $(window).scrolltop();      var docviewbottom = docviewtop + $(window).height();       var elemtop = $(elem).offset().top;      var elembottom = elemtop + $(elem).height();       return ((elembottom >= docviewtop) && (elemtop <= docviewbottom)          && (elembottom <= docviewbottom) &&  (elemtop >= docviewtop) );  }   // check whether player div has been scrolled view  function checkifvideoinview(){      var player = $('.section_video .video_column video');       // if true returned, play video      if ( isscrolledintoview(player) ) {          player.get(0).play();      }  }   window.onscroll = checkifvideoinview; 

i have tried using $(this).(elem) selector thought grab current element in view, console prompted element undefined.

could point me in right direction, or should bind unique identifiers each video on page load?

i think should use $(elem).each() , check out video elements. that:

$(window).scroll(function() {     $('.myvid').each(function() {         if(isinview(this)) {             this.play();         } else {             this.pause();         }     }); }); 

and function:

function isinview(el) {     windowtop = $(window).scrolltop();     windowbuttom = windowtop + $(window).height();     eltop = $(el).offset().top;     elbuttom = eltop + $(el).height();      return (eltop >= windowtop && elbuttom <= windowbuttom); } 

demo http://jsfiddle.net/t52sz0rt/1/


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -