【插件功能】

当用户向上或向下滚动页面到一定位置时,目标元素开始固定定位(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"从下到上触发
 
  1. ;(function($) {
  2. jQuery.fn.scrollFix = function(height, dir) {
  3. height = height || 0;
  4. height = height == "top" ? 0 : height;
  5. return this.each(function() {
  6. if (height == "bottom") {
  7. height = document.documentElement.clientHeight - this.scrollHeight;
  8. } else if (height < 0) {
  9. height = document.documentElement.clientHeight - this.scrollHeight + height;
  10. }
  11. var that = $(this),
  12. oldHeight = false,
  13. p, r, l = that.offset().left;
  14. dir = dir == "bottom" ? dir : "top"; //默认滚动方向向下
  15. if (window.XMLHttpRequest) { //非ie6用fixed
  16.  
  17. function getHeight() { //>=0表示上面的滚动高度大于等于目标高度
  18. return (document.documentElement.scrollTop || document.body.scrollTop) + height - that.offset().top;
  19. }
  20. $(window).scroll(function() {
  21. if (oldHeight === false) {
  22. if ((getHeight() >= 0 && dir == "top") || (getHeight() <= 0 && dir == "bottom")) {
  23. oldHeight = that.offset().top - height;
  24. that.css({
  25. position: "fixed",
  26. top: height,
  27. left: l
  28. });
  29. }
  30. } else {
  31. if (dir == "top" && (document.documentElement.scrollTop || document.body.scrollTop) < oldHeight) {
  32. that.css({
  33. position: "static"
  34. });
  35. oldHeight = false;
  36. } else if (dir == "bottom" && (document.documentElement.scrollTop || document.body.scrollTop) > oldHeight) {
  37. that.css({
  38. position: "static"
  39. });
  40. oldHeight = false;
  41. }
  42. }
  43. });
  44. } else { //for ie6
  45. $(window).scroll(function() {
  46. if (oldHeight === false) { //恢复前只执行一次,减少reflow
  47. if ((getHeight() >= 0 && dir == "top") || (getHeight() <= 0 && dir == "bottom")) {
  48. oldHeight = that.offset().top - height;
  49. r = document.createElement("span");
  50. p = that[0].parentNode;
  51. p.replaceChild(r, that[0]);
  52. document.body.appendChild(that[0]);
  53. that[0].style.position = "absolute";
  54. }
  55. } else if ((dir == "top" && (document.documentElement.scrollTop || document.body.scrollTop) < oldHeight) || (dir == "bottom" && (document.documentElement.scrollTop || document.body.scrollTop) > oldHeight)) { //结束
  56. that[0].style.position = "static";
  57. p.replaceChild(that[0], r);
  58. r = null;
  59. oldHeight = false;
  60. } else { //滚动
  61. that.css({
  62. left: l,
  63. top: height + document.documentElement.scrollTop
  64. })
  65. }
  66. });
  67. }
  68. });
  69. };
  70. })(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. Object对象具体解释(二)之clone

    clone方法会返回该实例对象的一个副本,通常情况下x.clone() != x || x.clone().getClass() == x.getClass() || x.clone().equals ...

  2. Java异常的捕获与处理

    Java提供了try(尝试).catch(捕捉).finally(最终)这三个关键字来处理异常.在处理各种异常时,需要用到对应的异常类,指的是由程序抛出的对象所属的类. 一.异常处理的使用 由于fin ...

  3. XML基础+Java解析XML +几种解析方式的性能比较

    XML基础+Java解析XML 一:XML基础 XML是什么: 可扩展的标记语言 XML能干什么: 描述数据.存储数据.传输(交换)数据. XML与HTML区别: 目的不一样 XML 被设计用来描述数 ...

  4. java 10000的阶乘

    package test; import java.math.BigInteger; import java.util.ArrayList; import java.util.List; import ...

  5. 国外物联网平台初探(六) ——Electric Imp

    公司背景 Electric Imp成立于2011年,公司设立在美国加利福尼亚州洛斯阿尔托斯和英国剑桥 公司投资者包括:富士康技术集团.PTI创投.Rampart资本.Redpoint创投 定位 Ele ...

  6. 【SCOI 2005】 骑士精神

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1085 [算法] IDA* [代码] #include<bits/stdc++. ...

  7. ansible usually

    链接地址:https://my.oschina.net/kangvcar/blog/1830155

  8. Git Learning Part II - Working locally

    file status life circle basic: modified:   Examples: untracked: unmodified: modified: Git branching ...

  9. ajax获取跨域数据

    1.效果图 2.源码 <%@ page contentType="text/html;charset=UTF-8" language="java" %&g ...

  10. Vue.js 2 vs Vue.js 3的实现 – 云栖社区

    Vue.js 2 vs Vue.js 3的实现 – 云栖社区 vue.js核心团队已经讨论过将在Vue3实现的变化.虽然API不会改变,但是数据响应机制(译者注:对数据改变的监听和通知)发生了变化.这 ...