12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- // 获取id命名的标签
- var btn1 = document.getElementById("btn1");
- var btn2 = document.getElementById("btn2");
- var a;
- // 点击事件
- btn1.onclick = function(){
- // 定时器
- a = setInterval(function(){
- // scrollTo(x,y) 相对于窗口
- // scrollBy(x,y) 相对于当前位置
- // scrollBy(0,200);
- // 滚动高度
- // document.body.scrollTop
- // document.documentElement.scrollTop
- // scrollBy(0,200)
- var scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
- // console.log(scrollTop)
- if(scrollTop < 1799) {
- scrollBy(0,200);
- } else {
- clearInterval(a);
- window.alert("到底了");
- }
- },500)
- // setInterval(function(){},时间)
- }
- // var btn3 = document.getElementById("btn3");
- // btn3.onclick = function() {
- // // 清除定时器的方法 clearInterval()
- // clearInterval(a);
- // }
- btn2.onclick = function() {
- var b = setInterval(function(){
- var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
- if(scrollTop > 0) {
- scrollBy(0,-200);
- } else {
- clearInterval(b);
- alert("回到顶部了");
- }
- },1000)
- }
|