滚动事件运行缓慢-有没有更轻松的方法
scroll event running slow - is there a lighter way?
我有以下滚动事件,该事件在滚动时测量用户在页面中的位置,并根据用户所在的部分更新导航样式。问题是我在滚动时执行的计算非常粗,滚动时会稍微减慢页面速度。这是我的代码:
screenHeight = $(window).height();
screenHeightRatio = screenHeight*0.3;
//here I calculate screen height plus the ratio of the screen height I would like for the menu elements to change
aboutOffset = $(".aboutcontainer").offset().top - screenHeightRatio;
portfolioOffset = $(".portfoliocontainer").offset().top - screenHeightRatio;
musicOffset = $(".musiccontainer").offset().top - screenHeightRatio;
contactOffset = $(".contactcontainer").offset().top - screenHeightRatio;
// here I calculate the offset for each section in the screen
$(window).scroll(function(){
var amountScrolled = $(document).scrollTop();
//here I see how far down the page the person has scrolled
if($(".header-options").hasClass("portfolio-inner-active")) {
return;
// here I cancel the scroll event if they are in a certain section
} else {
if(contactOffset <= amountScrolled) {
// each of the following if statements will calculate if the amount scrolled surpasses the various section offsets I defined outside of the scroll function
$(".header-options li").removeClass("active");
$(".contactbutton").addClass("active");
history.pushState('page2', 'Title', '/contact');
return;
} else {
if(musicOffset <= amountScrolled) {
$(".header-options li").removeClass("active");
$(".musicbutton").addClass("active");
history.pushState('page2', 'Title', '/music');
return;
} else {
if(portfolioOffset <= amountScrolled) {
$(".header-options li").removeClass("active");
$(".portfoliobutton").addClass("active");
history.pushState('page2', 'Title', '/portfolio');
return;
} else {
if(aboutOffset <= amountScrolled) {
$(".header-options li").removeClass("active");
$(".aboutbutton").addClass("active");
history.pushState('page2', 'Title', '/about');
}
}
}
}
}
});
我很想知道是否有一种不那么耗电的方式来做这件事,因为我真的很想在网站上看到这种效果。
干杯
使用本·阿尔曼的jQuery节流阀/防反弹
$(window).scroll( $.throttle( 250, function(){...} ) );
http://benalman.com/code/projects/jquery-throttle-debounce/jquery.ba-throttle-debounce.js
如果您只想延迟呼叫量,那么这可以在中工作
var waitForFinalEvent = (function () {
var timers = {};
return function (callback, ms, uniqueId) {
if (!uniqueId) {
uniqueId = "Don't call this twice without a uniqueId";
}
if (timers[uniqueId]) {
clearTimeout (timers[uniqueId]);
}
timers[uniqueId] = setTimeout(callback, ms);
};
})();
事实上,这是我在这里找到的一些代码,请原谅,我没有链接回原始源的url。这将使滚动后的通话延迟X秒。这意味着,它不会在这段时间内打5000万个电话,而是会打一个,这只会有所帮助。
你这样称呼它:
waitForFinalEvent(function() {
//stuff to do
}, 500, "randomString");
希望这能有所帮助!!将500调整为要延迟的时间。
原始帖子:JavaScript/JQuery:$(窗口).调整大小如何在调整大小完成后激发?
相关文章:
- 有没有任何方法可以将控制器从文件加载到ui路由器$stateProvider中
- 有没有一种方法可以添加相同的项目val=“0”;4〃;到JavaScript中数组的每个对象
- 有没有一种方法可以防止img get请求使用css或js发生
- Javascript,有没有一种方法可以将数组写成没有逗号或空格的单个文本字符串
- 有没有更好的动手、具体的方法来学习Javascript
- 有没有一种方法可以在控制台关闭的情况下让console.log()在IE中记录消息
- 在D3.js中,有没有任何方法可以将x和y方向上的滚动事件绑定到平移svg
- 有没有任何方法可以使用node-js从不同的机器打开浏览器
- 有没有一种方法可以仅使用HTML/CSS来隐藏基于特定值的数据单元格
- 有没有一种方法可以从URL跟踪请求的域
- 有没有比在app.js上绑定模块名称更好的方法来动态加载视图模型和视图以显示模态
- 有没有一种方法可以列出Ember.Object的所有绑定
- 有没有一种方法可以获得three.js的最小/lite版本
- 有没有一种方法可以在没有文档或jQuery的情况下使用javascript解码html实体
- 有没有一种方法可以从Javascript检测特定的应用程序是否安装在(AndroidiOS)设备上
- 在HTML/JavaScript中,有没有一种方法可以在图像开始加载时知道图像的最终布局尺寸
- 有没有一种方法可以在Javascript中进行可变递归currying
- 有没有一种方法可以检测ios<>使用jquery和触发器操作形成导航按钮
- 有没有一种方法可以让内联事件处理程序在元素创建后立即执行
- 有没有一种方法可以直接从cordova获得滚动位置