Home Web Front-end H5 Tutorial Using HTML5 to implement the function of zooming in and out of pictures using mouse wheel events_html5 tutorial skills

Using HTML5 to implement the function of zooming in and out of pictures using mouse wheel events_html5 tutorial skills

May 16, 2016 pm 03:46 PM
html5 Zoom

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

XML/HTML CodeCopy content to clipboard
  1. <img id="myimage" src="myimage.jpg" alt="my image" />

Now add the mouse wheel event handling code

XML/HTML CodeCopy content to clipboard
  1. var myimage = document.getElementById("myimage");
  2. if (myimage.addEventListener) {
  3. // IE9, Chrome, Safari, Opera
  4. myimage.addEventListener("mousewheel", MouseWheelHandler, false);
  5. // Firefox
  6. myimage.addEventListener("DOMMouseScroll", MouseWheelHandler, false);
  7. }
  8. // IE 6/7/8
  9. 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

XML/HTML CodeCopy content to clipboard
  1. function MouseWheelHandler(e) {
  2. // cross-browser wheel delta
  3. var e = window.event || e; // old IE support
  4. 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

XML/HTML CodeCopy content to clipboard
  1.  myimage.style.width = Math.max(50, Math.min(800 , myimage.width (30 * delta))) "px";
  2. return false;
  3. }

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

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1677
14
PHP Tutorial
1279
29
C# Tutorial
1257
24
Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

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

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

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.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

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

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

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

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

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

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

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

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

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

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

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

See all articles