Page custom scroll bar effect implemented by JS_javascript skills
The example in this article describes the page custom scroll bar effect implemented by JS. Share it with everyone for your reference, the details are as follows:
Here is a demonstration of the scroll bar effect used on a web page. It is a customized scroll bar code. Except for the up and down arrows, the scroll bar is basically the same as a normal browser. The mouse wheel scrolls and the scroll bar scrolls. The html structure is very simple, mainBox is the outer div, content is the content, and the scroll bar div is dynamically generated by js.
The screenshot of the running effect is as follows:
The online demo address is as follows:
http://demo.jb51.net/js/2015/js-web-zdy-scroll-style-codes/
The specific code is as follows:
<!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(http://files.jb51.net/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>
I hope this article will be helpful to everyone in JavaScript programming.

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version
Visual web development tools

Atom editor mac version download
The most popular open source editor
