2017-05-03 72 views
0

我試圖使用一個標誌來防止滾動觸發每次滾動觸發功能。 $(window).unbind("scroll");因爲我使用.scroll()與其他功能。那麼爲什麼它不能防止每次都觸發該功能呢?防止滾動在每個滾動觸發

JS:

$(window).scroll(function() { 
     $("iframe").each(function() { 
      $this = $(this); 
      _src = $this.attr('src'); 
      _ID = $this.attr('id'); 
      _yPos = $(window).scrollTop(); 
      _thisHT = $this.height(); 
      _thisTop = $this.offset().top; 
      _thisBottom = _thisTop + _thisHT; 
      _Play = true; 

      if (_Play) { 
       if(_yPos > _thisTop*0.5 && _yPos < _thisBottom) {  
        $this.attr('src', _src + '&autoplay=1'); 
        _Play=false; 

       //$(window).unbind("scroll"); 
       } else { 
        $this.attr('src', _src); 

       } 
      } 
     }); 
    }); 
+3

'_Play'總是正確的。 – Huelfe

回答

0

所以,我發現了一個sullotion,這是很明顯的。我不得不搬到外面_play$(window).scroll;然後我用increaments,而不是真/假(雖然這可能工程)

_play = '0'; 

$(window).scroll(function() { 


    $("iframe").each(function() { 
     $this = $(this); 
     _src = $this.attr('src'); 
     _ID = $this.attr('id'); 
     _yPos = $(window).scrollTop(); 
     _thisHT = $this.height(); 
     _thisTop = $this.offset().top; 
     _thisBottom = _thisTop + _thisHT; 
     if (_play === '0') { 
      if(_yPos > _thisTop*0.5 && _yPos < _thisBottom) {  
       _play = '1'; 
       $this.attr('src', _src + '&autoplay=1'); 





      //$(window).unbind("scroll"); 
      } else { 
       $this.attr('src', _src); 

      } 
     } 
    }); 
});