国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

jQuery scrollFix滾動定位插件

2024-05-06 16:18:05
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了jQuery scrollFix滾動定位插件,當用戶向上或向下滾動頁面到一定位置時,目標元素開始固定定位(position:fixed),當回滾到原位置時目標元素恢復到原狀態,需要的朋友可以參考下

當用戶向上或向下滾動頁面到一定位置時,目標元素開始固定定位(position:fixed),當回滾到原位置時目標元素恢復到原狀態,可以定制觸發滾動相對屏幕位置和觸發滾動方向,兼容IE6

【插件參數】

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

第一個參數: 可選,默認為"top",當目標元素到了屏幕相對的位置時開始觸發固定,可以填一個數值,如100,-200 ,負值表示相對于屏幕下方

第一個參數: 可選,默認為"top",表示觸發固定的滾動方向,"top"表示從上向下滾動時觸發,"bottom"表示從下向上滾動時觸發

 

<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>滾動到距離下面200px時開始固定,默認從上到下觸發</div>
</div>
 </div>
<div class="d">
<div class="demo" style="background: #82BF00;">$("#b").scrollFix(200,"bottom");
<div>滾動到距離上面200px時開始固定,指定"bottom"從下到上觸發</div>
</div>
 </div>
<div class="d">
<div class="demo" style="background: #0C9CAE;">$("#c").scrollFix("top","top");
<div>滾動到距離上面0時開始固定,指定"top"從上到下觸發</div>
</div>
 </div>
<div class="d">
<div class="demo" style="background: #478FCE;">$("#d").scrollFix("bottom","top");
<div>滾動到距離下面0時開始固定,指定"bottom"從下到上觸發</div>
</div>
</div>


實現代碼:

復制代碼代碼如下:

<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>

 

 

核心代碼:

 

  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. var that = $(this), 
  11. oldHeight = false
  12. p, r, l = that.offset().left; 
  13. dir = dir == "bottom" ? dir : "top"//默認滾動方向向下 
  14. if (window.XMLHttpRequest) { //非ie6用fixed 
  15.  
  16.  
  17. function getHeight() { //>=0表示上面的滾動高度大于等于目標高度 
  18. return (document.documentElement.scrollTop || document.body.scrollTop) + height - that.offset().top; 
  19. $(window).scroll(function() { 
  20. if (oldHeight === false) { 
  21. if ((getHeight() >= 0 && dir == "top") || (getHeight() <= 0 && dir == "bottom")) { 
  22. oldHeight = that.offset().top - height; 
  23. that.css({ 
  24. position: "fixed"
  25. top: height, 
  26. left: l 
  27. }); 
  28. else { 
  29. if (dir == "top" && (document.documentElement.scrollTop || document.body.scrollTop) < oldHeight) { 
  30. that.css({ 
  31. position: "static" 
  32. }); 
  33. oldHeight = false
  34. else if (dir == "bottom" && (document.documentElement.scrollTop || document.body.scrollTop) > oldHeight) { 
  35. that.css({ 
  36. position: "static" 
  37. }); 
  38. oldHeight = false
  39. }); 
  40. else { //for ie6 
  41. $(window).scroll(function() { 
  42. if (oldHeight === false) { //恢復前只執行一次,減少reflow 
  43. if ((getHeight() >= 0 && dir == "top") || (getHeight() <= 0 && dir == "bottom")) { 
  44. oldHeight = that.offset().top - height; 
  45. r = document.createElement("span"); 
  46. p = that[0].parentNode; 
  47. p.replaceChild(r, that[0]); 
  48. document.body.appendChild(that[0]); 
  49. that[0].style.position = "absolute"
  50. else if ((dir == "top" && (document.documentElement.scrollTop || document.body.scrollTop) < oldHeight) || (dir == "bottom" && (document.documentElement.scrollTop || document.body.scrollTop) > oldHeight)) { //結束 
  51. that[0].style.position = "static"
  52. p.replaceChild(that[0], r); 
  53. r = null
  54. oldHeight = false
  55. else { //滾動 
  56. that.css({ 
  57. left: l, 
  58. top: height + document.documentElement.scrollTop 
  59. }) 
  60. }); 
  61. }); 
  62. }; 
  63. })(jQuery); 
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 哈密市| 黄浦区| 凌云县| 绵竹市| 通化县| 曲沃县| 同德县| 吴川市| 峨边| 南漳县| 方山县| 和林格尔县| 县级市| 凤台县| 通辽市| 行唐县| 宁阳县| 武平县| 莲花县| 阿拉尔市| 尚义县| 株洲市| 沁水县| 宣威市| 德化县| 凤凰县| 房产| 临潭县| 海盐县| 从化市| 东宁县| 宣化县| 蓬莱市| 弥渡县| 石河子市| 岗巴县| 清丰县| 客服| 寿宁县| 辰溪县| 长汀县|