<body onload="DoSetup();">
<div id="oInfo"></div>
<img id="oImage" src="PL0.jpg" alt="The 'L' Piece">
...which creates an IMG object named oImage that will be the target of mousewheel events and a DIV for debugging and displaying information.
function onMouseWheelSpin(e) {
var nDelta = 0;
if (!e) { // For IE, access the global (window) event object
e = window.event;
}
// cross-bowser handling of eventdata to boil-down delta (+1 or -1)
if ( e.wheelDelta ) { // IE and Opera
nDelta= e.wheelDelta;
if ( window.opera ) { // Opera has the values reversed
nDelta= -nDelta;
}
}
else if (e.detail) { // Mozilla FireFox
nDelta= -e.detail;
}
if (nDelta > 0) {
HandleMouseSpin( 1, e.clientX, e.clientY );
}
if (nDelta < 0) {
HandleMouseSpin( -1, e.clientX, e.clientY );
}
if ( e.preventDefault ) { // Mozilla FireFox
e.preventDefault();
}
e.returnValue = false; // cancel default action
}
[/step]And here's how to set up so that the above code gets called:
//-------- set up to handle wheel events on object: oImage
function DoSetup() {
if (oImage.addEventListener) {
oImage.addEventListener('DOMMouseScroll', onMouseWheelSpin, false);
oImage.addEventListener('mousewheel', onMouseWheelSpin, false); // Chrome
}
else {
oImage.onmousewheel= onMouseWheelSpin;
}
}
Note: You can also set the event handler other ways, such as coding it directly in the HTML:
//----------- change the image from PL0.jpg to PL1, PL2,... PL7... PL0
var gnOrient = 0;
function HandleMouseSpin( nDelta, x,y ) {
gnOrient += nDelta;
if (gnOrient < 0) gnOrient = 7;
if (gnOrient > 7) gnOrient = 0;
oImage.src="PL"+gnOrient+".jpg";
oInfo.innerHTML = "Delta: " + nDelta + "<br>" + oImage.src;
}
[/step]
//----------- increase/decrease the size of the image
var gnOrigWide = -1;
var gnZoomLevel= 100;
function HandleMouseSpin(nDelta, x, y) {
if (gnOrigWide == -1) {
gnOrigWide = oImage.width;
}
gnZoomLevel += nDelta * 10;
if ( gnZoomLevel < 10) gnZoomLevel=10;
var nNewWide = (gnOrigWide * gnZoomLevel) / 100;
oImage.width = nNewWide;
oInfo.innerHTML = "Delta: " + nDelta + "<br>zoomed to: " + gnZoomLevel +"%";
}
[/step]
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (3)
Commented:
Commented:
Open in new window
Commented:
https://www.experts-exchange.com/questions/25150778/Mousewheel-scroll-focus-and-or-className.html