js實現緩動動畫
阿新 • • 發佈:2020-11-27
本文例項為大家分享了js實現緩動動畫的具體程式碼,供大家參考,具體內容如下
利用定時器來控制元素的offsetLeft的值,offsetLeft = 開始位置 + (最終位置 - 開始位置)* 緩動係數
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> * { margin: 0; padding: 0; border: none; list-style: none; } body { background-color: pink; } #nav { width: 900px; height: 63px; background: url("images/doubleOne.png") no-repeat right center #fff; margin: 0 auto; margin-top: 50px; border-radius: 5px; position: relative; } #nav ul { line-height: 70px; } #nav ul li { float: left; height: 63px; width: 88px; text-align: center; cursor: pointer; position: relative; } #t_mail { width: 88px; height: 63px; background: url("images/tMall.png") no-repeat; position: absolute; } </style> </head> <body> <nav id="nav"> <span id="t_mail"></span> <ul> <li>雙11狂歡</li> <li>服裝會場</li> <li>數碼家電</li> <li>母嬰童裝</li> <li>手機會場</li> <li>美妝會場</li> <li>家居建材</li> <li>進口會場</li> <li>飛豬旅行</li> </ul> </nav> <script> window.onload = function () { var nav = $('nav'); var t_mall = nav.children[0]; var ul = nav.children[1]; var allLis = ul.children; var beginX = 0; for (var i = 0; i < allLis.length; i++) { var li = allLis[i]; li.onmouseover = function () { end = this.offsetLeft; } li.onmousedown = function () { beginX = this.offsetLeft; } li.onmouseout = function () { end = beginX; } } var begin = 0,end = 0; setInterval(function () { begin = begin + (end - begin) / 10; t_mall.style.left = begin + 'px'; },10) function $(id) { return typeof id ? document.getElementById(id) : null; } } </script> </body> </html>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。