Dynamically change the location of the popover depending upon where it displays

前端 未结 2 590
醉梦人生
醉梦人生 2021-02-01 11:20

I\'d like to dynamically change the popover placement (left/right, top/bottom) depending on where the element is located on the screen.



        
相关标签:
2条回答
  • 2021-02-01 11:29

    I just checked the bootstrap source again and realised that functions passed to the placement property get passed arguments. I managed to achieve a similar thing to what you were attempting, try the following and see if it works for you:

    $('a[data-rel=popover]').popover({
      offset: 10,
      placement: get_popover_placement
    });
    
    function get_popover_placement(pop, dom_el) {
      // your placement function code here
    }
    
    0 讨论(0)
  • 2021-02-01 11:30

    Try this change, using the .on() function in jQuery to attach an event listener:

    Changed this reply by updating Kevin Ansfield's - added code to the placement function.

        $('a[data-rel=popover]').popover({
          placement: get_popover_placement
        });
    
        function get_popover_placement(pop, dom_el) {
          var width = window.innerWidth;
          if (width<500) return 'bottom';
          var left_pos = $(dom_el).offset().left;
          if (width - left_pos > 400) return 'right';
          return 'left';
        }
    
    0 讨论(0)
提交回复
热议问题