JavaScript Learning custom scroll bar plus wheel Event

Source: Internet
Author: User

To write a custom scroll bar with the wheel event, the previous no wheel event is incomplete, and today organized a.

1. Roller events are incompatible, Firefox is required to use event binding add, with the Dommousescroll, when scrolling the mouse, as long as the mouse scrolling method on the line, and this by detecting the sign of datail can be determined

2. Other use MouseWheel (ie is onmousewheel), through the detection of Wheeldelta

1<!doctype html>234<title> drag-Plus wheel </title>5<style>6 . main{width:300px; height:400px; background: #ccc; margin:10px Auto;overflow:hidden;}7. main-content{width:300px; position:relative; height:400px; overflow:hidden;}8. main-content-c{width:280px; background: #0F9932; position:absolute; top:0;}9. target{width:20px; background: #eee; height:400px; position:absolute; top:0;right:0;}Ten. bar{width:20px;min-height:10px; background: #333; border-radius:10px; position:absolute; top:0;} One</style> A<script> -Window.onload=function(){ -             varOcontent = document.getElementById ("Content"); the             varOtxt = Getbyclass (ocontent, "main-content-c") [0]; -             varOtarg = Getbyclass (ocontent, "target") [0]; -             varObar = Getbyclass (ocontent, "bar") [0]; -  +OBar.style.height = otarg.offsetheight* (ocontent.offsetheight/otxt.offsetheight) + ' px '; -  +Obar.onmousedown =function(EV) { A                 varoevent = EV | |event; at                 varDisy = Oevent.clienty-Obar.offsettop; -Document.onmousemove =function(EV) { -                     varoevent = EV | |event; -                     vart = oevent.clienty-Disy; - SetPos (t); -                 } inDocument.onmouseup =function(){ -Document.onmousemove =NULL; toDocument.onmouseup =NULL; +  -Obar.releasecapture &&obar.releasecapture (); the                 } *Obar.setcapture &&obar.setcapture (); $Oevent.preventdefault &&Oevent.preventdefault ();Panax Notoginseng                 return false; -             } the             functionSetPos (t) { +                 if(t<0){ AT =0; the}Else if(t>otarg.offsetheight-obar.offsetheight) { +t = otarg.offsetheight-Obar.offsetheight; -                 } $OBar.style.top = t+ ' px '; $  -                 varScale = t/(otarg.offsetheight-obar.offsetheight); -OTxt.style.top =-scale* (otxt.offsetheight-ocontent.offsetheight) + ' px '; the  -             }Wuyi  theAddmousewheel (Ocontent,function(down) { -                 vart =Obar.offsettop; Wu                 if(down) { -T +=10; About}Else{ $T-=10; -                 } - SetPos (t); -             }); A  +         } the  -         functionGetbyclass (oparent,sclass) { $             if(oparent.getelementsbyclassname) { the                 returnOparent.getelementsbyclassname (sclass); the             } the             varAele = Oparent.getelementsbytagname ("*"); the             varARes = []; -             varReg =NewREGEXP (' \\b ' +sclass+ ' \\b '); in              for(vari=0;i<aele.length;i++){ the                 if(Reg.test (aele[i].classname)) { the Ares.push (Aele[i]); About                 } the             } the             returnAele; the         } +         functionAddmousewheel (OBJ,FN) { -             functionFnwhell (EV) { the                 varoevent = EV | |event;Bayi                 varBdown =false; the  theBdown = Oevent.wheeldelta? Oevent.wheeldelta<0:oevent.detail>0; -  -FN &&fn (bdown,oevent); theOevent.preventdefault &&Oevent.preventdefault (); the                 return false; the             } the             if(Window.navigator.userAgent.toLowerCase (). IndexOf ("Firefox")!=-1){ -Obj.addeventlistener ("Dommousescroll", Fnwhell,false); the}Else{ theAddevent (obj, ' MouseWheel ', Fnwhell); the             }94         } the         functionaddevent (OBJ,SEV,FN) { the             if(obj.addeventlistener) { theObj.addeventlistener (SEV,FN,false);98}Else{ AboutObj.attachevent (' on ' +SEV,FN); -             }101         }102</script>103104<body> the<div class= "Main" >106<div class= "main-content" id= "Content" >107<div class= "Main-content-c" >108 just as I stretched out, planning to go to the toilet, suddenly think of one thing: if the module more and more, so many files are to be loaded separately? Wouldn't that seriously affect performance!? (What, you don't know why?) )109 to compress the merged JavaScript! So I choked the liquid, started compressing it with Yuicompressor, and manually merged two files.  the here will not show the results, because it is very painful, completely sorry I just suppressed the courage of the liquid! The result is, of course, failure. 111 Why does it fail? Think about it, at the same time open the compressed code to see, only to find the reason: the After compression, the require variable becomes a variable. Seajs is to judge the dependencies between modules by require literal, so require variables cannot be simplified. 113  the Well, Seajs has come up with this problem for us, so we're going to use SEAJS as a way to combine compression (and you can, of course, compress it yourself otherwise).  thePrior to 2.0, SEAJS was using SPM as a compression merge tool, up to 2.0, instead of GRUNT.JS,SPM into a package management tool, like NPM (Don't know npm? Google a bit) the 117 Automation is not only the convenience of science and technology, but also the grunt to the front of the Swiss Army knife. Using grunt, you can easily customize various tasks, such as compression, merging and so on. Before using grunt, you need to install the node environment and grunt tools, Google, 10 minutes back. 118 ...119  - Grunt The core of the two parts, Package.json, Gruntfile.js. 121 122 A. Package.json123Grunt a project/directory as a NPM module, Package.json is the information used to describe the module, including name, version, author, and so on.124 here, grunt that since the directory is considered a module, the module can of course rely on other modules.  the Let's look at this example:126 just as I stretched out, planning to go to the toilet, suddenly think of one thing: if the module more and more, so many files are to be loaded separately? Wouldn't that seriously affect performance!? (What, you don't know why?) )127 to compress the merged JavaScript! So I choked the liquid, started compressing it with Yuicompressor, and manually merged two files.  - here will not show the results, because it is very painful, completely sorry I just suppressed the courage of the liquid! The result is, of course, failure. 129 Why does it fail? Think about it, at the same time open the compressed code to see, only to find the reason: the After compression, the require variable becomes a variable. Seajs is to judge the dependencies between modules by require literal, so require variables cannot be simplified. 131  the Well, Seajs has come up with this problem for us, so we're going to use SEAJS as a way to combine compression (and you can, of course, compress it yourself otherwise). 133Prior to 2.0, SEAJS was using SPM as a compression merge tool, up to 2.0, instead of GRUNT.JS,SPM into a package management tool, like NPM (Don't know npm? Google a bit)134 135 Automation is not only the convenience of science and technology, but also the grunt to the front of the Swiss Army knife. Using grunt, you can easily customize various tasks, such as compression, merging and so on. Before using grunt, you need to install the node environment and grunt tools, Google, 10 minutes back. 136 ...137 138 Grunt The core of the two parts, Package.json, Gruntfile.js. 139  $ A. Package.json141Grunt a project/directory as a NPM module, Package.json is the information used to describe the module, including name, version, author, and so on.142 here, grunt that since the directory is considered a module, the module can of course rely on other modules. 143 Let's look at this example:144</div>145<div class= "Target" >146<div class= "Bar" ></div>147</div>148</div>149</div> Max</body>151View Code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.