Use the: hover pseudo class to replace the js hover event, hover

Source: Internet
Author: User

Use the: hover pseudo class to replace the js hover event, hover

To make a level-2 menu, move the mouse to display the sub-menu, move the mouse out of the sub-menu to hide it, or use the hover event of jquery to hide the sub-menu. For example:

$ (". Nav "). hover (function () {$ ("sub-nav "). addClass ("show") ;}, function () {$ ("sub-nav "). removeClass ("show ");});

The first function implements the style of moving the mouse up, and the second function implements the style of moving the mouse away, so a simple drop-down menu function is implemented.

There was no problem with doing so until yesterday: When the element already has a click event to implement this function, the hover event will implement the same function, and the hover will affect the click event, and remove the Click Event function. For example:

Add the current style for the currently clicked navigation, and then add the current event to the current element that you want to move the mouse to. Then, use the hover event, clicking Add current style will be affected by the hover event, and clicking Add style again will not work.

 

: Hover pseudo class

After a long struggle, I consulted my colleagues. My colleagues said there was no need to use the hover event. I solved it with a pseudo-category, so she was suddenly enlightened under her guidance.

. Nav li. current,. nav li: hover {// css code}

Current is the current style, and then the hover and current share a style, a simple conversion of ideas can solve the problem. Similarly, menu display problems:

. Nav: hover. sub-nav {display: block ;}

Conclusion: Sometimes we need to change our thinking and find the simplest way to implement the problem, instead of staying stuck in the problem.

Related Article

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.