Javascript 防止每个卷轴上的卷轴着火
我试图在每次触发滚动时使用一个标志来防止滚动到启动功能<代码>$(窗口)。解除绑定(“滚动”)代码>因为我将Javascript 防止每个卷轴上的卷轴着火,javascript,jquery,if-statement,scroll,Javascript,Jquery,If Statement,Scroll,我试图在每次触发滚动时使用一个标志来防止滚动到启动功能$(窗口)。解除绑定(“滚动”)因为我将.scroll()与其他函数一起使用。那么为什么它不阻止每次启动该函数呢 JS: $(窗口)。滚动(函数(){ $(“iframe”)。每个(函数(){ $this=$(this); _src=$this.attr('src'); _ID=$this.attr('ID'); _yPos=$(window.scrollTop(); _thisHT=$this.height(); _thisTop=$thi
.scroll()
与其他函数一起使用。那么为什么它不阻止每次启动该函数呢
JS:
$(窗口)。滚动(函数(){
$(“iframe”)。每个(函数(){
$this=$(this);
_src=$this.attr('src');
_ID=$this.attr('ID');
_yPos=$(window.scrollTop();
_thisHT=$this.height();
_thisTop=$this.offset().top;
_thisBottom=\u thisTop+\u thisHT;
_玩=真;
如果(_Play){
如果(\u yPos>\u thisTop*0.5&&u yPos<\u thistomp){
$this.attr('src','u src+'&autoplay=1');
_玩=假;
//$(窗口)。解除绑定(“滚动”);
}否则{
$this.attr('src',_src);
}
}
});
});
所以我发现了一个很明显的问题。我必须移动\u在$(窗口)外播放代码>然后我使用递增而不是真/假(尽管这可能适用于)
\u播放='0';
$(窗口)。滚动(函数(){
$(“iframe”)。每个(函数(){
$this=$(this);
_src=$this.attr('src');
_ID=$this.attr('ID');
_yPos=$(window.scrollTop();
_thisHT=$this.height();
_thisTop=$this.offset().top;
_thisBottom=\u thisTop+\u thisHT;
如果(_play==“0”){
如果(\u yPos>\u thisTop*0.5&&u yPos<\u thistomp){
_play='1';
$this.attr('src','u src+'&autoplay=1');
//$(窗口)。解除绑定(“滚动”);
}否则{
$this.attr('src',_src);
}
}
});
});
\u Play
总是正确的。
$(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);
}
}
});
});
_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);
}
}
});
});