为什么scrollTop跳
Why is scrollTop jumpy?
这个简单的问题很难解释,所以我拍了一个视频代替。参见链接:http://1drv.ms/1zvrYV8
当我向下滚动页面时,它可以完美地将图像替换为系列中的下一个图像。
问题:当我停止滚动时,它弹出图像回到以前的图像。我保持什么scrollTop(这是什么是记录到控制台在我的视频)返回的记录,它似乎在滚动结束时(当我从鼠标垫抬起我的手指)返回一个低值0左右。
为什么??当我把手指从滚轮或触控板上移开时,我只想让它保持在原来的位置。
下面是我的代码:<html>
<meta charset="UTF-8">
<head>
<script type="text/javascript">
var lastScrollTop;
function mainScript(){
bgResize();
lastScrollTop = document.body.scrollTop;
window.addEventListener("scroll", bgAnimation, false);
window.addEventListener('resize', bgResize);
}
function bgAnimation(){
var currentScrollTop = document.body.scrollTop;
var currentBg = document.getElementById("pictureAnimation").src;
var currentBgNum = currentBg.substring(currentBg.length-8, currentBg.length-4);
console.log(lastScrollTop);
if(currentScrollTop > lastScrollTop){
if((currentBgNum < 0196) && (currentBgNum > 99)){
var nextBgNum = Number(currentBgNum)+1;
nextBgNum = String("0" + nextBgNum);
var nextBg = currentBg.replace(currentBgNum, nextBgNum);
document.getElementById("pictureAnimation").src = nextBg;
lastScrollTop = currentScrollTop;
}
}
else if(currentScrollTop < lastScrollTop){
if((currentBgNum < 0197) && (currentBgNum > 100)){
//decrement image number by 1
var nextBgNum = Number(currentBgNum)-1;
//update image
nextBgNum = String("0" + nextBgNum);
var nextBg = currentBg.replace(currentBgNum, nextBgNum);
document.getElementById("pictureAnimation").src = nextBg;
lastScrollTop = currentScrollTop;
}
}
}
function bgResize(){
var newWindowWidth = 0, newWindowHeight = 0;
var emcPictureHeight = 1080, emcPictureWidth = 1920;
if( typeof( window.innerWidth ) == 'number' ) {
//Non-IE
newWindowWidth = window.innerWidth;
newWindowHeight = window.innerHeight;
}
else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
//IE 6+ in 'standards compliant mode'
newWindowWidth = document.documentElement.clientWidth;
newWindowHeight = document.documentElement.clientHeight;
}
else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
//IE 4 compatible
newWindowWidth = document.body.clientWidth;
newWindowHeight = document.body.clientHeight;
}
var scale = Math.max(newWindowWidth/emcPictureWidth, newWindowHeight/emcPictureHeight);
var width = scale * emcPictureWidth , height = scale * emcPictureHeight;
var left = (newWindowWidth-width)/2 , top = (newWindowHeight-height)/2;
var emcView = document.getElementById("emcView");
document.getElementById("emcView").style.width = width + "px";
emcView.style.height = height + "px";
emcView.style.position = "fixed";
emcView.style.left = left + "px";
emcView.style.zindex = 0;
emcView.style.top = top + "px";
//return true;
}
window.onload=function() {
mainScript();
};
</script>
<noscript>
<h3>This site requres JavaScript</h3>
</noscript>
<title> ELDP Awesomeness </title>
<style>
#emcView {
width:10px;
}
</style>
</head>
<body cz-shortcut-listen="true">
<div id ="emcView">
<image id = "pictureAnimation" src="Sequence_Pictures/Sequence0100.jpg">
</div>
</body>
</html>
您正在根据图像文件名的编号更新lastScrollPosition,尝试如下:
if(currentScrollTop > lastScrollTop){
if((currentBgNum < 0196) && (currentBgNum > 99)){
var nextBgNum = Number(currentBgNum)+1;
nextBgNum = String("0" + nextBgNum);
var nextBg = currentBg.replace(currentBgNum, nextBgNum);
document.getElementById("pictureAnimation").src = nextBg;
//lastScrollTop = currentScrollTop; remove this
}
}
else if(currentScrollTop < lastScrollTop){
if((currentBgNum < 0197) && (currentBgNum > 100)){
//decrement image number by 1
var nextBgNum = Number(currentBgNum)-1;
//update image
nextBgNum = String("0" + nextBgNum);
var nextBg = currentBg.replace(currentBgNum, nextBgNum);
document.getElementById("pictureAnimation").src = nextBg;
//lastScrollTop = currentScrollTop; remove this
}
}
lastScrollTop = currentScrollTop; // put it here so that it gets updated on every scroll event
相关文章:
- 为什么不't Javascript对我的输入值进行了一些重新检查
- 为什么“;未定义的“;在JavaScript中结束循环
- 为什么这在IE中的工作方式与在Firefox中不同
- 知道为什么我的旋转木马不会自动更改图片吗
- 为什么会出现错误;未捕获的类型错误:undefined不是函数;
- 为什么在单独的函数中应用时转换会闪烁/断断续续(D3)
- 为什么在变形之前不缺少Fx
- 为什么JavaScript在for循环为3时向所有4发出警报
- 为什么不是't窗口.恢复正常工作吗?(javascript/jquery)
- 为什么Airbnb风格指南说不鼓励依赖函数名称推断
- 为什么要使用0>javascript中的0
- 为什么无法在TypeScript中导出类实例
- 为什么grunt contrib connect的中间件选项的第三个参数是未定义的
- 为什么 body.scrollTop 被弃用了
- 为什么我的 scrollTop 用于检测何时滚动到页面底部不起作用
- 为什么scrollTop,pageYOffset和height都不起作用
- 为什么.animate to scrollTop在动画之前会跳到0
- 为什么scrollTop跳
- 为什么可以't我得到了scrollTop()
- 为什么.scrollTop();不能正常工作