请教一下前辈们,我写的这个随滚动条滚动的层的动画效果有点问题:
当滚动条滚动滚动距离比较短(或者快速滚动滚动条)的时候,滚动效果就不是那么平滑了,
请教前辈们该怎么修改,才能使滚动的动画效果比较平滑?谢谢!!<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
body{
margin:0px;
padding:0px;
}
#container{
width:600px;
height:800px;
background:#ccc;
margin:0px auto;
}
#mes{
width:200px;
height:100px;
background:#aaa;
position:absolute;
left:0;
top:0;
}
</style>
</head><body>
<div id="container"></div>
<div id="mes">test</div>
<script type="text/javascript">
var mes=document.getElementById("mes");
window.onscroll=function(){
if (mes.movement) {
clearInterval(mes.movement);
}
mes.movement=setInterval("changePos()",2);
}
cur_pos=mes.scrollTop;
function changePos(){
var center_pos=document.documentElement.scrollTop+(document.documentElement.clientHeight-mes.clientHeight)/2;
if(cur_pos<center_pos)
{
cur_pos=cur_pos+3;
mes.style.top=cur_pos+"px";
}
if(cur_pos>center_pos)
{
cur_pos=cur_pos-3;
mes.style.top=cur_pos+"px";
}
}
</script>
</body>
</html>
当滚动条滚动滚动距离比较短(或者快速滚动滚动条)的时候,滚动效果就不是那么平滑了,
请教前辈们该怎么修改,才能使滚动的动画效果比较平滑?谢谢!!<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
body{
margin:0px;
padding:0px;
}
#container{
width:600px;
height:800px;
background:#ccc;
margin:0px auto;
}
#mes{
width:200px;
height:100px;
background:#aaa;
position:absolute;
left:0;
top:0;
}
</style>
</head><body>
<div id="container"></div>
<div id="mes">test</div>
<script type="text/javascript">
var mes=document.getElementById("mes");
window.onscroll=function(){
if (mes.movement) {
clearInterval(mes.movement);
}
mes.movement=setInterval("changePos()",2);
}
cur_pos=mes.scrollTop;
function changePos(){
var center_pos=document.documentElement.scrollTop+(document.documentElement.clientHeight-mes.clientHeight)/2;
if(cur_pos<center_pos)
{
cur_pos=cur_pos+3;
mes.style.top=cur_pos+"px";
}
if(cur_pos>center_pos)
{
cur_pos=cur_pos-3;
mes.style.top=cur_pos+"px";
}
}
</script>
</body>
</html>
width:200px;
height:100px;
background:#aaa;
position:fixed;
left:0;
top:0;
}
这样你满意吗~