2017-03-03 39 views
1

我在固定寬度容器的頁面上使用固定自舉響應導航,即小設備頁面本身具有水平滾動,但導航採用頁面寬度。固定導航在寬頁面上的自舉彈出

其中一個導航按鈕有一個彈出窗口。問題是:當您將主頁滾動到右側時,彈出窗口顯示不正確或完全消失。

HTML

<!-- Fixed navbar --> 
<nav class="navbar navbar-default navbar-fixed-top" role="navigation"> 
    <div class="container"> 
     <div class="navbar-header"> 
      <a class="navbar-brand" href="#">Project name</a> 
     </div> 
     <div id="navbar" class="navbar-collapse collapse"> 
      <ul class="nav navbar-nav"> 
       <li><a href="#dummy1">Dummy 1</a></li> 
       <li><a href="#dummy2">Dummy 2</a></li> 
       <li> 
        <a href="#contact" data-placement="bottom" data-trigger="click" data-container="body" data-toggle="popover" title="Popover title" data-content="And here's some amazing content. It's very engaging. Right?">Contact Popover</a> 
       </li>    
      </ul> 
     </div> 
     <!--/.nav-collapse --> 
    </div> 
</nav> 
<div id="main" class="container"> 
    <div class="row"> 
     <h1>Dummy 1</h1> 
     <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,</p> 
    </div>  
</div> 

CSS

body { 
    padding-top: 50px; 
    width: 1700px; 
} 

下面是一個例子:https://jsfiddle.net/nnxfk7s6/8/

  • 運行片斷
  • 新聞 「聯繫酥料餅」 看到酥料餅
  • 滾動頁面右側
  • 新聞「聯繫酥料餅」再看看沒有完全呈現酥料餅

我試圖與酥料餅的containerviewport性能發揮,但它並沒有解決問題。 (http://getbootstrap.com/javascript/#popovers-options

+1

我懷疑你可能需要用'onscroll'處理這個問題看看這個答案http://stackoverflow.com/a/22606657 – hammus

+0

@hammus謝謝你的建議,問題是關於垂直位置,但也可以採用水平位置。會試試這個! –

回答

0

所以我結束了一點JavaScript代碼來調整彈出窗口的位置。

  • 我們需要酥料餅的觸發設置爲manual
  • 我們必須設置爲酥料餅本身
  • 我們知道,上述酥料餅的小箭頭位置始終是正確的,我們可以使用方向偏移計算正確widthmin-width酥料餅的位置

下面是代碼:

$(function() { 
    $('[data-toggle="popover"]').popover(); 
    $('body').on("click", '[data-toggle="popover"]', function (e) { 
     e.preventDefault(); 
     if (!$(this).attr('aria-describedby')) { 
      $(this).popover("show"); 
      // aria-describedby attribute keeps id of the popover 
      var $pop = $('#' + $(this).attr('aria-describedby')); 
      if($pop) { 
      // find arrow element within popover to obtain its offset 
      var $arrow = $pop.find('.arrow'); 
      // set new left position for the popover 
      $pop.css('left', $arrow.offset().left - $pop.width()*0.75); 
      // set new arrow position 
      $arrow.css('left', '75%'); 
      } 
     } else { 
      $(this).popover("hide"); 
     } 
    }); 
}); 

這裏是工作示例:https://jsfiddle.net/nnxfk7s6/9/