JS Capture mouse wheel event code _javascript Tips

Source: Internet
Author: User

Before I saw a senior front-end interview for Renren

Require handwriting code, one of the problems is to do a picture show

Similar to Baidu picture the bottom small thumbnail that display bar

And then ask for a mouse wheel roll to change the size of the requirements

I really don't know how to do this, I looked for information on the Internet

Discovery of events that can capture OnMouseWheel

Then, according to the Event.wheeldelta value of the positive and negative to determine whether to roll forward or roll back

Casually wrote a small example, the way to capture the keyboard keys, not very beautiful no line

Because it's made with textnode, it doesn't add to the HTML code.

Is it possible to prevent XSS injection in this way?

Copy Code code as follows:

<body onkeydown= "Showkey ()" Onmousewheel= "Showkey ()" >

Copy Code code as follows:

function Showkey () {
if (Event.wheeldelta) {

Copy Code code as follows:

Positive 120 for roll forward minus 120 for roll back
var textnode = document.createTextNode (event.wheeldelta+ "");
Document.body.appendChild (Textnode);
Document.body.normalize ();
}
if (Event.keycode)
{
var textnode = document.createTextNode (event.keycode+ "");
Document.body.appendChild (Textnode);
Document.body.normalize ();
}
}

And it's just one of those textnode elements that you just saw today at the advanced design.

Ways to combine multiple Textnode
Copy Code code as follows:

Normalize ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.