Javascript DOM onwheel Event via addEventListener() method

Introduction

In JavaScript, using the addEventListener() method:

object.addEventListener("wheel",
       myScript);

This example uses the addEventListener() method to attach a "wheel" event to a DIV element.

A function is triggered when you roll the mouse wheel over div.

The function sets the font-size of div to 35 pixels.

View in separate window

<!DOCTYPE html>
<html>
<head>
<style>
#myDIV {/*from  ww w  .  j  a v a 2s.  c  o m*/
  border: 1px solid black;
}
</style>
</head>
<body>

<div id="myDIV">

Roll the mouse wheel over me - either up or down!</div>
<script>
document.getElementById("myDIV").addEventListener("wheel", myFunction);

function myFunction() {
  this.style.fontSize = "35px";
}
</script>

</body>
</html>
Bubbles: Yes
Cancelable: Yes
Event type: WheelEvent
Supported HTML tags: All HTML elements



PreviousNext

Related