bootstrap icon indicating copy to clipboard operation
bootstrap copied to clipboard

Dropdown and popover position based on offset

Open paglias opened this issue 14 years ago • 26 comments

This is not a priority but it would be useful to Open dropdowns and popovers based on current position: for example if the popover toggle is positioned at the top of the page open the popover with position: bottom so that popovers and dropdiwns will be always visible.

I thought about jquery .offset()

paglias avatar Jan 30 '12 00:01 paglias

My nav bar is positioned at the bottom instead of the top, and the dropdowns are all off-screen.

+1 for this.

Edit: found a bottom-up class in the css files that solves my problem. Looks like there's no documentation for this yet.

ghost avatar Jan 31 '12 05:01 ghost

This is a bit of an issue. You can see in this screenshot for any popover element close to the right-side of page, that the popover goes off-page, lost in some dark corners of computer screens, far, far away (from sight).

aanomm avatar Feb 01 '12 13:02 aanomm

I have a similar issue with a dropdown near the end of the screen on the right. It opens and you can only see a little bit of it before it fades out of view. It would be nice if it could guess whether to open on the bottom-right, bottom-left, top-right, or top-left.

MichaelCastelbuono avatar Feb 09 '12 15:02 MichaelCastelbuono

+1

suigintou avatar Feb 09 '12 16:02 suigintou

+1

mccombs avatar Feb 21 '12 19:02 mccombs

+1

arhea avatar Feb 23 '12 18:02 arhea

+1 - this is especially a problem on my Droids (dont have an iPhone)... but with the narrow strip of content, it seems to almost always be appearing outside the viewport.

skattabrain avatar Feb 27 '12 14:02 skattabrain

You just need to provide a custom position function. There's one floating around in the github issues if you search a bit, or you could try the mailing list :)

fat avatar Mar 06 '12 07:03 fat

This fixes it if you do an inline style adjustment:

<ul class="dropdown-menu" style="right: 0; left: auto;">

coderberry avatar Mar 09 '12 17:03 coderberry

A custom position function and inline styles are messy. Wan't offset included in v1 of bootstrap? I don't think it's an unreasonable request.

mccombs avatar Mar 09 '12 17:03 mccombs

I do need offset to achieve corner positionning (cf. https://github.com/twitter/bootstrap/issues/2915).

Please add it or allow use to set (or return via a lambda) an object {top, left} instead of only strings so we can position stuff more precisely.

mgcrea avatar Apr 02 '12 18:04 mgcrea

The existing custom position function fat talked about is here : https://github.com/twitter/bootstrap/issues/345 I didn't manage to make it work.

tkrotoff avatar Apr 18 '12 13:04 tkrotoff

The dropdown function doesn't seem to support a position function, so what would be the preferred way to handle positioning for a dropdown?

Cybolic avatar Feb 04 '13 13:02 Cybolic

If your menu is on the right then use dropdown-menu pull-right this specifies the inline style suggested by cavneb.

engineersmnky avatar Feb 06 '13 20:02 engineersmnky

What about menus that are triggered from in-page content, meaning that the user can have scrolled to a point where the menu is not visible when clicked?

Cybolic avatar Feb 06 '13 20:02 Cybolic

Bootstrap menus close when they lose focus so if a user scrolls to a point where the menu is not visible and interacts with any other object the menu will close. I don't know if this is what you were asking.

engineersmnky avatar Feb 06 '13 20:02 engineersmnky

No, I mean something like a button on a page, that when the page is scrolled down will be at the top of the screen and would therefore naturally have the menu display below it, but when the page is scrolled up, the button would be at the bottom of the screen and the menu would then open outside of the viewport since its position is static. How would one deal with a situation like this?

Cybolic avatar Feb 06 '13 21:02 Cybolic

You may have to build a custom javascript to handle this situation to toggle class .bottom-up which supposedly pushes the menu up instead of down. I have not tested this.

engineersmnky avatar Feb 07 '13 18:02 engineersmnky

The function @fat was talking about is this

$("a[rel=popover]").popover({
  placement: function(tip, element) {
    var $element, above, actualHeight, actualWidth, below, boundBottom, boundLeft, boundRight, boundTop, elementAbove, elementBelow, elementLeft, elementRight, isWithinBounds, left, pos, right;
    isWithinBounds = function(elementPosition) {
      return boundTop < elementPosition.top && boundLeft < elementPosition.left && boundRight > (elementPosition.left + actualWidth) && boundBottom > (elementPosition.top + actualHeight);
    };
    $element = $(element);
    pos = $.extend({}, $element.offset(), {
      width: element.offsetWidth,
      height: element.offsetHeight
    });
    actualWidth = 283;
    actualHeight = 117;
    boundTop = $(document).scrollTop();
    boundLeft = $(document).scrollLeft();
    boundRight = boundLeft + $(window).width();
    boundBottom = boundTop + $(window).height();
    elementAbove = {
      top: pos.top - actualHeight,
      left: pos.left + pos.width / 2 - actualWidth / 2
    };
    elementBelow = {
      top: pos.top + pos.height,
      left: pos.left + pos.width / 2 - actualWidth / 2
    };
    elementLeft = {
      top: pos.top + pos.height / 2 - actualHeight / 2,
      left: pos.left - actualWidth
    };
    elementRight = {
      top: pos.top + pos.height / 2 - actualHeight / 2,
      left: pos.left + pos.width
    };
    above = isWithinBounds(elementAbove);
    below = isWithinBounds(elementBelow);
    left = isWithinBounds(elementLeft);
    right = isWithinBounds(elementRight);
    if (above) {
      return "top";
    } else {
      if (below) {
        return "bottom";
      } else {
        if (left) {
          return "left";
        } else {
          if (right) {
            return "right";
          } else {
            return "right";
          }
        }
      }
    }
  }
});

However, I'd recommend not using rel, just select a class like .bs-popover and note that the size of the popover is set. Adjust it as you need and keep in mind this doesn't solve offset issues you may have.

archonic avatar Feb 28 '13 20:02 archonic

@archonic, I found that function as well, but since dropdown doesn't support a position function, it's of no use for that widget. Haven't tested it for popover, where I assume it works as desired.

Cybolic avatar Mar 01 '13 07:03 Cybolic