jquery Locator plug-ins, fixed elements in a page position, not scrolling with scroll bars

Source: Internet
Author: User

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

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.