Javascript scroll wheel Event _ javascript skills

Source: Internet
Author: User
The implementation of the mouse wheel event is a little different in IE and FF, so you need to write this event as a unified and available event. Today, I found a function that responds to the scroll wheel on the Internet and changed it to the following class.

The Code is as follows:


Function wheelEvent (obj, handle)
{
This. handle = handle;
// Different events between Firefox and IE
Window. addEventListener? Obj. addEventListener ("DOMMouseScroll", this. wheel, false): (obj. onmousewheel = this. wheel );
}
WheelEvent. prototype. wheel = function (event)
{
Var ev = event | window. event;
Var delta = ev. wheelDelta? (Ev. wheelDelta/120): (-ev. detail/3); // Firefox using 'wheeldelta' IE using 'detail'
Eval ('delta? '+ Parent. handle +' (delta): null ;');
}


When using this function, you need to define an execution function to operate based on the values obtained from the preceding class and add events to the specified webpage element. For example

The Code is as follows:


Function handle (delta)
{
Document. getElementById ('text'). scrollTop-= delta * 20;
}
New wheelEvent (document. getElementById ('text'), 'handle ');

In the preceding example, the first parameter is the webpage element with the scroll event id of p, and the second parameter is the name of the execution function handle.
The handle function must have only one parameter, delta. When the scroll wheel goes up, delta is greater than 0, and down is less than 0. In the preceding example, the handle function is used to implement the scroll bar function for p using the scroll wheel.
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.