js实现点击按钮滚动条缓慢滚动到顶部


            toTop:function(){
                //toTop 滚动到顶部
                var currentPosition,timer;
                var speed=10;
                timer=setInterval(function(){
                    currentPosition=document.documentElement.scrollTop || document.body.scrollTop;
                    currentPosition-=speed; //speed变量
                    if(currentPosition>0){
                        window.scrollTo(0,currentPosition);
                    }else{
                        window.scrollTo(0,0);
                        clearInterval(timer);
                    }
                },1);
            }    

 

【注意】document.body.scrollTop的值一直是0的情况:

1.页面指定了DTD,即指定了DOCTYPE时,使用document.documentElement

2.页面没有DTD,即没指定DOCTYPE时,使用document.body


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM