动画函数封装:设置任意的一个元素,移动到指定的目标位置
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>title</title> <style> div{ width: 200px; height: 200px; background-color: red; /*脱离文档流*/ position: absolute; } </style> <script> //设置任意的一个元素,移动到指定的目标位置 function animate(element, target) { clearinterval(element.timeid); //定时器的id值存储到对象的一个属性中 element.timeid = setinterval(function () { //获取元素的当前的位置,数字类型 var current = element.offsetleft; //每次移动的距离 var step = 10; step = current < target ? step : -step; //当前移动到位置 current += step; if (math.abs(current - target) > math.abs(step)) { element.style.left = current + "px"; } else { //清理定时器 clearinterval(element.timeid); //直接到达目标 element.style.left = target + "px"; } }, 20); } </script> </head> <body> </body> </html>
您可能感兴趣的文章:
- 使用vue实现复选框单选多选
- vue - Error: Can't resolve '@/assets/img/github.svg (vue-cli3.0,无法解析.svg图片,已解决)
- HTML中使背景图片自适应浏览器大小实例详解
- 使用ant design组件时,Select设置mode="multiple"或mode="tags"时遇到问题:Uncaught Error: must set key for <rc-animate> children
- javascript 焦点的两个问题
- 磁力搜索网站 BT torrent search engine 推荐 2019/12/25日更新
- vue-cli启动本地服务局域网不能访问的原因分析
如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!
网友评论