国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

JS實現的頁面自定義滾動條效果

2019-11-20 11:24:10
字體:
來源:轉載
供稿:網友

本文實例講述了JS實現的頁面自定義滾動條效果。分享給大家供大家參考,具體如下:

這里演示網頁上用的滾動條效果,是一個自定義的滾動條代碼,除了上下兩個箭頭以外,滾動條和一般的瀏覽器基本差不多,鼠標滾輪滾動,滾動條滾動。html結構很簡單,mainBox是外層div,content是內容,滾動條div是js動態生成的。

運行效果截圖如下:

在線演示地址如下:

http://demo.VeVB.COm/js/2015/js-web-zdy-scroll-style-codes/

具體代碼如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>滾動條</title><style type="text/css">*{ margin:0; padding:0;}p{ height:1000px;}#mainBox{ width:400px; height:500px; border:1px #bbb solid; position:relative; overflow:hidden; margin:50px auto;}#content{ height:2500px; position:absolute; left:0; top:0; background:url(//files.VeVB.COm/file_images/article/201510/20151026113716032.jpg) }.scrollDiv{ width:18px; position:absolute; top:0; background:#666; border-radius:10px;}</style></head><body><div id="mainBox"> <div id="content"></div></div><p></p><script type="text/javascript">var doc=document;var _wheelData=-1;var mainBox=doc.getElementById('mainBox');function bind(obj,type,handler){ var node=typeof obj=="string"?$(obj):obj; if(node.addEventListener){  node.addEventListener(type,handler,false); }else if(node.attachEvent){  node.attachEvent('on'+type,handler); }else{  node['on'+type]=handler; }}function mouseWheel(obj,handler){ var node=typeof obj=="string"?$(obj):obj;  bind(node,'mousewheel',function(event){   var data=-getWheelData(event);   handler(data);   if(document.all){    window.event.returnValue=false;   }else{    event.preventDefault();   }  });  //火狐  bind(node,'DOMMouseScroll',function(event){   var data=getWheelData(event);   handler(data);   event.preventDefault();  });  function getWheelData(event){   var e=event||window.event;   return e.wheelDelta?e.wheelDelta:e.detail*40;  }}function addScroll(){ this.init.apply(this,arguments);}addScroll.prototype={ init:function(mainBox,contentBox,className){  var mainBox=doc.getElementById(mainBox);  var contentBox=doc.getElementById(contentBox);  var scrollDiv=this._createScroll(mainBox,className);  this._resizeScorll(scrollDiv,mainBox,contentBox);  this._tragScroll(scrollDiv,mainBox,contentBox);  this._wheelChange(scrollDiv,mainBox,contentBox);  this._clickScroll(scrollDiv,mainBox,contentBox); }, //創建滾動條 _createScroll:function(mainBox,className){  var _scrollBox=doc.createElement('div')  var _scroll=doc.createElement('div');  var span=doc.createElement('span');  _scrollBox.appendChild(_scroll);  _scroll.appendChild(span);  _scroll.className=className;  mainBox.appendChild(_scrollBox);  return _scroll; }, //調整滾動條 _resizeScorll:function(element,mainBox,contentBox){  var p=element.parentNode;  var conHeight=contentBox.offsetHeight;  var _width=mainBox.clientWidth;  var _height=mainBox.clientHeight;  var _scrollWidth=element.offsetWidth;  var _left=_width-_scrollWidth;  p.style.width=_scrollWidth+"px";  p.style.height=_height+"px";  p.style.left=_left+"px";  p.style.position="absolute";  p.style.background="#ccc";  contentBox.style.width=(mainBox.offsetWidth-_scrollWidth)+"px";  var _scrollHeight=parseInt(_height*(_height/conHeight));  if(_scrollHeight>=mainBox.clientHeight){   element.parentNode.style.display="none";  }  element.style.height=_scrollHeight+"px"; }, //拖動滾動條 _tragScroll:function(element,mainBox,contentBox){  var mainHeight=mainBox.clientHeight;  element.onmousedown=function(event){   var _this=this;   var _scrollTop=element.offsetTop;   var e=event||window.event;   var top=e.clientY;   //this.onmousemove=scrollGo;   document.onmousemove=scrollGo;   document.onmouseup=function(event){    this.onmousemove=null;   }   function scrollGo(event){    var e=event||window.event;    var _top=e.clientY;    var _t=_top-top+_scrollTop;    if(_t>(mainHeight-element.offsetHeight)){     _t=mainHeight-element.offsetHeight;    }    if(_t<=0){     _t=0;    }    element.style.top=_t+"px";    contentBox.style.top=-_t*(contentBox.offsetHeight/mainBox.offsetHeight)+"px";    _wheelData=_t;   }  }  element.onmouseover=function(){   this.style.background="#444";   }  element.onmouseout=function(){   this.style.background="#666";   } }, //鼠標滾輪滾動,滾動條滾動 _wheelChange:function(element,mainBox,contentBox){  var node=typeof mainBox=="string"?$(mainBox):mainBox;  var flag=0,rate=0,wheelFlag=0;  if(node){   mouseWheel(node,function(data){    wheelFlag+=data;    if(_wheelData>=0){     flag=_wheelData;     element.style.top=flag+"px";     wheelFlag=_wheelData*12;     _wheelData=-1;    }else{     flag=wheelFlag/12;    }    if(flag<=0){     flag=0;     wheelFlag=0;    }    if(flag>=(mainBox.offsetHeight-element.offsetHeight)){     flag=(mainBox.clientHeight-element.offsetHeight);     wheelFlag=(mainBox.clientHeight-element.offsetHeight)*12;    }    element.style.top=flag+"px";    contentBox.style.top=-flag*(contentBox.offsetHeight/mainBox.offsetHeight)+"px";   });  } }, _clickScroll:function(element,mainBox,contentBox){  var p=element.parentNode;  p.onclick=function(event){   var e=event||window.event;   var t=e.target||e.srcElement;   var sTop=document.documentElement.scrollTop>0?document.documentElement.scrollTop:document.body.scrollTop;   var top=mainBox.offsetTop;   var _top=e.clientY+sTop-top-element.offsetHeight/2;   if(_top<=0){    _top=0;   }   if(_top>=(mainBox.clientHeight-element.offsetHeight)){    _top=mainBox.clientHeight-element.offsetHeight;   }   if(t!=element){    element.style.top=_top+"px";    contentBox.style.top=-_top*(contentBox.offsetHeight/mainBox.offsetHeight)+"px";    _wheelData=_top;   }  } }}new addScroll('mainBox','content','scrollDiv');</script></body></html>

希望本文所述對大家JavaScript程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 泊头市| 大城县| 文安县| 南宁市| 平利县| 多伦县| 交城县| 湟源县| 石门县| 马山县| 牙克石市| 开原市| 尼木县| 平乡县| 临桂县| 巨野县| 剑阁县| 南木林县| 宿迁市| 公主岭市| 安福县| 金乡县| 马山县| 乐东| 确山县| 乌海市| 赣榆县| 景东| 沙湾县| 青州市| 长汀县| 商丘市| 延庆县| 甘肃省| 长丰县| 故城县| 郓城县| 和田县| 四川省| 什邡市| 澜沧|