今天来个 缓动动画的小例子 ,是为了方便自己日后的复制粘贴,若是能帮助到大家,拿走不谢。话不多说,直接上代码。
<style type="text/css">
*{
margin: 0;
padding: 0;
}
.box{
width: 200px;
height: 200px;
background: yellow;
text-align: center;
position: relative;
top: 0px;
left: 0;
}
button{
padding: 0 20px;
height: 40px;
margin-top: 40px;
}
</style>
<div class="box">
动画越来越来越慢,叫缓动动画
</div>
<button>点击有动画效果</button>
js 部分:
<script type="text/javascript">
var odiv = document.querySelector('.box');
var obtn = document.querySelector('button');
function animate(odiv,target,callback){
clearInterval(odiv.timer);
odiv.timer = setInterval(function(){
console.log(1);
var step = (target - odiv.offsetLeft) / 10 ;// 目标距离-现在的距离
step = step > 0 ? Math.ceil(step) : Math.floor(step);
if (odiv.offsetLeft === target) {
c