


Using HTML5 to implement the function of zooming in and out of pictures using mouse wheel events_html5 tutorial skills
You and I both know that adding mouse wheel events to HTML5 web pages can better allow users to interact with the web page. In HTML5, the mouse wheel can not only slide the web page up and down. In fact, you can also rely on it to complete more functions, such as zooming in and out of the plane of view.
Look at the actual demonstration effect
Most browsers support mouse wheel events, so you can subscribe to the mouse wheel event method first. Whenever the event is triggered, you can get An attribute named wheelDelta, which represents the size of the mouse wheel just changed, where a positive value means the wheel slides down, and a negative value means the wheel slides up. The larger the absolute value of the value, the larger the sliding range.
But unfortunately there is still a browser that does not support mouse wheel events. That's FireFox. Mozilla has implemented an event processing called "DOMMouseScroll", which will pass an event parameter named event with a detail attribute. However, this detail attribute is different from wheelDelta, it can only return positive values. That is, it can only maintain the value of the mouse wheel scrolling down.
You should pay special attention to the fact that Apple has also disabled mouse scrolling to control page sliding up and down in the Safari browser, but this function is still used normally in the webkit engine, so the code you write will not trigger any problems. .
Add mouse wheel event handling method
First we add an image to the web page, and later we can use the mouse wheel to control the zoom of the image
- <img id="myimage" src="myimage.jpg" alt="my image" />
Now add the mouse wheel event handling code
- var myimage = document.getElementById("myimage");
- if (myimage.addEventListener) {
- // IE9, Chrome, Safari, Opera
- myimage.addEventListener("mousewheel", MouseWheelHandler, false);
- // Firefox
- myimage.addEventListener("DOMMouseScroll", MouseWheelHandler, false);
- }
- // IE 6/7/8
- else myimage.attachEvent("onmousewheel", MouseWheelHandler);
In order to support different browsers
In the following case, we will invert the Firefox detail value and return one of 1 or -1
- function MouseWheelHandler(e) {
- // cross-browser wheel delta
- var e = window.event || e; // old IE support
- var delta = Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail)));
Now we directly determine the size range of the image. The following code sets the width range of the image between 50-800 pixels
- myimage.style.width = Math.max(50, Math.min(800 , myimage.width (30 * delta))) "px";
- return false;
- }
Last point, we return false in the method to terminate the standard mouse wheel event processing to prevent it from sliding the web page up and down.
View actual demonstration

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.
