javascript实现点击按钮像上和像下是否缓动的效果(以后的操作dom库慢慢的完善)
发布于 11 年前 作者 zengwenbing 3706 次浏览 最后一次编辑是 8 年前

void function(){ var Yound = { /** 设置滚动的值 * value (string) 值 * return (undefined) 无 / setScroll:function(value){ var con = document.documentElement.scrollTop ? document.documentElement : document.body; con.scrollTop = value; }, / 设置滚动的值 * return (number) 返回滚动条当前的滚动高度 / getScroll:function(){ return (document.documentElement.scrollTop || document.body.scrollTop) }, / * els (node) 绑定点击的元素的事件 * fx (string) 代表滚动的方向 可写值 bottom 和 top * isHd(boolean) 是否缓动 true 代表是 false 代表不是 * return (undefined) **/ scroll:function(els,fx,isHd){
els = (typeof els === “string”) ? document.getElementById(els) : els; els.onclick = function(){ var scrollTop = getScroll(); if(fx === “bottom”){ //加 if(isHd){ var time = setInterval(function(){ scrollTop += 10; setScroll(scrollTop); if(getScroll() >= (document.documentElement.scrollHeight - document.documentElement.clientHeight )){ clearInterval(time); return; }

                	 	 },1000 / 30);
                	 }else{
                	 	 setScroll(document.documentElement.scrollHeight - document.documentElement.clientHeight );
                	 }

                            
                }
                else if(fx === "top"){
                     //减
                     if(isHd){

                     	  var time = setInterval(function(){
                     	  	 scrollTop -= 10;                  	
                     	  	 if(getScroll() <= 0){
    
                     	  	 	 clearInterval(time);
                     	  	     return;
                     	  	 }
                     	  	   	 setScroll(scrollTop);
                     	  	 

                     	  },1000 / 30); 
                     }else{
                     	  setScroll(0);

                     }


                }
      	 } 
      }

  };
  for(var k in Yound){
  	 window[k] = Yound[k];
  }

}();

调用

window.onload = function(){scroll(“dd”,“bottom”,true)}

回到顶部