【插件功能】

当用户向上或向下滚动页面到一定位置时,目标元素开始固定定位(position:fixed),当回滚到原位置时目标元素恢复到原状态,可以定制触发滚动相对屏幕位置和触发滚动方向,兼容IE6

【插件参数】

$(".target_element").scrollFix( [ "top" | "bottom" | length(可以为负,表示相对bottom), [ "top" | "bottom" ] ]);

第一个参数: 可选,默认为"top",当目标元素到了屏幕相对的位置时开始触发固定,可以填一个数值,如100,-200 ,负值表示相对于屏幕下方

第一个参数: 可选,默认为"top",表示触发固定的滚动方向,"top"表示从上向下滚动时触发,"bottom"表示从下向上滚动时触发

【下载插件】

下载插件(download)

【代码示例】

$("#a").scrollFix(-200);

滚动到距离下面200px时开始固定,默认从上到下触发
$("#b").scrollFix(200,"bottom");

滚动到距离上面200px时开始固定,指定"bottom"从下到上触发
$("#c").scrollFix("top","top");

滚动到距离上面0时开始固定,指定"top"从上到下触发
$("#d").scrollFix("bottom","top");

滚动到距离下面0时开始固定,指定"bottom"从下到上触发
 
;(function($) {
jQuery.fn.scrollFix = function(height, dir) {
height = height || 0;
height = height == "top" ? 0 : height;
return this.each(function() {
if (height == "bottom") {
height = document.documentElement.clientHeight - this.scrollHeight;
} else if (height < 0) {
height = document.documentElement.clientHeight - this.scrollHeight + height;
}
var that = $(this),
oldHeight = false,
p, r, l = that.offset().left;
dir = dir == "bottom" ? dir : "top"; //默认滚动方向向下
if (window.XMLHttpRequest) { //非ie6用fixed function getHeight() { //>=0表示上面的滚动高度大于等于目标高度
return (document.documentElement.scrollTop || document.body.scrollTop) + height - that.offset().top;
}
$(window).scroll(function() {
if (oldHeight === false) {
if ((getHeight() >= 0 && dir == "top") || (getHeight() <= 0 && dir == "bottom")) {
oldHeight = that.offset().top - height;
that.css({
position: "fixed",
top: height,
left: l
});
}
} else {
if (dir == "top" && (document.documentElement.scrollTop || document.body.scrollTop) < oldHeight) {
that.css({
position: "static"
});
oldHeight = false;
} else if (dir == "bottom" && (document.documentElement.scrollTop || document.body.scrollTop) > oldHeight) {
that.css({
position: "static"
});
oldHeight = false;
}
}
});
} else { //for ie6
$(window).scroll(function() {
if (oldHeight === false) { //恢复前只执行一次,减少reflow
if ((getHeight() >= 0 && dir == "top") || (getHeight() <= 0 && dir == "bottom")) {
oldHeight = that.offset().top - height;
r = document.createElement("span");
p = that[0].parentNode;
p.replaceChild(r, that[0]);
document.body.appendChild(that[0]);
that[0].style.position = "absolute";
}
} else if ((dir == "top" && (document.documentElement.scrollTop || document.body.scrollTop) < oldHeight) || (dir == "bottom" && (document.documentElement.scrollTop || document.body.scrollTop) > oldHeight)) { //结束
that[0].style.position = "static";
p.replaceChild(that[0], r);
r = null;
oldHeight = false;
} else { //滚动
that.css({
left: l,
top: height + document.documentElement.scrollTop
})
}
});
}
});
};
})(jQuery);

原文地址:https://www.cnblogs.com/Hodor/archive/2012/07/25/2607831.html

