当我的页面滚动(或当鼠标滚轮)到达距离顶部特定距离时,我想滚动彩色div。在mozilla中它可以工作但是在Chrome中,即当div“spostamento”时我有一个不好的效果重新定位。你能帮我解决这个问题吗?谢谢
HTML
<div class="tutto" style="width:100%;position:relative;">
<div class="spostamento" style="position:absolute;width:100%;">
<div style="width:100%;height:300px;background-color:#000000;">
</div>
<div class="enter" style="height:200px;width:100%;position:relative;overflow:hidden;">
<div class="fascia" style="height:200px;">
<div class="sposta" style="position:absolute;width:100%;left:0px;top:0px;height:200px;">
<div class="first" style="position:absolute;left:0px;height:200px;width:300px;background-color:#0F3"></div>
<div style="position:absolute;left:300px;height:200px;width:300px;background-color:#069"></div>
<div style="position:absolute;left:600px;height:200px;width:300px;background-color:#C09"></div>
<div style="position:absolute;left:900px;height:200px;width:300px;background-color:#6C6"></div>
<div style="position:absolute;left:1200px;height:200px;width:300px;background-color:#00F"></div>
<div style="position:absolute;left:1500px;height:200px;width:300px;background-color:#0F3"></div>
<div style="position:absolute;left:1800px;height:200px;width:300px;background-color:#6C6"></div>
<div style="position:absolute;left:2100px;height:200px;width:300px;background-color:#069"></div>
<div style="position:absolute;left:2400px;height:200px;width:300px;background-color:#C09"></div>
<div style="position:absolute;left:2700px;height:200px;width:300px;background-color:#6C6"></div>
<div style="position:absolute;left:3000px;height:200px;width:300px;background-color:#00F"></div>
<div style="position:absolute;left:3300px;height:200px;width:300px;background-color:#6C6"></div>
<div style="position:absolute;left:3600px;height:200px;width:300px;background-color:#069"></div>
<div style="position:absolute;left:3900px;height:200px;width:300px;background-color:#C09"></div>
<div style="position:absolute;left:4200px;height:200px;width:300px;background-color:#6C6"></div>
<div class="last" style="position:absolute;left:4500px;height:200px;width:300px;background-color:#0F3"></div>
</div>
</div>
</div>
<div style="width:100%;height:1000px;background-color:#000000;"></div>
</div>
</div>
脚本
$(document).ready(function(){
var left=0;
var lastScroll = 0;
var dimensioneSpostamento=0;
var distanzaFascia=$(".fascia").offset().top-200;
$(".sposta > div").each(function(){
dimensioneSpostamento +=$(this).width();
});
var dimensioneEffettiva=dimensioneSpostamento-$(window).width();
$(".tutto").height(dimensioneEffettiva+300+1000);
$(window).scroll(function (event) {
var st = $(this).scrollTop();
if(st>distanzaFascia&&st<dimensioneEffettiva){
$(".spostamento").css("top",st-distanzaFascia);
left +=st-lastScroll;
if (st > lastScroll) {
$(".sposta").css("left", -left);
}
else {
$(".sposta").css("left", -left);
}
lastScroll = st;
}
});
});
答案 0 :(得分:0)
我认为Firefox与Chrome之间的区别在于,Chrome每次鼠标滚动时都会同时滚动100px,默认情况下Firefox会启用平滑滚动,这会导致滚动几个像素,一次鼠标滚动一次。
这会导致您的示例出现问题,因为当$(".spostamento").css("top")
低于st
时,您未将distanzaFascia
设置回其原始值。你也可以在Firefox中获得相同的效果。但它更轻。
尝试以下方法。我认为应该这样做:
if (st <= distanzaFascia) {
$(".spostamento").css("top", 0);
$(".sposta").css("left", 0);
}
这是您更新的JSFiddle。