|
Line 0
Link Here
|
|
|
1 |
/* Source: http://www.webspeaks.in/2011/07/new-gmail-like-floating-toolbar-jquery.html |
| 2 |
Revision: http://jsfiddle.net/pasmalin/AyjeZ/ |
| 3 |
*/ |
| 4 |
(function($){ |
| 5 |
$.fn.fixFloat = function(options){ |
| 6 |
|
| 7 |
var defaults = { |
| 8 |
enabled: true |
| 9 |
}; |
| 10 |
var options = $.extend(defaults, options); |
| 11 |
|
| 12 |
var offsetTop; /**Distance of the element from the top of window**/ |
| 13 |
var s; /**Scrolled distance from the top of window through which we have moved**/ |
| 14 |
var fixMe = true; |
| 15 |
var repositionMe = true; |
| 16 |
|
| 17 |
var tbh = $(this); |
| 18 |
var originalOffset = tbh.position().top; /**Get the actual distance of the element from the top mychange:change to position better work**/ |
| 19 |
|
| 20 |
if (tbh.css('position')!='absolute') { |
| 21 |
var tbhBis = $("<div></div>"); |
| 22 |
tbhBis.css({"display":tbh.css("display"),"visibility":"hidden"}); |
| 23 |
tbhBis.width(tbh.outerWidth(true)); |
| 24 |
tbhBis.height(tbh.outerHeight(true)); |
| 25 |
tbh.after(tbhBis); |
| 26 |
tbh.width(tbh.width()); |
| 27 |
tbh.css({'position':'absolute'}); |
| 28 |
} |
| 29 |
tbh.css({'z-index':1000}); |
| 30 |
|
| 31 |
if(options.enabled){ |
| 32 |
$(window).scroll(function(){ |
| 33 |
var offsetTop = tbh.offset().top; /**Get the current distance of the element from the top **/ |
| 34 |
var s = parseInt($(window).scrollTop(), 10); /**Get the from the top of wondow through which we have scrolled**/ |
| 35 |
var fixMe = true; |
| 36 |
if(s > offsetTop){ |
| 37 |
fixMe = true; |
| 38 |
}else{ |
| 39 |
fixMe = false; |
| 40 |
} |
| 41 |
|
| 42 |
if(s < originalOffset){ |
| 43 |
repositionMe = true; |
| 44 |
}else{ |
| 45 |
repositionMe = false; |
| 46 |
} |
| 47 |
|
| 48 |
if(fixMe){ |
| 49 |
var cssObj = { |
| 50 |
'position' : 'fixed', |
| 51 |
'top' : '0px' |
| 52 |
} |
| 53 |
tbh.css(cssObj); |
| 54 |
tbh.addClass("floating"); |
| 55 |
} |
| 56 |
if(repositionMe){ |
| 57 |
var cssObj = { |
| 58 |
'position' : 'absolute', |
| 59 |
'top' : originalOffset |
| 60 |
} |
| 61 |
tbh.css(cssObj); |
| 62 |
tbh.removeClass("floating"); |
| 63 |
} |
| 64 |
}); |
| 65 |
} |
| 66 |
}; |
| 67 |
})(jQuery); |