Bootstrap: ScrollSpy scrollspy.js
Giải phóng thời gian, khai phóng năng lực
Ví dụ trong thanh điều hướng
Plugin ScrollSpy sẽ tự động cập nhật vị trí của các mục tiêu điều hướng dựa trên vị trí của thanh cuộn. Hãy thử kéo thanh cuộn trong vùng nội dung bên dưới thanh điều hướng và quan sát sự thay đổi của class được kích hoạt. Các mục con trong menu sổ xuống cũng sẽ được in đậm
@fat
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.
@mdo
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.
one
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.
two
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.
three
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.
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.
Cách sử dụng
Yêu cầu vị trí tương đối
Bất kể bạn sử dụng cách nào, scrollspy luôn yêu cầu position: relative;
được thiết lập trên phần tử mà bạn muốn sử dụng. Thông thường, nó được sử dụng trên <body>
.
Thông qua các thuộc tính dữ liệu
Để dễ dàng thêm hành vi scrollspy vào thanh điều hướng trên cùng của bạn, bạn chỉ cần thêm thuộc tính data-spy="scroll"
vào phần tử mà bạn muôn (thông thường là <body>
). Sau đó thêm thuộc tính data-target
là ID hoặc class của phần tử cha của bất kỳ thành phần .nav
nào trong BootStrap.
body {
position: relative;
}
<body data-spy="scroll" data-target=".navbar-example">
...
<div class="navbar-example">
<ul class="nav nav-tabs" role="tablist">
...
</ul>
</div>
...
</body>
Thông qua JavaScript
Sau khi thêm thuộc tính position: relative;
vào CSS của bạn, hãy gọi scrollspy bằng JavaScript:
$('body').scrollspy({ target: '.navbar-example' })
Yêu cầu ID đích
Các liên kết trên thanh điều hướng luôn phải có id đích tương ứng. Ví dụ, một <a href="#home">home</a>
phải tương ứng với<div id="home"></div>
.
Các phần tử đích Non-:visible
bị bỏ qua
Các phần tử đích mà không :visible
theo jQuery sẽ bị bỏ qua và các thành phần điều hướng tương ứng của chúng sẽ không được in đậm.
Các phương thức
.scrollspy('refresh')
Khi sử dụng scrollspy cùng với việc thêm hoặc xóa các phần tử khỏi DOM, bạn sẽ cần sử dụng đến phương thức làm mới như sau:
$('[data-spy="scroll"]').each(function () {
var $spy = $(this).scrollspy('refresh')
})
Các tùy chọn
Các tùy chọn có thể được truyền vào bằng các thuộc tính dữ liệu hoặc JavaScript. Đối với các thuộc tính dữ liệu, hãy thêm tên của tùy chọn vào data-
, tương tự như data-offset=""
.
Tên | Loại | Mặc định | Mô tả |
---|---|---|---|
offset | number | 10 | Độ lệch so với bên trên tính bằng pixel khi tính toán vị trí của thanh cuộn. |
Các sự kiện
Loại sự kiện | Mô tả |
---|---|
activate.bs.scrollspy | Sự kiện này xảy ra bất cứ khi nào một mục mới thay đổi thành trạng thái kích hoạt bởi scrollspy. |
$('#myScrollspy').on('activate.bs.scrollspy', function () {
// do something…
})
Giải phóng thời gian, khai phóng năng lực