jQuery scrollFix滚动定位插件的更多相关文章

  1. jQuery.hhNewSilder 滚动图片插件

    /**  * jQuery.hhNewSilder 滚动图片插件  * User: huanhuan  * QQ: 651471385  * Email: th.wanghuan@gmail.com ...

  2. 自己写一个jQuery垂直滚动栏插件(panel)

    html中原生的滚动栏比較难看,所以有些站点,会自己实现滚动栏,导航站点hao123在一个側栏中,就自己定义了垂直滚动栏,效果比較好看,截图例如以下: watermark/2/text/aHR0cDo ...

  3. [JQuery]ScrollMe滚动特效插件

    最近考完试,一切顺利,昨晚闲着无聊把最近要用的一个插件翻译了一下:ScrollMe. (╯‵□′)╯︵┻━┻地址请戳: /* ScrollMe -李明夕翻译(╯‵□′)╯︵┻━┻ */ ScrollM ...

  4. 网站开发常用jQuery插件总结(13)定位插件scrollto

    一.scrollto插件功能 scrollto用于定位页面中元素的位置,并使滚动条滚动到当前元素. 二.scrollto官方地址 https://github.com/flesler/jquery.s ...

  5. jquery 页面滚动到底部自动加载插件集合

    很多社交网站都使用无限滚动的翻页技术来提高用户体验,当你页面滑到列表底部时候无需点击就自动加载更多的内容.下面为你推荐 10 个 jQuery 的无限滚动的插件: 1. jQuery ScrollPa ...

  6. 【精心推荐】12款很好用的 jQuery 图片滚动插件

    这里收集了12款很好用的 jQuery 图片滚动插件分享给大家.jQuery 作为最流行的 JavaScript 框架,使用简单灵活,同时还有许多优秀的插件可供使用.其中最令人印象深刻的应用之一就是各 ...

  7. Flexslider - 响应式的 jQuery 内容滚动插件

    FlexSlider 是一款轻量的响应式 jQuery 内容滚动插件,能够帮助你在项目轻松的创建漂亮的内容滚动效果.这款插件曾经连续多年入选 WDL 的年度最佳 jQuery 插件,值得大家在网站开发 ...

  8. jQuery页面滚动监听事件及高级效果插件

    jQuery页面滚动监听事件及高级效果插件 1. One Page scroll (只适用于上下焦点图)http://www.thepetedesign.com/demos/onepage_scrol ...

  9. 10款很好用的 jQuery 图片滚动插件

    jQuery 作为最流行的 JavaScript 框架,使用简单灵活,同时还有许多优秀的插件可供使用.其中最令人印象深刻的应用之一就是各种很酷的图片效果,它可以让的网站更具吸引力.这里收集了10款很好 ...

随机推荐

  1. Java设计模式菜鸟系列(十五)建造者模式建模与实现

    转载请注明出处:http://blog.csdn.net/lhy_ycu/article/details/39863125 建造者模式(Builder):工厂类模式提供的是创建单个类的模式.而建造者模 ...

  2. 百度2016研发project师笔试题(四)

    百度2016研发project师笔试题(四) 2015/12/8 10:42(网上收集整理的,參考答案在后面.若有错误请大神指出) 1. 关于MapReduce的描写叙述错误的是() A. 一个Tas ...

  3. 用python阐释工作量证明(proof of work)

    了解比特币的都知道挖矿非常耗电,这是由于比特币用到了工作量证明. 工作量证明是指系统为达到某目标而设置的工作度量方法.一開始是用在网络攻防上,大大提高攻击者的计算量,攻击成本也就上去了. 工作量证明须 ...

  4. The Euler function(hdoj --2824-欧拉函数)

    The Euler function Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  5. ClassLoader.getResourceAsStream(name);获取配置文件的方法

    ClassLoader.getResourceAsStream(name);路径问题 InputStream in = getClass().getResourceAsStream('/'+" ...

  6. python 下 excel,csv 文件的读写

    python 可以用利用xlrd 库读取数据excel数据,可以用xlwt写入excel数据,用csv 操作csv文件 xlrd xlwt  python 模块 官方链接  https://pypi. ...

  7. 在ubuntu中安装Markdown神器Typora

    title: 在ubuntu中安装Markdown神器Typora toc: false date: 2018-09-01 17:48:15 categories: methods tags: ubu ...

  8. 如何卸载Win10自带Xbox、人脉、天气等应用?

    Win10中的Modern应用一般可在右键菜单中进行卸载,不过某些自带的Modern应用,比如Xbox.天气.人脉.照片等应用是无法在右键菜单中进行卸载的.如果你有卸载这些应用的需求,可参考下面的方法 ...

  9. Android stroke 边框线 某一边

    有时候需要给View加边框线,我们经常是四边一起加,就像这样: <shape xmlns:android="http://schemas.android.com/apk/res/and ...

  10. Python 之 基础知识(五)

    一.变量 1.引用 id() 函数传参 与 返回值 都是传递保存的数据的引用 2.可变和不可变类型(变量的引用地址只在赋值语句后变化) 不可变类型 内存中的数据不允许被修改 数字类型 int,bool ...