2012-07-10 47 views
5

Dưới đây là mã của tôi, nhưng tôi biết không có gì sai với nó, bởi vì nó hoạt động tốt khi bạn lấy nó ra khỏi tab. Cũng không có Id trùng lặp nào mà tôi nghĩ có thể gây ra sự cố. Dù sao, nếu có ai có bất kỳ ý tưởng hay cách giải quyết nào sẽ tuyệt vời!Bootstrap scrollspy không hoạt động bên trong các tab?

<div id="sampleNavbar" class="navbar navbar-static"> 
    <div class="navbar-inner"> 
     <div class="container" style="width: auto;"> 
      <a class="brand" href="#">Project Name</a> 
      <ul class="nav"> 
       <li><a href="#PA">PA</a></li> 
       <li><a href="#VT">VT</a></li> 
       <li class="dropdown"> 
        <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <b class="caret"></b></a> 
        <ul class="dropdown-menu"> 
         <li><a href="#uno">uno</a></li> 
         <li><a href="#dos">dos</a></li> 
         <li class="divider"></li> 
         <li><a href="#tres">tres</a></li> 
        </ul> 
       </li> 
      </ul> 
     </div> 
    </div> 
</div> 
<div id="scrollspy-example" data-spy="scroll" data-target="#sampleNavbar" data-offset="300" style="height: 200px; overflow-y: scroll;"> 
    <h4 id="PA">PA</h4> 
    <p>Ad leggings keytar, brunch id art party dolor labore. Pitchfork yr enim lo-fi before they sold out qui. Tumblr farm-to-table bicycle rights whatever. Anim keffiyeh carles cardigan. Velit seitan mcsweeney's photo booth 3 wolf moon irure. Cosby sweater lomo jean shorts, williamsburg hoodie minim qui you probably haven't heard of them et cardigan trust fund culpa biodiesel wes anderson aesthetic. Nihil tattooed accusamus, cred irony biodiesel keffiyeh artisan ullamco consequat. 
    </p> 
    <h4 id="VT">VT</h4> 
    <p>Veniam marfa mustache skateboard, adipisicing fugiat velit pitchfork beard. Freegan beard aliqua cupidatat mcsweeney's vero. Cupidatat four loko nisi, ea helvetica nulla carles. Tattooed cosby sweater food truck, mcsweeney's quis non freegan vinyl. Lo-fi wes anderson +1 sartorial. Carles non aesthetic exercitation quis gentrify. Brooklyn adipisicing craft beer vice keytar deserunt. 
    </p> 
    <h4 id="uno">uno</h4> 
    <p>Occaecat commodo aliqua delectus. Fap craft beer deserunt skateboard ea. Lomo bicycle rights adipisicing banh mi, velit ea sunt next level locavore single-origin coffee in magna veniam. High life id vinyl, echo park consequat quis aliquip banh mi pitchfork. Vero VHS est adipisicing. Consectetur nisi DIY minim messenger bag. Cred ex in, sustainable delectus consectetur fanny pack iphone. 
    </p> 
    <h4 id="dos">dos</h4> 
    <p>In incididunt echo park, officia deserunt mcsweeney's proident master cleanse thundercats sapiente veniam. Excepteur VHS elit, proident shoreditch +1 biodiesel laborum craft beer. Single-origin coffee wayfarers irure four loko, cupidatat terry richardson master cleanse. Assumenda you probably haven't heard of them art party fanny pack, tattooed nulla cardigan tempor ad. Proident wolf nesciunt sartorial keffiyeh eu banh mi sustainable. Elit wolf voluptate, lo-fi ea portland before they sold out four loko. Locavore enim nostrud mlkshk brooklyn nesciunt. 
    </p> 
    <h4 id="tres">tres</h4> 
    <p>Ad leggings keytar, brunch id art party dolor labore. Pitchfork yr enim lo-fi before they sold out qui. Tumblr farm-to-table bicycle rights whatever. Anim keffiyeh carles cardigan. Velit seitan mcsweeney's photo booth 3 wolf moon irure. Cosby sweater lomo jean shorts, williamsburg hoodie minim qui you probably haven't heard of them et cardigan trust fund culpa biodiesel wes anderson aesthetic. Nihil tattooed accusamus, cred irony biodiesel keffiyeh artisan ullamco consequat. 
    </p> 
    <p>Keytar twee blog, culpa messenger bag marfa whatever delectus food truck. Sapiente synth id assumenda. Locavore sed helvetica cliche irony, thundercats you probably haven't heard of them consequat hoodie gluten-free lo-fi fap aliquip. Labore elit placeat before they sold out, terry richardson proident brunch nesciunt quis cosby sweater pariatur keffiyeh ut helvetica artisan. Cardigan craft beer seitan readymade velit. VHS chambray laboris tempor veniam. Anim mollit minim commodo ullamco thundercats. 
    </p> 
</div>   

Trả lời

10

Đây không phải là lỗi trong Bootstrap. Điều đang xảy ra là khi trang của bạn tải, tab có các phần tử cuộn được ẩn. Như vậy, khi plugin cuộn tìm cách tìm số $.position() của mỗi số <h4 id="foo"> trong div cuộn của bạn, họ gọi trả lại {top: 0, left: 0}. Đó là lý do tại sao bạn thấy nhấp nháy khi bạn cuộn; plugin cho rằng tất cả các mục tiêu cuộn của bạn đều ở cùng vị trí.

Nếu bạn nhìn vào documentation for the scrollspy plugin, bạn sẽ thấy rằng nếu bạn thêm hoặc xóa các phần tử khỏi DOM, bạn cần gọi hàm .scrollspy('refresh') để plugin có thể đánh giá lại vị trí của các mục tiêu cuộn khác nhau .

Điều đó nói rằng, những gì bạn cần làm là chờ cho đến khi tab có nội dung cuộn được tải và sau đó gọi hàm làm mới, như vậy. Lưu ý rằng tôi cũng đã kết hợp tất cả các bộ xử lý nhấp chuột riêng thành một handler

$(function(){ 
    $('.nav-tabs li a').click(function(e) { 
     e.preventDefault(); 
     $(this).tab('show'); 

     // If we are showing the scrollspy tab, let the 
     // plugin refresh itself so it can function properly 
     if(this.id === 'scrotab') { 
      $(this).on('shown',function(){ 
       $('#scrollspy-example').scrollspy('refresh'); 
      }); 
     } 
    }); 
}); 

Ngoài ra, thay vì sử dụng một thuộc tính data-offset="300" vào yếu tố <div id="scrollspy-example"> của bạn, bạn cần phải thiết lập các position của nguyên tố này để relative với CSS:

#scrollspy-example { 
    position: relative; 
} 

Dưới đây là một Demo jsFiddle rằng hoạt động:

http://jsfiddle.net/GyMYE/3/

+0

Cảm ơn rất nhiều. Điều đó thật tuyệt. Mặc dù nó hài hước vì nó chỉ ra rằng tôi nghĩ bạn phải có lớp mờ dần để nó hoạt động. Cảm ơn! – dezman

1

Thêm:

$('#scrollspy-example').scrollspy('process'); 

sau:

$('#scrollspy-example').scrollspy('refresh'); 

đảm bảo rằng đúng vị trí được cập nhật khi bạn tải tab cụ thể này. Nếu không, phần tử li sai sẽ được đánh dấu bằng scrollspy cho đến khi bạn di chuyển/bắt đầu cuộn, gây ra làm mới. Dòng 'process' thực hiện điều này khi tải tab.

Các vấn đề liên quan