This 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!