


Sample code sharing to implement switching delay when the js mouse passes through the tab tab
Mar 25, 2017 pm 02:43 PMThis article mainly introduces in detailjsThe switching delay effect is achieved when the mouse passes through the tab tab. It has a certain reference value. Interested friends can refer to it
I accidentally discovered this effect while browsing the web. When the mouse slides over the tab inadvertently, it will not switch. It will switch when the mouse stays on it for a while.
Personally, I think the user experience is good. The advantages are: 1. When the user just slides over the tab, there is no need to switch. At this time, if switching tabs requires requesting data, unnecessary asynchronous requests will be avoided; 2. Avoid The page switches and jumps when the user does not need it, affecting the user experience.
I checked several methods online and found that the following method is more concise and effective. Organized for future reference.
The key point is the js code: the principle is to set the timer when passing through hover, delay the execution of the switching method, and clear the timer when leaving. When the hover time is less than the delay time, the timer will be cleared and the switching method will not be executed. Switching will only occur when the dwell time is greater than the delay time. This can effectively avoid sliding the tab to trigger the switch event .
Copy code
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script type="text/javascript" src="js/jquery.1.11.3.min.js"></script> <script> $(function() { var t_li = $(".tab") var c_li = $(".tab-content p") t_li.hover(function() { var i = t_li.index($(this)); function way() { t_li.removeClass("cur").eq(i).addClass("cur"); c_li.hide().eq(i).show(); } timer = setTimeout(way, 500); }, function() { clearTimeout(timer); }); }); </script> <style> .head { width: 300px; height: 50px; border: 1px dashed #ccc; } .tab { width: 50%; float: left; line-height: 50px; cursor: pointer; } .cur { border-bottom: 2px solid red; } </style> </head> <body> <p style="width: 300px;margin-left: 300px;" class="main"> <p class="head"> <p class="tab cur">tab1</p> <p class="tab">tab2</p> </p> <p class="tab-content"> <p>tab1的内容<br>tab1的内容<br>tab1的内容<br></p> <p style="display: none;">tab2的内容<br>tab2的内容<br>tab2的内容<br></p> </p> </p> </body> </html>
The above is the detailed content of Sample code sharing to implement switching delay when the js mouse passes through the tab tab. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

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

How to use JS and Baidu Maps to implement map pan function

Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS

Recommended: Excellent JS open source face detection and recognition project

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts

How to create a stock candlestick chart using PHP and JS

How to use JS and Baidu Maps to implement map polygon drawing function

How to use JS and Baidu Maps to implement map heat map function

How to use JS and Baidu Map to implement map click event processing function
