Detect if dropdown navigation would go off screen and reposition it

后端 未结 3 1426
谎友^
谎友^ 2020-12-02 10:18

I\'ve got your typical dropdown navigation, and I\'m trying to make sure the drop menu links are always accessible and visible:

  • 相关标签:
    3条回答
    • 2020-12-02 10:25

      I think you were almost there...

      You should really only be interested in the calculations involved in the width. If the width of the dropdown element and the offset of that element is greater than the width of the container, you want to switch your menu.

      $(function () {
          $(".dropdown li").on('mouseenter mouseleave', function (e) {
              if ($('ul', this).length) {
                  var elm = $('ul:first', this);
                  var off = elm.offset();
                  var l = off.left;
                  var w = elm.width();
                  var docH = $(".container").height();
                  var docW = $(".container").width();
      
                  var isEntirelyVisible = (l + w <= docW);
      
                  if (!isEntirelyVisible) {
                      $(this).addClass('edge');
                  } else {
                      $(this).removeClass('edge');
                  }
              }
          });
      });
      

      http://jsfiddle.net/G7qfq/582/

      0 讨论(0)
    • 2020-12-02 10:32

      Here is a function that can be used for menus that fly-out to the right, or down (based off @r0m4n's code):

      function fixFlyout (containerElement, parentElement,flyoutElement,flyoutDirection) {
          $(parentElement).on('mouseenter mouseleave', function (e) {
              var element = $(flyoutElement, this);
              var offset = element .offset();
              switch(flyoutDirection) {
                  case 'down':
                      var top = offset.top;
                      var height = element.height();
                      var windowHeight = $(containerElement).height();
                      var isEntirelyVisible = (top + height <= windowHeight);
                      break;
                  case 'right':
                      var left = offset.left;
                      var width = element.width();
                      var windowWidth = $(containerElement).width();
                      var isEntirelyVisible = (top + width <= windowWidth);
                      break;
              }
              if (!isEntirelyVisible ) {
                  $(element).addClass('edge');
              } else {
                  $(element).removeClass('edge');
              }
          });
      }
      //Level 1 Flyout
      fixFlyout(containerElement = '.header',parentElement = '.header .navigation>.menu>.expanded',flyoutElement = '.menu:first',flyoutDirection = 'down');
      
      0 讨论(0)
    • 2020-12-02 10:45

      I was not able to get addClass('edge') to work correctly but I was able to modify the CSS for the element in question to achieve the behavior. (only slightly modified from r0m4n):

              //Align submenu to right if cut-off from window
              $(function () {
                  $(".dropdown").on('mouseenter mouseleave', function (e) {
                      if ($('.dropdown-content', this).length) {
                          var dropdownElement = $('.dropdown-content:first', this);
                          var elementOffset = dropdownElement.offset();
                          var elementOffsetLeft = elementOffset.left;
                          var elementWidth = dropdownElement.width();
                          var pageHeigth = $(".show-on-scroll-wrapper").height();
                          var pageWidth = $(".show-on-scroll-wrapper").width();
                          //if left offset + width of dropdown is bigger than container width then it is cut-off
                          if ((elementOffsetLeft + elementWidth) > pageWidth) {
                              //Align Right
                              $(".dropdown-content").css({ "left":"auto", "right":"0", "margin-right":"-10px;"});
                          } else {
                              //Align Left
                              $(".dropdown-content").css({ "left":"0", "right":"auto", "margin-left": "-10px;" });
                          }
                      }
                  });
              });
      
      0 讨论(0)
    提交回复
    热议问题