js控制滚动条(简单插件手写)

xiaoxiao2021-02-28  126

<head> <meta charset="UTF-8"> <title></title> <style> body { height: 5000px; } input{position:fixed;top:100px;} </style> </head> <body> <input id="text" type="number" /> </body> <script type="text/javascript"> text.onkeyup = function(){ var goTop = new GoTop(parseInt(this.value)) } /* garget:目标位置 speed:速度 * */ var currentPosition; function GoTop(target,speed) { var that = this; this.runToTop = function(){ currentPosition = document.documentElement.scrollTop || document.body.scrollTop; currentPosition < target?currentPosition += 10:currentPosition -= 10; window.scrollTo(0, currentPosition); //console.log(currentPosition - target); if(currentPosition - target < 11 && currentPosition - target > -11){ clearInterval(that.timer) } }; this.timer = setInterval(this.runToTop, speed || 10); } </script>

转载请注明原文地址: https://www.6miu.com/read-72488.html

最新回复(0)