How to make with twitter bootstrap tabs on hover instead of clicking?
Asked Answered
S

7

23

I finally got Bootstrap tabs to work. I was wondering if there's a way to change the behaviour so instead of clicking just hovering the cursor would show the hidden content?

Singlephase answered 22/3, 2012 at 1:29 Comment(2)
Also, see: #8878533Spearwort
The question is [answered here][1] using a few lines of css! [1]: #8878533Propose
M
38

In your $(document).ready or elsewhere ...

put something like this:

$('.nav-tabs > li > a').hover(function() {
  $(this).tab('show');
});

You wont have to modify the core bootstrap code.

Additionally you could do this:

$('.nav-tabs > li ').hover(function() {
  if ($(this).hasClass('hoverblock'))
    return;
  else
    $(this).find('a').tab('show');
});

$('.nav-tabs > li').find('a').click(function() {
  $(this).parent()
    .siblings().addClass('hoverblock');
});

Which will prevent hover selection after the first time a user clicks a tab.

Muriate answered 28/3, 2012 at 12:43 Comment(0)
M
14

It's better to bind a handler on the document object so it will work with dynamically generated tabs too:

$(document).on('mouseenter', '[data-toggle="tab"]', function () {
  $(this).tab('show');
});

Also there is a small Bootstrap plugin which automatically activates tabs on hover: https://github.com/tonystar/bootstrap-hover-tabs.

The complete HTML using this plugin is:

body { padding: 2rem; }
.tab-content { margin-top: 1.5rem; }
<link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>

<!-- Nav pills -->
<ul class="nav nav-pills">
  <li class="active"><a href="#tab-1" data-toggle="tab">Tab 1</a></li>
  <li><a href="#tab-2" data-toggle="tab">Tab 2</a></li>
  <li><a href="#tab-3" data-toggle="tab">Tab 3</a></li>
  <li><a href="#tab-4" data-toggle="tab">Tab 4</a></li>
</ul>

<!-- Tab panes -->
<div class="tab-content well">
  <div class="tab-pane active" id="tab-1">Content 1</div>
  <div class="tab-pane" id="tab-2">Content 2</div>
  <div class="tab-pane" id="tab-3">Content 3</div>
  <div class="tab-pane" id="tab-4">Content 4</div>
</div>

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="//cdn.rawgit.com/tonystar/bootstrap-hover-tabs/v3.1.1/bootstrap-hover-tabs.js"></script>
Melamine answered 21/6, 2015 at 21:0 Comment(2)
i guess you need to update your Demo link in your github page.Felder
This is the best solution, but it no longer works for Bootstrap 5. What would be the necessary changes for BS5?Strapless
S
2

JavaScript:

In your $(document).ready or elsewhere ...
put something like this:

$('.nav-tabs[data-toggle="tab-hover"] > li > a').hover( function(){
    $(this).tab('show');
});

HTML:

<ul class="nav nav-tabs" data-toggle="tab-hover">
    ....
</ul>
Siderostat answered 7/8, 2013 at 2:59 Comment(0)
L
1

Modify bootstrap.js (or boostrap-tab.js if you aren't using the full bootstrap.js file)

Where you see:

 /* TAB DATA-API
  * ============ */

  $(function () {
    $('body').on('click.tab.data-api', '[data-toggle="tab"], [data-toggle="pill"]', function (e) {
      e.preventDefault()
      $(this).tab('show')
    })
  })

change the 'click.tab.data-api' to 'hover.tab.data-api'

Note: I tested this with Bootstrap v2.0.2

Langobardic answered 22/3, 2012 at 1:52 Comment(0)
B
0

Right from my production code. Orthogonal, unobtrusive, can "unclick" any user interface. Selector can be modified to match many kinds of clickables.

$(document).on('mouseover','.js-mouseover-to-click',function (event) {
    $(event.target).trigger('click');
});
Bacciform answered 5/6, 2018 at 7:29 Comment(0)
C
0

i hope it is nice solution

(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 () {
      $(this).tab('show');
    });
  });
})(jQuery);
Comparable answered 12/6, 2018 at 6:37 Comment(0)
F
0
var triggerTabList = [].slice.call(document.querySelectorAll('.nav-link'))
triggerTabList.forEach(function (triggerEl) {
  var tabTrigger = new bootstrap.Tab(triggerEl);
  triggerEl.addEventListener('mouseenter', function (event) {
    event.preventDefault();
    tabTrigger.show();
  });
});
Floribunda answered 17/8, 2023 at 8:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.