這是一個能讓指定元素隨頁面捲動而固定跟隨的插件,主要特性如下:
1. 支援定義多個跟隨 ID,採用 CSS fixed 屬性,不支援 IE6,但相容其他主流瀏覽器。
2. 支援定義滾動到底部的最小高度,不會覆蓋底部內容。
3. 頁面大小 resize 後,插件會自動重置參數。
代碼如下:
程式碼如下:
<script type="text/javascript" src="follow.js"></script>
window.onload = function(){
var followIds = [document.getElementById("follow"),document.getElementById("follow2")];
new Follow({
obj:followIds,
bottom:150
});
}
程式碼如下:
程式碼如下:
<!DOCTYPE html>
<html lang="zh-CN">
<head profile="https://gmpg.org/xfn/11">
<meta charset="UTF-8">
<title>Js Follow</title>
<style>
html,body{width:100%;margin:0;padding:0;}
div{padding:0;margin:0;text-align:center;font-size:40px;font-weight:bold;color:#fff;}
.clear:after{content:"";display:block;height:0;clear:both;visibility: hidden;overflow:hidden;}
.clear{*zoom:1;}
.wrap{width:1000px;margin:0 auto;}
.header{width:100%;background:#1BA1E2;height:200px;line-height:300px;}
.footer{width:100%;background:#666;height:150px;line-height:100px;}
.content{width:100%;margin:0 auto;background:#aaa;}
.left{width:70%;float:left;background:#8CBF26;}
.text{padding:50px;text-align:left;word-break:break-all;line-height:36px;font-size:16px;font-weight:normal;}
.right{width:30%;float:left;}
.aside{padding:10px 0px;margin-top:50px;background:#F09609;width:300px;height:150px;}
.aside2{padding:10px 0px;background:#DB4F33;}
</style>
</head>
<body>
<div class="wrap">
<div class="header">Header</div>
<div class="content clear">
<div class="left">
<div class="text">
使用方法:<br/>
1.載入插件,在頁面載入之後,window.onload = function(){}<br/>
2.創建需要跟隨的對象的數組,比如 var followIds = [document.getElementById("follow"),document.getElementById("follow2")];<br/>
3.初始化Follow:new Follow();<br/>
4.傳參,obj是對象數組,bottom是滾動塊距離底部的最小的高度<br/>
<hr/>
<pre style="word-break:break-all;word-wrap:break-word;width:600px;">
<script type="text/javascript" src="follow.js"></script>
window.onload = function(){
var followIds = [document.getElementById("follow"),document.getElementById("follow2")];
new Follow({
obj:followIds,
bottom:150
});
}
</pre>
</div>
<br/><br/><br/><br/><br/><br/><br/><br/><br/>
</div>
<div class="right">
<div class="aside" id="follow">
跟隨滾動<br/>模塊1
</div>
<div class="aside2" id="follow2">
跟隨滾動<br/>模塊2
</div>
</div>
</div>
<div class="footer">Footer</div>
</div>
<script type="text/javascript" src="follow.min.js"></script>
<script>
window.onload = function(){
var followIds = [document.getElementById("follow"),document.getElementById("follow2")];
new Follow({
obj:followIds,
bottom:150
});
}
</script>
</body>
</html>
使用時只要在頁面載入後建立跟隨物件陣列,再初始化 Follow 並傳入 obj(物件陣列)與 bottom(距離底部的最小高度)兩個參數即可,非常適合側邊欄廣告或工具列等需要固定跟隨的區塊。