admin管理员组文章数量:1394228
I'm trying to create simple jQuery tabs. The tab content should be displayed on hover. So I'm trying following code. The problem is that if you hover fast enough on the tab titles, you can still see the other tabs content before the right tab content is displayed.
Demo:
Please see the Fiddle link to see the problem: /
Code:
jQuery(document).ready(function($) {
$(".tab-titles li").hover(function() {
$(".tab-content").hide();
$(".tab-titles li").removeClass('active');
$(this).addClass("active");
var selected_tab = $(this).find("a").attr("href");
$(selected_tab).fadeIn();
return false;
});
});
I'm trying to create simple jQuery tabs. The tab content should be displayed on hover. So I'm trying following code. The problem is that if you hover fast enough on the tab titles, you can still see the other tabs content before the right tab content is displayed.
Demo:
Please see the Fiddle link to see the problem: http://jsfiddle/0v9nhxu3/
Code:
jQuery(document).ready(function($) {
$(".tab-titles li").hover(function() {
$(".tab-content").hide();
$(".tab-titles li").removeClass('active');
$(this).addClass("active");
var selected_tab = $(this).find("a").attr("href");
$(selected_tab).fadeIn();
return false;
});
});
Share
Improve this question
asked May 5, 2015 at 8:48
user1355300user1355300
4,99718 gold badges50 silver badges73 bronze badges
2
- why downvote? please enlighten me whats wrong with the question? – user1355300 Commented May 5, 2015 at 8:57
- I agree this question is fine +1 – Jack Commented May 5, 2015 at 9:00
3 Answers
Reset to default 3Use stop()
before fadeIn
to stop the current running animation.
jQuery(document).ready(function($) {
$(".tab-titles li").hover(function() {
$(".tab-content").hide();
$(".tab-titles li").removeClass('active');
$(this).addClass("active");
var selected_tab = $(this).find("a").attr("href");
$(selected_tab).stop().fadeIn();
return false;
});
});
Fiddle Demo
jQuery(document).ready(function($) {
$(".tab-titles li").hover(function() {
$(".tab-content").removeClass('tab-show');
$(".tab-titles li").removeClass('active');
$(this).addClass("active");
var selected_tab = $(this).find("a").attr("href");
$(selected_tab).addClass("tab-show");
return false;
});
});
http://jsfiddle/0v9nhxu3/9/
Quick way and customizable
$(function(){
$('.tabs li').hover(function(){
//hover in
var index= $(this).index();
$('.tabcontainer div').eq(index).show(500);
},function(){
//hover out
$('.tabcontainer div').hide(500);
});
})
.tabcontainer{
padding: 20px;
}
.tabcontainer div{
display: none;
padding: 10px;
border: thin solid #ccc;
}
.tabs{
margin: 0;
padding: 0;
}
.tabs li{
list-style: none;
padding: 10px;
display: inline-block;
cursor: pointer;
}
.tabs li:hover{
background-color: #ccc;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="tabcontainer">
<ul class="tabs">
<li>Tab 1</li>
<li>Tab 2</li>
<li>Tab 3</li>
<li>Tab 4</li>
</ul>
<div>Tab 1 content</div>
<div>Tab 2 content</div>
<div>Tab 3 content</div>
<div>Tab 4 content</div>
</div>
本文标签: javascriptjQuery hover tabsStack Overflow
版权声明:本文标题:javascript - jQuery hover tabs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744582610a2614011.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论