<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>