jquery.pinBox是一款可将任何元素固定在容器中的jQuery插件。通过该jQuery插件,你可以将元素固定在容器的某个位置,使它在页面滚动时跟随一起滚动。
该插件具有响应式的特点,你可以设定一个最小的视口宽度值,当屏幕小于这个宽度时,元素就不会再位置固定(例如可以在手机和平板上将元素占据满整列)。
使用方法
使用jquery.pinBox插件需要在页面中引入jquery1.7+和jquery.pinBox.min.js文件。
<!-- jQuery --> <script src="jquery-1.9.1.min.js"></script> <!-- Include pinBox plugin --> <script src="js/jquery.pinBox.min.js"></script>
HTML结构
你不需要进行任何特殊的设置,只需要将你需要滚动固定的元素放置在一个容器中,例如下面的代码中的#pinBoxContainer,通过为固定元素添加一个class,例如.pinBox。
<!-- container with id -->
<div class="container" id="pinBoxContainer">
<!--如果你不使用bootstrap.css,并且div的父元素是浮动(float)的,就要确保为其添加一个[position:relative]属性-->
<div class="col-sm-4">
<!-- box you want to pin inside [id="pinBoxContainer"] have class or id -->
<div class="pinBox">
<h2 class="headColor">Example box</h2>
<p>some text</p>
</div>
</div>
<div class="col-sm-8">
<h2 class="headColor">Example box</h2>
<p>some text</p>
</div>
</div>
初始化插件
在页面初始化完毕之后,可以通过下面的方法来初始化该插件。
$(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:回调事件。
jquery.pinBox官方网站地址为:http://nezamy.com/pinBox/






![MBTI 16型人格职业性格测试源码PC+H5自适应完整版基于ThinkPHP框架[亲测可用]](https://www.icbot.net/upload/portal/20250429/32ccad646f32a5f4926b3b7b5ef3232a.png)















