JS實(shí)現(xiàn)的頁(yè)面自定義滾動(dòng)條效果
本文實(shí)例講述了JS實(shí)現(xiàn)的頁(yè)面自定義滾動(dòng)條效果。分享給大家供大家參考,具體如下:
這里演示網(wǎng)頁(yè)上用的滾動(dòng)條效果,是一個(gè)自定義的滾動(dòng)條代碼,除了上下兩個(gè)箭頭以外,滾動(dòng)條和一般的瀏覽器基本差不多,鼠標(biāo)滾輪滾動(dòng),滾動(dòng)條滾動(dòng)。html結(jié)構(gòu)很簡(jiǎn)單,mainBox是外層div,content是內(nèi)容,滾動(dòng)條div是js動(dòng)態(tài)生成的。
運(yùn)行效果截圖如下:

在線演示地址如下:
http://demo.jb51.net/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>滾動(dòng)條</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(//img.jbzj.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);
},
//創(chuàng)建滾動(dòng)條
_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;
},
//調(diào)整滾動(dòng)條
_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";
},
//拖動(dòng)滾動(dòng)條
_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";
}
},
//鼠標(biāo)滾輪滾動(dòng),滾動(dòng)條滾動(dòng)
_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>
希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。
- JS實(shí)現(xiàn)判斷滾動(dòng)條滾到頁(yè)面底部并執(zhí)行事件的方法
- js監(jiān)聽(tīng)html頁(yè)面的上下滾動(dòng)事件方法
- js,jquery滾動(dòng)/跳轉(zhuǎn)頁(yè)面到指定位置的實(shí)現(xiàn)思路
- 當(dāng)滾動(dòng)條滾動(dòng)到頁(yè)面底部自動(dòng)加載增加內(nèi)容的js代碼
- js實(shí)現(xiàn)滾動(dòng)條滾動(dòng)到頁(yè)面底部繼續(xù)加載
- js判斷滾動(dòng)條是否已到頁(yè)面最底部或頂部實(shí)例
- JS和JQUERY獲取頁(yè)面大小,滾動(dòng)條位置,元素位置(示例代碼)
- js阻止移動(dòng)端頁(yè)面滾動(dòng)的兩種方法
- js實(shí)現(xiàn)刷新頁(yè)面后回到記錄時(shí)滾動(dòng)條的位置【兩種方案可選】
- JavaScript實(shí)現(xiàn)頁(yè)面無(wú)縫滾動(dòng)效果
相關(guān)文章
關(guān)于JS中一維數(shù)組和二維數(shù)組互轉(zhuǎn)問(wèn)題
這篇文章主要介紹了js中一維數(shù)組和二維數(shù)組互轉(zhuǎn),本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04
js純數(shù)字逐一停止顯示效果的實(shí)現(xiàn)代碼
下面小編就為大家?guī)?lái)一篇js純數(shù)字逐一停止顯示效果的實(shí)現(xiàn)代碼。小編覺(jué)得非常不錯(cuò)?,F(xiàn)在分享給大家。給大家一個(gè)參考2016-03-03
javascript Promise簡(jiǎn)單學(xué)習(xí)使用方法小結(jié)
下面小編就為大家?guī)?lái)一篇javascript Promise簡(jiǎn)單學(xué)習(xí)使用方法小結(jié)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-05-05
JS函數(shù)多個(gè)參數(shù)默認(rèn)值指定方法分析
這篇文章主要介紹了JS函數(shù)多個(gè)參數(shù)默認(rèn)值指定方法,結(jié)合實(shí)例形式分析了javascript函數(shù)參數(shù)的定義與傳遞相關(guān)操作技巧,需要的朋友可以參考下2016-11-11
javascript動(dòng)態(tài)控制服務(wù)器控件實(shí)例
在頁(yè)面中放入一個(gè)DropDownList控件,并添加一項(xiàng),用來(lái)分析其產(chǎn)生的HTML代碼,這樣在使用js進(jìn)行動(dòng)態(tài)控制時(shí),將會(huì)非常清晰2014-09-09
深入淺析JavaScript中的arguments對(duì)象(強(qiáng)力推薦)
這篇文章主要介紹了JavaScript中的arguments對(duì)象(強(qiáng)力推薦)的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06
JS實(shí)現(xiàn)圖片無(wú)間斷滾動(dòng)代碼匯總
這篇文章主要介紹了JS實(shí)現(xiàn)圖片無(wú)間斷滾動(dòng)代碼匯總,非常實(shí)用的特效代碼,需要的朋友可以參考下2014-07-07
基于JS實(shí)現(xiàn)任務(wù)隊(duì)列的示例代碼
顧名思義,任務(wù)隊(duì)列就是存放任務(wù)的隊(duì)列,隊(duì)列中的任務(wù)都嚴(yán)格按照進(jìn)入隊(duì)列的先后順序執(zhí)行,所以下面我們就來(lái)看看如何基于JS實(shí)現(xiàn)任務(wù)隊(duì)列吧2023-08-08
詳解如何在微信小程序開(kāi)發(fā)中正確的使用vant ui組件
這篇文章主要介紹了詳解如何在微信小程序開(kāi)發(fā)中正確的使用vant ui組件,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-09-09

