admin管理员组文章数量:1418022
I am using bootstrap tabs functionality and I would like to implement the following features:
- Use mouseenter instead of click to toggle between tabs
- To prevent clicking action on links.
Here is my jsfiddle sample code. /
$('.nav li a').hover(function (e) {
e.preventDefault()
$(this).tab('show');
});
$('.nav li a').click(function (e) {
return true;
});
How this could be achieved?
I need to be able click on mainmenu items (Home, Profile, Messages, Settings) and go to the specific url.
I am using bootstrap tabs functionality and I would like to implement the following features:
- Use mouseenter instead of click to toggle between tabs
- To prevent clicking action on links.
Here is my jsfiddle sample code. https://jsfiddle/irider89/bmLpwtqb/3/
$('.nav li a').hover(function (e) {
e.preventDefault()
$(this).tab('show');
});
$('.nav li a').click(function (e) {
return true;
});
How this could be achieved?
I need to be able click on mainmenu items (Home, Profile, Messages, Settings) and go to the specific url.
Share Improve this question edited Jun 10, 2015 at 10:29 robuh asked Jun 10, 2015 at 10:13 robuhrobuh 611 silver badge9 bronze badges 3- What is the issue/question? – Stian Commented Jun 10, 2015 at 10:17
- Your JSFiddle seems to work in a proper way, what do you want to achieve? – SzybkiSasza Commented Jun 10, 2015 at 10:25
- I need to be able click on mainmenu items (Home, Profile, Messages, Settings) and go to the specific url. – robuh Commented Jun 10, 2015 at 10:28
6 Answers
Reset to default 4Don't use window.location.href = ...
method! Middle mouse click will not work.
1) Toggle on hover. Bind a simple mouseenter handler on the document object:
$(document).on('mouseenter', '[data-toggle="tab"]', function () {
$(this).tab('show');
});
2) Prevent clicking. It's better to use .off('click', ...)
method to reset Bootstrap built-in handler instead of writing custom click handler dealing with custom data attribute. Like so:
$(document).off('click.bs.tab.data-api', '[data-toggle="tab"]');
Then use native href
to specify external link and data-target
to point to the content pane.
Also there is a small Bootstrap plugin which does all of that automatically: https://github./tonystar/bootstrap-hover-tabs.
Demo
Use some new "data-" attribute to mention the url, like this below
<a href="#home" aria-controls="home" role="tab" data-toggle="tab" data-href="www.google.">Home</a>
and use the script like this
$('.nav li a').on("click",function (e) {
window.location.href = $(this).attr("data-href");
});
Use this to go the url:
$('.nav li a').click(function (e) {
window.location.href = $(this).attr("href");
});
You can simply use this code to implement that feature no 1, changing tabs on hover
$('.nav-tabs li').hover(function(){
$('.nav-tabs li a').removeClass('active show');
$(this).children('a').addClass('active show');
var href = $(this).children('a').attr('href');
href = href.substr(1);
$('.tab-pane').removeClass('active show');
$('#'+href).addClass('active show');
});
for next feature add this code to disable click
$('.nav-tabs li').click( function(e){
e.prevenetDefault();
}
Try this:
//first, we need to show tab-content on mouseover
$(".nav-link").mouseover( function() {
$(this).tab("show");
});
// on hovering quickly, need to remove the active class from the related tab panel
$(".nav-link").on("show.bs.tab", function(e) {
const tabPanelId = e.relatedTarget.getAttribute("href");
$(tabPanelId).removeClass("active");
});
The above answers did not work pletely. The TonyStar demo quickly breaks when the user hovers semi quickly between tabs. The code has been updated to add a delay to avoid that issue. Add a data-url="" attribute any tab you'd like to link to to website when clicked.
<script>
(function($) {
$(function() {
$(document).off('click.bs.tab.data-api', '[data-hover="tab"]');
$(document).on('mouseenter.bs.tab.data-api', '[data-toggle="tab"], [data-hover="tab"]', function() {
var $this = $(this);
if (!$this.hasClass('active') && !$this.hasClass('disabled')) {
var delay = setTimeout(function() {
$this.tab('show');
}, 150);
$this.data('tab-delay', delay);
}
}).on('mouseleave.bs.tab.data-api', '[data-toggle="tab"], [data-hover="tab"]', function() {
var $this = $(this);
clearTimeout($this.data('tab-delay'));
}).on('click', '[data-hover="tab"]', function() {
var $this = $(this);
var url = $this.data('url');
if (url) {
window.location.href = url;
}
});
});
})(jQuery);
</script>
本文标签: javascriptBootstrap tabs on hover with clickable linksStack Overflow
版权声明:本文标题:javascript - Bootstrap tabs on hover with clickable links - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745283208a2651548.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论