jquery.pinBox是一款可将任何元素固定在容器中的jQuery插件。通过该jQuery插件,你可以将元素固定在容器的某个位置,使它在页面滚动时跟随一起滚动。
该插件具有响应式的特点,你可以设定一个最小的视口宽度值,当屏幕小于这个宽度时,元素就不会再位置固定(例如可以在手机和平板上将元素占据满整列)。
使用方法
使用jquery.pinBox插件需要在页面中引入jquery1.7+和jquery.pinBox.min.js文件。
HTML结构
你不需要进行任何特殊的设置,只需要将你需要滚动固定的元素放置在一个容器中,例如下面的代码中的#pinBoxContainer,通过为固定元素添加一个class,例如.pinBox。
Example box
some text
Example box
some text
初始化插件
在页面初始化完毕之后,可以通过下面的方法来初始化该插件。
$(document).ready(function() {
$(".pinBox").pinBox({
Top : '50px',
Container : '#pinBoxContainer',
});
});
配置参数
$(document).ready(function() {
$(".pinBox").pinBox({
//default 0px
Top : '50px',
//default '.container'
Container : '#pinBoxContainer',
//default 20
ZIndex : 20,
//default '767px' if you disable pinBox in mobile or tablet
MinWidth : '767px',
//events if scrolled or window resized
Events : function(e){
console.log(e);
// e.current => current scroll top [number]
// e.direction => scroll down or up [up,down]
// e.width => window width [number]
// e.active => if pinBox active [true,false]
// e.disabled => if window width > MinWidth pinBox will disabled [true, false]
}
});
});
Top:元素固定时距离容器顶部的距离,默认为0。
Container:容器的jQuery选择器,默认值为.container。
ZIndex:元素的z-index属性,默认值为20。
MinWidth:视口的最小宽度,当小于这个宽度时元素不再固定。默认值为767像素。
Events:回调事件。