Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deactivate hover when in "mobile mode" #146

Open
haurg opened this issue Feb 17, 2016 · 4 comments
Open

Deactivate hover when in "mobile mode" #146

haurg opened this issue Feb 17, 2016 · 4 comments

Comments

@haurg
Copy link

haurg commented Feb 17, 2016

Hi,

I am wondering if there is a possibility to deactivate the hover when the menu is collapsed.

So, when I am in mobile mode, the hover seems to be deactivated as the link only open on click, but as soon as the mouse is moved, then the menu closes.
Is there a way to stop this behavior?

Best,
haurg

@ghost
Copy link

ghost commented Feb 26, 2016

I am using a workaround for this by executing the plugin via jQuery on screens larger than 480px only:

   $(function() {
       var pageWidth = $(window).width();
       if( $(window).width()> 480){
           $('.dropdown-toggle').dropdownHover();
       }
   });

So on everything smaller than this I am using Bootstrap's data-toggle="dropdown" without the plugin. Just make sure data-toggle="dropdown" isn't available for screens larger than 480px. There the plugin comes into play and not Bootstrap's data-toggle.

Maybe this workaround helps a little.

P. S.: That also means that one should put the parent menu item into the dropdown menu's children list as well to have a clickable link which otherwise wouldn't be available on those small screens. (Mobile only.) Hope to have everything explained somehow understandable :-)

@holtkamp
Copy link

holtkamp commented May 4, 2017

@haurg does this work for you? Seems this issue can be closed...

@mattez
Copy link

mattez commented Dec 27, 2017

Hi. ...and how you disable Bootstrap Hover Drop-down after its initialisation? When screen width is again less then 480px? For example when user rotates device?

@MrMartiniMo
Copy link

Here is my solution for this.

jQuery(function($) {
  var MOBILE_BREAKPOINT = 768

  var $window = $(window)
  var $dropdowns = $('[data-hover="dropdown"]')

  var resizeId;
  var initilized = true

  // Debounce resize function.
  $window
    .on('resize.handleDropdowns', function() {
      clearTimeout(resizeId);
      resizeId = setTimeout(resizedEnded, 500);
    })
    .trigger('resize.handleDropdowns')

  function resizedEnded() {
    if ($window.width() <= MOBILE_BREAKPOINT) {
      // Remove dropdown on hover if initialized.
      if (initilized) {
        $dropdowns
          .each(function() {
            var $link = $(this)
            $link.off('mouseenter').off('mouseleave')
            $link.parent().off('mouseenter').off('mouseleave')
          })
          .on('click.handleDropdowns', function(e) {
            e.preventDefault()

            var $link = $(this)
            var $menuItem = $link.closest('.menu-item')
            var $subMenu = $menuItem.find('.dropdown-menu')

            if ($menuItem.hasClass('open')) {
              $menuItem.removeClass('open')
              $link.attr('aria-expanded', false)
            } else {
              $menuItem.addClass('open')
              $link.attr('aria-expanded', true)
            }
          })
        initilized = false
      }
    } else {
      // Add dropdown on hover and remove dropdown on click.
      if (!initilized) {
        $dropdowns
          .off('click.handleDropdowns')
          .dropdownHover()
        initilized = true
      }
    }
  }
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants