<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<script>
function startMove(obj,json,fnEnd){
clearInterval(obj.timer);
obj.timer=setInterval(function(){
let bStop=true;
let cur=0;
for(let attr in json){
if(attr=='opacity'){
cur=Math.round(parseFloat(getComputedStyle(obj,false)[attr])*100);
}else if(attr=='scrollTop'){
cur=obj[attr]
}
else{
cur=parseInt(getComputedStyle(obj,false)[attr])
}
if(cur!=json[attr])
bStop=false
let speed=(json[attr]-cur)/6;
speed=speed>0?Math.ceil(speed):Math.floor(speed);
if(attr=='opacity'){
obj.style.opacity=(cur+speed)/100
}else if(attr=='scrollTop'){
//火狐滚轮事件
if (obj.addEventListener) {
obj.addEventListener('DOMMouseScroll', function (ev) {
clearInterval(obj.timer)
}, false);
}
// chrome滚轮事件
obj.onmousewheel = function (ev) {
clearInterval(obj.timer)
};
if(speed==1)
speed=2;
else if(speed==-1)
speed=-2
obj[attr]=cur+speed;
}else{
obj.style[attr]=cur+speed+'px'
}
}
if(bStop){
if(fnEnd)
fnEnd();
clearInterval(obj.timer)
}
},30)
}
window.onload=function(){
startMove(document.documentElement, { scrollTop: 0 })
var oDiv=document.querySelector('div')
var oBtn=document.querySelector('input[type=button]')
oBtn.onclick=function(){
startMove(oDiv, { top: 300, left: 300, opacity: 0 })
startMove(document.documentElement, {scrollTop:500})
}
}
</script>
<body style="height:3000px;">
<div style="width:100px;height:100px;background:red;position:absolute;left:100px;top:0;"></div>
<input type="button" value="移动"/>
</body>
</html>
2019-05-23 带指定滚动条位置的缓冲动画框架
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- # 第一种方法:focus (这个是元素正好在屏幕中间) targetElem = browser.find_el...
- 一,宽、高 jQuery中获取宽高,最经典的就是width()和height()。因为CSS盒子模型的缘故,元素的...
- 原网址:https://www.cnblogs.com/jaxu/archive/2013/05/17/30830...
- 滚动页面到指定元素位置(有滚动条的overflow:hidden内部也可以)main是这个overflow:hid...