[英]jQuery parallax effect and lagging
我有一个带有标题和内容的页面。 向下滚动时,标题菜单会停留在顶部,而内容具有某种“视差”效果(它的移动速度比标题要快,这正是我所需要的)。
我的小型jQuery脚本可很好地实现“视差”效果,但是当向下滚动达到最大值时,内容开始出现停滞/滞后。 该脚本似乎一直在尝试不断地向上移动内容(至少使用Apple Magic Mouse鼠标),这会造成这种不良的副作用。
我该如何预防呢?
PS:为了清楚地显示卡纸问题,我在JSFiddle中夸大了视差效果。
PPS:测试时请确保您具有可滚动的页面(较小的浏览器高度),否则,效果和问题当然不会发生。
//sticky header menu $(window).scroll(function() { if ($(document).scrollTop() > 92) { if (!$('.fixed').length) { $('.menu').addClass('fixed'); } } else { if ($('.fixed').length) { $('.menu').removeClass('fixed'); } } }); // Parallax of content on scroll var iCurScrollPos = 0; $(window).scroll(function() { iCurScrollPos = $(this).scrollTop(); $('.content').css('margin-top', -iCurScrollPos * 1.2 + 'px') });
body { width: 100%; height: 100%; margin: 0px; padding: 0px; background: #ccc; } header { position: relative; width: 100%; background: #fff; z-index: 1; height: 146px; } .void { position: relative; width: 100%; height: 100px; } .menu { position: relative; width: 100%; height: 54px; background: #aaa; } .fixed { position: fixed; left: 0px; top: 0px; } img { width: 100% }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <header> <div class="void"></div> <nav class="menu"></nav> </header> <div class="content"> <img src="https://farm8.staticflickr.com/7632/16990947835_3894284fd8_b.jpg"> </div>
JSFiddle
您可以使用滚动位置的历史记录,通过比较最后两个位置是否与第三和第四位置相同,来确定是否发生了卡顿现象:
$(window).scroll(function() {
if ($(document).scrollTop() > 92){
if (!$('.fixed').length){$('.menu').addClass('fixed');}
}
else {
if ($('.fixed').length){$('.menu').removeClass('fixed');}
}
});
// Parallax of page on scroll
var iCurScrollPos = 0;
// Contains the last 4 scroll positons.
var lastPositions = [];
$(window).scroll(function () {
iCurScrollPos = $(this).scrollTop();
lastPositions.push(iCurScrollPos);
// Control over when locaties are marked as duplicates. Use it to fine tune the response.
var duplicateRange = 20;
// The stutter bug can be caught be checking if two last locations are the same as the 3rd and 4th.
if(Math.abs(lastPositions[0] - lastPositions[2]) < duplicateRange && Math.abs(lastPositions[1] - lastPositions[3]) < duplicateRange){
lastPositions = [];
return;
}
console.log(lastPositions);
if(lastPositions.length === 4){
lastPositions = [];
}
$('.content').css('margin-top',-iCurScrollPos*1.2+'px')
});
jsFiddle: https ://jsfiddle.net/maartendev/sj5egqhd/25/
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<header>
<div class="void"></div>
<nav class="menu">
</nav>
</header>
<div class="content">
<!-- Picture displayed twice for the example only -->
<!-- to be sure everyone gets scollable page-->
<img src="https://farm8.staticflickr.com/7632/16990947835_3894284fd8_b.jpg">
<img src="https://farm8.staticflickr.com/7632/16990947835_3894284fd8_b.jpg">
</div>
<style>
body{
width:100%;
height:100%;
margin:0px;
padding:0px;
background: #ccc;
}
header{
position:relative;
width:100%;
background: #fff;
z-index:1;
height:146px;
}
.void{
position:relative;
width:100%;
height:100px;
}
.menu{
position:relative;
width:100%;
height:54px;
background:#aaa;
}
.fixed{
position: fixed;
left:0px;
top: 0px;
}
img{
width:100%
}
</style>
<script>
// Sticky header menu
$(window).scroll(function() {
if ($(document).scrollTop() > 92){
if (!$('.fixed').length){$('.menu').addClass('fixed');}
}else {
if ($('.fixed').length){$('.menu').removeClass('fixed');}
}
});
// Parallax of page on scroll
var iCurScrollPos = 0;
$(window).scroll(function () {
console.log(iCurScrollPos ,iCurScrollPos - screen.height,$(window).height());
var max_sc = iCurScrollPos - screen.height;
iCurScrollPos = $(this).scrollTop();
if(iCurScrollPos < max_sc)
$('.content').css('margin-top',-iCurScrollPos*1.2+'px')
});
</script>
在文档末尾时,您必须停止滚动事件。
要计算您的页面结尾iCurScrollPos-screen.height 。
找到相同的jsfiddle链接。 https://jsfiddle.net/cpo73s5g/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.