Sticky source code, a small plug-in that slides along the form

Source: Internet
Author: User

Copy codeThe Code is as follows: $. fn. stickyfloat = function (options, lockBottom ){
Var $ obj = this;
Var parentPaddingTop = parseint(incluobj.parent().css ('padding-top '));
Var startOffset = $ obj. parent (). offset (). top;
Var opts = $. extend ({startOffset: startOffset, offsetY: parentPaddingTop, duration: 200, lockBottom: true}, options );

Export obj.css ({position: 'absolute '});

If (opts. lockBottom ){
Var bottomPos = $ obj. parent (). height ()-$ obj. height () + parentPaddingTop;
If (bottomPos <0)
BottomPos = 0;
}

$ (Window). scroll (function (){
$ Obj. stop ();

Var pastStartOffset = $ (document). scrollTop ()> opts. startOffset;
Var objFartherThanTopPos = $ obj. offset (). top> startOffset;
Var objBiggerThanWindow = $ obj. outerHeight () <$ (window). height ();

If (pastStartOffset | objFartherThanTopPos) & objBiggerThanWindow ){
Var newpos = ($ (document). scrollTop ()-startOffset + opts. offsetY );
If (newpos> bottomPos)
Newpos = bottomPos;
If ($ (document). scrollTop () <opts. startOffset)
Newpos = parentPaddingTop;

$ Obj. animate ({top: newpos}, opts. duration );
}
});
};

The method is simple: you just need to introduce the plug-in to the page and then call it with the selector:Copy codeThe Code is as follows: $ ('# menu15'). stickyfloat ({duration: 500 });
$ ('# Menu14'). stickyfloat ({duration: 500 });
$ ('# Menu13'). stickyfloat ({duration: 500 });
$ ('# Menu12'). stickyfloat ({duration: 500 });

The subsequent duration parameter indicates the sliding speed. The larger the sliding speed, the slower the sliding speed.

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.