最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

jQuery scrollFix滾動(dòng)定位插件

 更新時(shí)間:2015年04月01日 14:49:11   投稿:mdxy-dxy  
這篇文章主要介紹了jQuery scrollFix滾動(dòng)定位插件,當(dāng)用戶向上或向下滾動(dòng)頁(yè)面到一定位置時(shí),目標(biāo)元素開(kāi)始固定定位(position:fixed),當(dāng)回滾到原位置時(shí)目標(biāo)元素恢復(fù)到原狀態(tài),需要的朋友可以參考下

當(dāng)用戶向上或向下滾動(dòng)頁(yè)面到一定位置時(shí),目標(biāo)元素開(kāi)始固定定位(position:fixed),當(dāng)回滾到原位置時(shí)目標(biāo)元素恢復(fù)到原狀態(tài),可以定制觸發(fā)滾動(dòng)相對(duì)屏幕位置和觸發(fā)滾動(dòng)方向,兼容IE6

【插件參數(shù)】

$(".target_element").scrollFix( [ "top" | "bottom" | length(可以為負(fù),表示相對(duì)bottom), [ "top" | "bottom" ] ]);

第一個(gè)參數(shù): 可選,默認(rèn)為"top",當(dāng)目標(biāo)元素到了屏幕相對(duì)的位置時(shí)開(kāi)始觸發(fā)固定,可以填一個(gè)數(shù)值,如100,-200 ,負(fù)值表示相對(duì)于屏幕下方

第一個(gè)參數(shù): 可選,默認(rèn)為"top",表示觸發(fā)固定的滾動(dòng)方向,"top"表示從上向下滾動(dòng)時(shí)觸發(fā),"bottom"表示從下向上滾動(dòng)時(shí)觸發(fā)

【下載插件】scrollFix(jb51.net).rar

復(fù)制代碼 代碼如下:

<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="scrollFix.js"></script>
<p><span style="color: #808000;">【代碼示例】</span></p>
<div class="d">
<div class="demo" style="background: #ff6000;">$("#a").scrollFix(-200);
<div>滾動(dòng)到距離下面200px時(shí)開(kāi)始固定,默認(rèn)從上到下觸發(fā)</div>
</div>
&nbsp;</div>
<div class="d">
<div class="demo" style="background: #82BF00;">$("#b").scrollFix(200,"bottom");
<div>滾動(dòng)到距離上面200px時(shí)開(kāi)始固定,指定"bottom"從下到上觸發(fā)</div>
</div>
&nbsp;</div>
<div class="d">
<div class="demo" style="background: #0C9CAE;">$("#c").scrollFix("top","top");
<div>滾動(dòng)到距離上面0時(shí)開(kāi)始固定,指定"top"從上到下觸發(fā)</div>
</div>
&nbsp;</div>
<div class="d">
<div class="demo" style="background: #478FCE;">$("#d").scrollFix("bottom","top");
<div>滾動(dòng)到距離下面0時(shí)開(kāi)始固定,指定"bottom"從下到上觸發(fā)</div>
</div>
</div>

實(shí)現(xiàn)代碼:
復(fù)制代碼 代碼如下:

<script type="text/javascript">// <![CDATA[
window.onload=function(){
  $(".demo:eq(0)").scrollFix(-200);
  $(".demo:eq(1)").scrollFix(200,"bottom");
  $(".demo:eq(2)").scrollFix("top","top");
  $(".demo:eq(3)").scrollFix("bottom","bottom");
}
// ]]></script>

核心代碼:

;(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"; //默認(rèn)滾動(dòng)方向向下
   if (window.XMLHttpRequest) { //非ie6用fixed


    function getHeight() { //>=0表示上面的滾動(dòng)高度大于等于目標(biāo)高度
     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) { //恢復(fù)前只執(zhí)行一次,減少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)) { //結(jié)束
      that[0].style.position = "static";
      p.replaceChild(that[0], r);
      r = null;
      oldHeight = false;
     } else { //滾動(dòng)
      that.css({
       left: l,
       top: height + document.documentElement.scrollTop
      })
     }
    });
   }
  });
 };
})(jQuery);

相關(guān)文章

最新評(píng)論

青岛市| 雷州市| 汝城县| 南皮县| 塔河县| 潮安县| 杨浦区| 自贡市| 股票| 即墨市| 利辛县| 陆丰市| 微博| 萨嘎县| 麦盖提县| 怀仁县| 卓尼县| 合作市| 安化县| 五大连池市| 休宁县| 福清市| 松潘县| 鹤庆县| 盐津县| 塔河县| 永胜县| 扶余县| 介休市| 瓮安县| 涟源市| 泌阳县| 胶州市| 同仁县| 沐川县| 德保县| 镇宁| 江西省| 棋牌| 定日县| 河东区|