Code:
(function ($) { "Use Strict"; $.fn.pin=function(options) {varscrolly = 0, elements = [], disabled =false, $window =$ (window); Options= Options | | {}; varRecalculatelimits =function () { for(vari = 0, len = elements.length; i < Len; i++) { var$ This=Elements[i]; if(Options.minwidth && $window. Width () <=options.minwidth) {if($ This. Parent (). is (". Pin-wrapper")) {$ This. Unwrap ();} $ This. css ({width: "", Left: "", Top: "", Position: "") }); if(Options.activeclass) {$ This. Removeclass (Options.activeclass);} Disabled=true; Continue; } Else{disabled=false; } var$container = Options.containerselector? $ This. Closest (Options.containerselector): $ (document.body); varOffset = $ This. offset (); varContaineroffset =$container. offset (); varParentoffset = $ This. OffsetParent (). offset (); if(!$ This. Parent (). is (". Pin-wrapper")) { $ This. Wrap ("<div class= ' Pin-wrapper ' >"); } varPad =$.extend ({top:0, bottom:0}, Options.padding|| {}); $ This. Data ("Pin", {pad:pad, from: (Options.containerselector? ContainerOffset.top:offset.top)-Pad.top, To:containerOffset.top+ $container. Height ()-$ This. Outerheight ()-Pad.bottom, End:containerOffset.top+$container. Height (), parentTop:parentOffset.top}); $ This. css ({width: $ This. Outerwidth ()}); $ This. Parent (). CSS ("height", $ This. Outerheight ()); } }; varOnscroll =function () { if(disabled) {return; } Scrolly=$window. scrolltop (); varElmts = []; for(vari = 0, len = elements.length; i < Len; i++) { var$ This=$ (elements[i]), data= $ This. Data ("Pin"); if(!data) {//removed element Continue; } elmts.push ($ This); varFrom = Data.from-Data.pad.bottom, to= Data.To-Data.pad.top; if(From + $ This. Outerheight () >data.end) {$ This. css (' position ', ' '); Continue; } if(From < scrolly && to >scrolly) { ! ($ This. css ("position") = = "fixed") && $ This. css ({left: $ This. Offset (). Left, top:data.pad.top}). CSS ("Position", "fixed"); if(Options.activeclass) {$ This. addclass (Options.activeclass);} } Else if(Scrolly >=To ) { $ This. css ({left:"", Top:to-Data.parenttop +data.pad.top}). CSS ("Position", "absolute"); if(Options.activeclass) {$ This. addclass (Options.activeclass);} } Else { $ This. css ({position: "", Top: "", Left: "") }); if(Options.activeclass) {$ This. Removeclass (Options.activeclass);} }} elements=Elmts; }; varUpdate =function() {recalculatelimits (); Onscroll ();}; This. each (function () { var$ This= $( This), Data= $( This). Data (' Pin ') | | {}; if(Data && data.update) {return; } Elements.push ($ This); $("IMG", This). One ("Load", recalculatelimits); Data.update=Update; $( This). Data (' PIN ', data); }); $window. Scroll (onscroll); $window. Resize (function() {recalculatelimits ();}); Recalculatelimits (); $window. Load (update); return This; };}) (jQuery);
Call Method:
$ ("#fixdiv"). Pin ();
Note If the page has dynamically generated HTML, call the pin after HTML generation ()
jquery Locator plug-ins, fixed elements in a page position, not scrolling with scroll bars