Simple Example of js mouse pulley rolling event binding

Source: Internet
Author: User

Simple Example of js mouse pulley rolling event binding

This article mainly introduces a simple example of js mouse pulley rolling event binding (compatible with mainstream browsers ). For more information, see.

The Code is as follows:

/** Event handler for mouse wheel event.

* Mouse scroll event

*/

Var wheel = function (event ){

Var delta = 0;

If (! Event)/* For IE .*/

Event = window. event;

If (event. wheelDelta) {/* IE/Opera .*/

Delta = event. wheelDelta/120;

} Else if (event. detail ){

/** Mozilla case .*/

/** In Mozilla, sign of delta is different than in IE.

* Also, delta is multiple of 3.

*/

Delta =-event. detail/3;

}

/** If delta is nonzero, handle it.

* Basically, delta is now positive if wheel was scrolled up,

* And negative, if wheel was scrolled down.

*/

If (delta)

Handle (delta );

/** Prevent default actions caused by mouse wheel.

* That might be uugly, but we handle scrolls somehow

* Anyway, so don't bother here ..

*/

If (event. preventDefault)

Event. preventDefault ();

Event. returnValue = false;

}

 

/** Initialization code.

* If you use your own event management code, change it as required.

*/

If (window. addEventListener ){

/** DOMMouseScroll is for mozilla .*/

Window. addEventListener ('domainscroll', wheel, false );

}

/** IE/Opera .*/

Window. onmousewheel = document. onmousewheel = wheel;

 

/** This is high-level function.

* It must react to delta being more/less than zero.

*/

Var handle = function (delta ){

Var random_num = Math. floor (Math. random () * 100) + 50 );

If (delta <0 ){

// Alert ("scroll down the mouse pulley:" + delta + "times! "); // 1

$ ("Btn_next_pic"). onclick (random_num );

Return;

} Else {

// Alert ("scroll up the mouse wheel:" + delta + "times! "); //-1

$ ("Btn_last_pic"). onclick (random_num );

Return;

}

}

 

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.