我想将var num更改为百分比而不是像素数。 我怎样才能做到这一点?
我的网站需要这个: http://www.sutanaryan.com/Tutorials/fixed-menu-when-scrolling-page-with-CSS-and-jQuery/ 但它们使用像素,我的网站使用%(因为例如在高清屏幕或全高清屏幕上进行自动缩放)
/ *动态顶级菜单定位 * * /
var num = 150 ; //number of pixels before modifying styles 150
$(window).bind('scroll', function () {
if ($(window).scrollTop() > num) {
$('.menu').addClass('fixed');
} else {
$('.menu').removeClass('fixed');
}
});
//USE SCROLL WHEEL FOR THIS FIDDLE DEMO
答案 0 :(得分:1)
首先,让我告诉你这是一个可怕的解决方案。每次听每个滚动事件并调用addClass()或removeClass()都很昂贵。 //结束讲道
无论如何,这里是你问题的答案:
var baseHeight = $(window).height(); // for body, use $("body").height();
var num = .25; // this the percentage of vertical scroll
$(window).bind('scroll', function () {
if ($(window).scrollTop() / baseHeight > num) {
$('.menu').addClass('fixed');
} else {
$('.menu').removeClass('fixed');
}
});