JS make list scrolling (with scroll bar)

Source: Internet
Author: User

function MouseWheel (obj, fn) {var ff = navigator.userAgent.indexOf ("Firefox"); if (ff! =-1) {Obj.addeventlistener (" Dommousescroll ", wheel, False);} Else{obj.onmousewheel = wheel;} Function Wheel (event) {var event = Event | | Window.event;var down = true;//determine if scroll down if (event.preventdefault) {//clear default event even T.preventdefault ();} if (event.detail) {//ffdown = Event.detail < 0;} Else{down = event.wheeldelta > 0;} Fn.apply (Obj,[event, down]); return false;}} function Scrollfn () {var wrap = Document.queryselector (". Panelfather"), var middle = document.queryselector (". panelsons var sliderwrap = document.queryselector ("#sliderWrap"), var slider = document.queryselector ("#slider"), var y = 0;// Set the scroll button height function scrollheight () {var scale =wrap.clientheight/880;var height = sliderwrap.offsetheight * scale; if (Heigh T <) {height = 50;} Slider.style.height = height + "px";} ScrollHeight (); var contmaxheight = 880-wrap.clientheight; var slidermaxheight = sliderwrap.offsetheight-slider.clientheight;//ScrollingFunctions function Move () {if (y <= 0) {//limit scroll range y = 0;} else if (y >= slidermaxheight) {y = slidermaxheight;} var movescale = y/slidermaxheight; Slider.style.top = y + "px"; middle.style.top =-Contmaxheight * movescale + "px";} Scroll button Drag Slider.onmousedown = function (event) {var event = Event | | Window.event;var disy = Event.clienty-slider.offsetto P;document.onmousemove = function (event) {var event = Event | | window.event;y = Event.clienty-disy;move ();} Document.onmouseup = function () {document.onmousemove = null;} return false;} Custom scrolling Distance MouseWheel (wrap, function (event, down) {if (down) {y-= 40;} Else{y + = 40;} Move ();}); /keyboard up/down CONTROL scroll Document.onkeydown = function (event) {var event = Event | | Window.event;switch (event.keycode) {case 38:y-= 5; Break;case 40:y + = 5;break;} Move ();} Click the scroll bar to jump to the appropriate location Sliderwrap.onmousedown = function (event) {var event = Event | | window.event;//calculates the distance to move/* mouse vertical coordinate-outermost element spacing = mouse The distance to move the Y value is the distance of the mouse movement minus half the height of the scroll button, so that the center position of the scroll button height is aligned with the mouse coordinate */y = Event.clienty-wrap.offsettop-slidEr.offsetheight/2;move ();}} SCROLLFN ();

Direct code, easy to understand

JS make list scrolling (with scroll bar)

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.