Advertisement
Guest User

Untitled

a guest
Nov 25th, 2015
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.         var dragtime = function(){
  3.             this.video =
  4.                     document.querySelectorAll('.js-dt-video');
  5.  
  6.             this.label =
  7.                     document.querySelectorAll('.js-dt-label');
  8.  
  9.             this.tabs =
  10.                     document.querySelectorAll('.switch');
  11.  
  12.             this.init();
  13.         };
  14.  
  15.         dragtime.prototype.init = function() {
  16.             this.attachEvent();
  17.         };
  18.  
  19.         dragtime.prototype.attachEvent = function() {
  20.             var t = this;
  21.  
  22.             for (var i = 0; i < this.tabs.length; i++) {
  23.                 this.tabs[i].onclick = function() {
  24.                     setTimeout(t.scrollHandler.bind(t), 1000);
  25.                 };
  26.             }
  27.  
  28.             window.addEventListener('scroll', function(){
  29.                 window.requestAnimationFrame(t.scrollHandler.bind(t));
  30.             });
  31.         };
  32.  
  33.         dragtime.prototype.scrollHandler = function(){
  34.             for(var i = 0; i < this.video.length; i++){
  35.                 if(this.video[i].offsetParent !== null) {
  36.                     this.checkPosition(this.video[i], i);
  37.                 }
  38.             }
  39.         };
  40.  
  41.         dragtime.prototype.checkPosition = function(video_item, i){
  42.             var t = this;
  43.             var rectBody = document.body.getBoundingClientRect();
  44.             var rect = video_item.getBoundingClientRect();
  45.             var offsetTop = rect.top - rectBody.top;
  46.             var topHeight = offsetTop + video_item.clientHeight;
  47.  
  48.             if (topHeight >= window.scrollY &&
  49.                     offsetTop <= window.scrollY + window.innerHeight - video_item.clientHeight/1.5) {
  50.  
  51.                 if(video_item.currentTime === 0)
  52.                     video_item.play();
  53.  
  54.                 video_item.onended = function(){
  55.                     t.label[i].style.opacity = 1;
  56.                 }
  57.  
  58.             }
  59.         };
  60.  
  61.         new dragtime();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement