基于JavaScript實(shí)現(xiàn)自定義滾動(dòng)條
更新時(shí)間:2017年01月25日 11:15:04 作者:萬(wàn)花果子
這篇文章主要為大家詳細(xì)介紹了基于JavaScript實(shí)現(xiàn)自定義滾動(dòng)條,可以直接使用的滾動(dòng)條,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
可直接使用的js滾動(dòng)條,先看看效果圖:

代碼如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>自定義滾動(dòng)條</title>
</head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>滾動(dòng)條</title>
<style type="text/css">
*{ margin:0; padding:0;}
#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(https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1485320653275&di=b7bc29cc2c7b3388d44958d8f97db0a3&imgtype=0&src=http%3A%2F%2Fphotocdn.sohu.com%2F20151212%2Fmp48094209_1449905365456_7.jpg)
}
.scrollDiv{
width:18px;
position:absolute;
top:0;
background:#666;
border-radius:10px;
}
</style>
</head>
<body>
<div id="mainBox">
<div id="content"></div>
</div>
<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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- JS自定義滾動(dòng)條效果
- js滾輪事件 js自定義滾動(dòng)條的實(shí)現(xiàn)
- 原生JS實(shí)現(xiàn)自定義滾動(dòng)條效果
- JS自定義滾動(dòng)條效果簡(jiǎn)單實(shí)現(xiàn)代碼
- JavaScript限定范圍拖拽及自定義滾動(dòng)條應(yīng)用(3)
- 原生js封裝自定義滾動(dòng)條
- JavaScript自定義瀏覽器滾動(dòng)條兼容IE、 火狐和chrome
- JavaScript 輪播圖和自定義滾動(dòng)條配合鼠標(biāo)滾輪分享代碼貼
- javascript自定義滾動(dòng)條實(shí)現(xiàn)代碼
- JS實(shí)現(xiàn)的頁(yè)面自定義滾動(dòng)條效果
- js實(shí)現(xiàn)自定義滾動(dòng)條的示例
相關(guān)文章
JS獲取字符對(duì)應(yīng)的ASCII碼實(shí)例
下面小編就為大家?guī)?lái)一篇JS獲取字符對(duì)應(yīng)的ASCII碼實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就想給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09
詳述JavaScript實(shí)現(xiàn)繼承的幾種方式(推薦)
這篇文章主要介紹了詳述JavaScript實(shí)現(xiàn)繼承的幾種方式(推薦)的相關(guān)資料,需要的朋友可以參考下2016-03-03
JS Map 和 List 的簡(jiǎn)單實(shí)現(xiàn)代碼
本篇文章是對(duì)在JS中Map和List的簡(jiǎn)單實(shí)現(xiàn)代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-07-07
javascript實(shí)現(xiàn)可鍵盤(pán)控制的抽獎(jiǎng)系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)可鍵盤(pán)控制的抽獎(jiǎng)系統(tǒng)的相關(guān)資料,需要的朋友可以參考下2016-03-03
關(guān)于在Typescript中做錯(cuò)誤處理的方式詳解
錯(cuò)誤處理是軟件工程重要的一部分,如果處理得當(dāng),它可以為你節(jié)省數(shù)小時(shí)的調(diào)試和故障排除時(shí)間,我發(fā)現(xiàn)了與錯(cuò)誤處理相關(guān)的三大疑難雜癥:TypeScript的錯(cuò)誤類(lèi)型,變量范圍和嵌套,讓我們逐一深入了解它們帶來(lái)的撓頭問(wèn)題,感興趣的朋友可以參考下2023-09-09
基于JavaScript實(shí)現(xiàn)動(dòng)態(tài)雨滴特效
這篇文章主要為大家詳細(xì)介紹了如何利用JavaScript實(shí)現(xiàn)動(dòng)態(tài)雨滴特效并且點(diǎn)擊雨滴會(huì)有雨滴爆裂的效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06

