Native JS Implementation div follow scroll bar plugin

Source: Internet
Author: User

Js following scrolling effect plugin

Support for defining multiple follow IDs, using CSS fixed attributes, not supporting IE6, and compatible with other mainstream browsers.

Support defines the minimum height of scrolling to the bottom and does not overwrite

When the page size is resize, the plug-in automatically resets the parameter

=======
Js following scrolling effect plugin

1. Support for defining multiple follow IDs, using CSS fixed properties, not supporting IE6, compatible with other mainstream browsers.
2. Support to define the minimum height of scrolling to the bottom, will not overwrite
3. After the page size resize, the plug-in will automatically reset the parameter
=======

How to use

The code is as follows Copy Code

<script type= "Text/javascript" src= "follow.js" ></script>//This JS in the demo has, everyone save into a follow.js and then call

Window.onload = function () {
var followids = [document.getElementById ("Follow"), document.getElementById ("Follow2")];
New Follow ({
Obj:followids,
bottom:150
});
}

Test instance

The code is as follows Copy Code

<! DOCTYPE html>
<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: "";d Isplay: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>
<body>
<div class= "Wrap" >
<div class= "Header" >Header</div>
<div class= "content clear" >
<div class= "Left" >
<div class= "Text" >
How to use:<br/>
1. Load plugin, after page load, window.onload = function () {}<br/>
2. Create an array of objects to follow, such as var followids = [document.getElementById ("Follow"), document.getElementById ("Follow2")];<br/ >
3. Initialization of Follow:new Follow ();<br/>
4. Reference, obj is an object array, bottom is the minimum height of the scroll block from the bottom <br/>
<pre style= "word-break:break-all;word-wrap:break-word;width:600px;" >
&lt;script type= "Text/javascript" src= "Follow.js" &gt;&lt;/script&gt;

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" >
Follow scrolling <br/> Module 1
</div>
<div class= "Aside2" id= "Follow2" >
Follow scrolling <br/> Module 2
</div>
</div>
</div>
<div class= "Footer" >Footer</div>
</div>

<script type= "Text/javascript" >

(function () {var e=function (a) {var b=this;b.objarr=a.obj;b.arrlen=b.objarr.length;b.obj=b.objarr[0]; B.bottomheight=a.bottom;b.mtop=parseint (B.getstyle (b.obj, ' margintop ')) b.posx=b.position (b.obj) [' X '];b.posY= B.position (b.obj) [' Y '];b.bodyheight=document.documentelement.scrollheight;b.sawheight= Document.documentelement.clientheight;b.minusheight=b.bodyheight-b.sawheight-b.bottomheight;b.newwrap=null; B.newwrapheight=null;b.addeventcheck (window, ' scroll ', function () {b.action ()}); B.addeventcheck (window, ' resize ', function () {B.reset ()})};e.prototype={addeventcheck:function (a,b,c) {if (A.addeventlistener) {A.addeventlistener (b) , C,false)}else if (a.attachevent) {a.attachevent (' on ' +b,c)}},position:function (a) {var b=this;return a.offsetParent?{ X: (A.offsetleft+b.position (a.offsetparent) [' X ']), Y: (A.offsettop+b.position (a.offsetparent) [' Y '])}:{x: A.offsetleft,y:a.offsettop}},action:function () {var a=this;var b=document.body.scrolltop+ Document.documentelement.scrolltop;if (b+a.mtop>a.posy) {A.FIXEDFN (); if (a.newWrap&&b+a.bottomheight+a.newwrapheight>a.bodyheight) {var c=b+a.bottomheight+ A.newwrapheight-a.bodyheight;if (a.sawheight>a.newwrapheight) {c+=a.sawheight-a.newwrapheight}else{c=0;if (b) >a.minusheight) {C=b-a.minusheight}};a.setstyle (a.newwrap,{' bottom ': c+ ' px ', ' top ': ' Auto '})}}else{a.showobj ( A.objarr); if (a.newwrap) {a.hideobj (a.newwrap)}}},fixedfn:function () {var a=this;if (a.newwrap) {A.setstyle ( a.newwrap,{' bottom ': ' Auto ', ' top ': ' 0px '}; a.hideobj (A.objarr) a.showobj (a.newwrap)}else{a.newwrap= Document.createelement (' div '); var b=null;for (var c=0;c<a.arrlen;c++) {B=a.objarr[c].clonenode (true); B.setattribute (' id ', '); A.newwrap.appendchild (b)};a.setstyle (a.newwrap,{' position ': ' fixed ', ' left ': a.posx+ ' px ', ' Top ': ' 0px ', ' bottom ': ' Auto '}; a.obj.parentnode.appendchild (A.newwrap); A.newwrapheight=a.newwrap.offsetheight}} , Hideobj:function (a) {var b=this;if (B.isarray (a)) {var c=a.length;for (var d=0;d<c;d++) {a[d].style.display= ' none ' }}else{a.style.display= ' None '}},showobj:function (a) {var b=this; if (B.isarray (a)) {var c=a.length;for (var d=0;d<c;d++) {a[d].style.display= ' block '}}else{a.style.display= ' Block '}},isarray:function (a) {return Object.prototype.toString.call (a) = = ' [Object Array] '},getstyle:function (a,b) {if (A.currentstyle) {return A.currentstyle[b]}else{return Document.defaultView.getComputedStyle (a,null) [b]}}, Setstyle:function (a,b) {for (Var C in b) {a.style[c]=b[c]}},reset:function () {var a=this;a.showobj (A.objarr); a.posX= A.position (a.obj) [' X '];a.posy=a.position (a.obj) [' Y '];a.sawheight=document.documentelement.clientheight;] A.minusheight=a.bodyheight-a.sawheight-a.bottomheight;if (a.newwrap) {A.setstyle (a.newWrap,{' left ': a.posX+ ' px '}) }a.action ()}};window. Follow=e} ());
</script>
<script>
Window.onload = function () {
var followids = [document.getElementById ("Follow"), document.getElementById ("Follow2")];
New Follow ({
Obj:followids,
bottom:150
});
}
</script>
</body>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.