Home  >  Article  >  Web Front-end  >  JavaScript implements mouse wheel control page image switching

JavaScript implements mouse wheel control page image switching

小云云
小云云Original
2018-01-29 10:27:121957browse

The scroll wheel on the mouse is a good thing. Why do I say that? Because it can help us browse the web quickly and read long articles quickly. For us on the web front-end, how can we not pay attention to this mouse wheel? So how can it allow users to browse the web better? This article mainly introduces JavaScript to implement the mouse wheel control page image switching function, involving JavaScript event response and dynamic operation of page elements related implementation techniques. Friends in need can refer to it, I hope it can help everyone.

The most common thing is to switch pictures. You can browse pictures by scrolling the wheel, saving the user the need to click on the next picture and do such tedious steps. Let’s look at a simple example.





鼠标通过滚动滚轮切换图片



  

鼠标通过滚动滚轮切换图片

  

               

Focus on the js code. Different browsers have different mouse wheel events. To put it bluntly, it is a compatibility issue. There are mainly two types, onmousewheel(IE/Opera/Safari/Chrome) and DOMMouseScroll( Firefox), if you want to be compatible with firefox, you should use addEventListener to listen. This function has 3 parameters, addEventListener(type, listener, useCapture), type is click, focus... type, and listener can directly write the method function( ){}, you can also call the written method body, as in my example. useCapture is a Boolean value, with only true and false, indicating the response sequence of the event. If false is selected, the bubbling method is used, and if true is selected, the Capture method is used. There will be a detailed explanation of addEventListener later.

In the MouseWheel method, e.wheelDelta is compatible with other browsers such as IE. Each time the wheel is scrolled, it will return +3/-3 (scroll up/down), while e.detail is compatible with Firefox browser. When the scroll wheel is scrolled once, it will return +120/-120 (scroll up/scroll down). These returned values ​​can be used to determine whether to scroll up or down. The for loop only hides and displays pictures in order. I believe this is not difficult to understand.

We welcome your corrections for errors and omissions in the article.

Related recommendations:

JS control page font size problem_html/css_WEB-ITnose

jQuery study notes Control page implementation code_jquery

js 5 ways to control page jump_javascript skills

The above is the detailed content of JavaScript implements mouse wheel control page image switching. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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