首页 > 其他 > 详细

动画函数

时间:2019-12-02 09:56:16      阅读:69      评论:0      收藏:0      [点我收藏+]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,user-scalable=no,viewport-fit=cover">
<title>动画函数</title>
<style>
* {
margin: 0;
padding: 0;
}
#box {
width: 100px;
height: 100px;
background: red;
position: absolute;
top: 60px;
left: 0;
}
</style>
</head>
<body>
<button id="btn1">滑动到400px</button>
<button id="btn2">滑动到800px</button>
<div id="box"></div>
<script>
var btn1 = document.querySelector(‘#btn1‘);
var btn2 = document.querySelector(‘#btn2‘);
var box = document.querySelector(‘#box‘);

btn1.addEventListener(‘click‘, function () {
animate(box, 400);
});
btn2.addEventListener(‘click‘, function () {
animate(box, 800);
});

//封装动画函数
function animate(obj, target) {
clearInterval(obj.timerId);
obj.timerId = setInterval(function () {
var step = 9;
var current = obj.offsetLeft;
step = current < target ? step : -step;
current = current + step;
if (Math.abs(target - current) > Math.abs(step)) {
obj.style.left = current + ‘px‘;
}else {
obj.style.left = target + ‘px‘;
clearInterval(obj.timerId);
}
}, 30)
}
</script>
</body>
</html>

动画函数

原文:https://www.cnblogs.com/wangsai-666/p/11968682.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!