forked from yashilanka/makefixed.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakefixed.min.js
6 lines (6 loc) · 2.15 KB
/
makefixed.min.js
1
2
3
4
5
6
/**
* makefixed.js
* @author Guilherme Augusto Madaleno <[email protected]>
* @version 1.0
*/
$.fn.makeFixed=function(options){var sel=$(this).selector;var el;var elc;var elAttr;var defOpts={defTopPos:"0px",defZIndex:1e4};var attr={cIsFixed:"data-mfx-is-fixed",cPosition:"data-mfx-current-position",cLeft:"data-mfx-current-left",cTop:"data-mfx-current-top",cWidth:"data-mfx-current-width",cZIndex:"data-mfx-current-zindex",fLeft:"data-mfx-left",fTop:"data-mfx-top",fWidth:"data-mfx-width",fAtPos:"data-mfx-fix-at",fTopPos:"data-mfx-top-position"};var clearAttr=function(element){$(element).removeAttr(attr.fLeft+" "+attr.fWidth+" "+attr.cLeft+" "+attr.cTop+" "+attr.cWidth+" "+attr.cZIndex)};$(window).scroll(function(e){var scrolled=$(window).scrollTop();$(sel).each(function(){el=$(this);elc=this;if(!el.attr(attr.fLeft))el.attr(attr.fLeft,el.offset().left+"px");if(!el.attr(attr.fWidth))el.attr(attr.fWidth,el.width()+"px");if(!el.attr(attr.cPosition))el.attr(attr.cPosition,el.css("position"));if(!el.attr(attr.cLeft))el.attr(attr.cLeft,el.css("left"));if(!el.attr(attr.cTop))el.attr(attr.cTop,el.css("top"));if(!el.attr(attr.cWidth))el.attr(attr.cWidth,el.css("width"));if(!el.attr(attr.cZIndex))el.attr(attr.cZIndex,el.css("z-index"));if(!el.attr(attr.fAtPos))el.attr(attr.fAtPos,el.offset().top);if(el.attr(attr.fAtPos)<=scrolled){el.attr(attr.cIsFixed,1);el.css({position:"fixed",left:el.attr(attr.fLeft),top:el.attr(attr.fTopPos)?el.attr(attr.fTopPos)+"px":defOpts.defTopPos,width:el.attr(attr.fWidth),zIndex:defOpts.defZIndex})}else{el.attr(attr.cIsFixed,0);el.css({position:el.attr(attr.cPosition),left:el.attr(attr.cLeft),top:el.attr(attr.cTop),width:el.attr(attr.cWidth),zIndex:el.attr(attr.cZIndex)});clearAttr(elc)}if(el.attr(attr.cIsFixed)==1&&options&&options.onFixed&&typeof options.onFixed=="function")options.onFixed(elc);if(el.attr(attr.cIsFixed)==0&&options&&options.onUnFixed&&typeof options.onUnFixed=="function")options.onUnFixed(elc);$(window).off("orientationchange").on("orientationchange",function(){el.css({position:el.attr(attr.cPosition),left:el.attr(attr.cLeft),top:el.attr(attr.cTop),width:el.attr(attr.cWidth),zIndex:el.attr(attr.cZIndex)});clearAttr(elc)})})})};