bootstrap scrollspy offset高度不适用于firefox

时间:2014-12-26 22:06:41

标签: javascript jquery twitter-bootstrap

我在我的bootstrap主题中使用此脚本来实现scrollspy偏移高度。这段代码在firefox中无法正常工作,并且正确的偏移操作,这适用于chrome。

var offsetHeight = 120;
    $("#p-menu a[href^='#']").on('click', function(e) {

   // prevent default anchor click behavior
   e.preventDefault();

   // store hash
   var hash = this.hash;

   // animate
   $('html, body').animate({
       scrollTop: $(this.hash).offset().top-offsetHeight}, 300, function(){

       // when done, add hash to url
       // (default click behaviour)
       window.location.hash = hash;
     });

    });

HTML:

<ul class="nav nav-tabs bg" role="tablist" id="p-menu">
      <div class="container">
        <a href="#info" data-scroll="#info" class="product-scrollspy active"><i id="info-icon"></i><span>Informations</span></a>
        <a href="#details" data-scroll="#details" class="product-scrollspy "><i id="details-icon"></i><span>Specifications</span></a>
        <a href="#comments" data-scroll="#comments" class="product-scrollspy "><i id="comments-icon"></i><span>Comments</span></a>
        <a href="#diagram" data-scroll="#diagram" class="product-scrollspy "><i id="diagram-icon"></i><span>Diagram</span></a>
      </div>
    </ul>

    <div class="row bg" id="info" data-spy="scroll" data-target=".navbar-example">
        <div class="p_info" id="details">
          ...
        </div>

        <div class="p_info" id="comments">
          ...
        </div>
        <div class="p_info" id="diagram">
          ...
        </div>       
    </div>

1 个答案:

答案 0 :(得分:1)

我有同样的问题并用这个

解决了
random_score

而不是

history.pushState(null, null, myHash);