gpt4 book ai didi

javascript - 调整浏览器大小时尝试关闭 requestAnimation 功能

转载 作者:太空宇宙 更新时间:2023-11-04 12:39:08 28 4
gpt4 key购买 nike

我有以下代码,我试图在运行窗口调整大小时关闭该功能,目前它只是继续在 window.resize 上运行。

function headerParallax(x) {
if (x ==="true"){
$(window).scroll(function() {
// Store scrollTop in variable
var scrollPos = $(window).scrollTop();
// var viewportHeight = $(window).height();
console.log(scrollPos + 'bgbottle1');

var bouncePos = ((-1 * (scrollPos - 75) * 1.5) + scrollPos).toFixed(2);
var bouncePos1 = ((-1 * (scrollPos - 150) * 1.25) + scrollPos).toFixed(2);

$(".bottle1").css({ 'background-position': "right " + bouncePos + 'px'});

if (scrollPos > 150){
$(".bottle2").css({ 'background-position': "left " + bouncePos1 + 'px'});
}
});
}else if(x === "false"){
alert("no");
}
}

$(window).resize(function(){
if ($(window).width() < 1200){
window.requestAnimationFrame(headerParallax("false"));
}
});

if ($(window).width() > 1200){
window.requestAnimationFrame(headerParallax("true"));
}

最佳答案

你可以尝试这样的事情:

var _preflag = -1;
var _unbindScroll = function(){};

// it will be fired only once (when flag is changed)
function headerParallax(flag){
if (flag === _preflag){
return;
}

_preflag = flag;

if (flag){
// TODO adjust the UI for true
window.requestAnimationFrame(theCalculatedValue);

_unbindScroll(); // It's duplicate work to unbind scroll here, but there's no harm calling it :)
$(window).on('scroll', _onscroll);

// update the ubind scroll so that anyone can ubind it safely
_unbindScroll = function(){
$(window).off('scroll', _onscroll);
_unbindScroll = function(){};
};

} else {
// TODO adjust the UI for false
window.requestAnimationFrame(theCalculatedValue);
_unbindScroll(); // unbind scrolling, this is what you want, right?
}

function _onscroll(){
// TODO
}
}

function resize(){
// this will be fired multipe times, need to check it in sub functions to take it once
headerParallax($(window).width() < 1200);
}

$(window).resize(resize);
resize();

关于javascript - 调整浏览器大小时尝试关闭 requestAnimation 功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27029736/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com