Bug 16400: Reintroduce the floating class
[koha_ffzg] / koha-tmpl / intranet-tmpl / lib / jquery / plugins / jquery.fixFloat.js
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 ($, window) {
5     "use strict";
6     $.fn.fixFloat = function (options={}) {
7         var tbh = $(this);
8         var defaults = {
9             enabled: true,
10             originalOffset: tbh.position().top
11         };
12         var originalOffset = typeof options.originalOffset === 'undefined'
13             ? defaults.originalOffset
14             : options.originalOffset;
15         options = $.extend(defaults, options);
16
17         if (tbh.css('position') !== 'absolute') {
18             var tbhBis = tbh.clone();
19             tbhBis.css({
20                 "display": tbh.css("display"),
21                     "visibility": "hidden"
22             });
23             tbhBis.width(tbh.outerWidth(true));
24             tbhBis.height(tbh.outerHeight(true));
25             tbh.after(tbhBis);
26             tbh.width(tbh.width());
27             var tbl = tbh.find("th,td");
28             if (tbl.length > 0) {
29                 tbl.each(function () {
30                     var $elt = $(this);
31                     $elt.width($elt.outerWidth(true));
32                 });
33             }
34             tbh.css({
35                 'position': 'absolute',
36                     'top': originalOffset
37             });
38         }
39         tbh.css({
40             'z-index': 1000
41         });
42
43         if (options.enabled) {
44             $(window).scroll(function () {
45                 var offsetTop = tbh.offset().top;
46                 var s = parseInt($(window).scrollTop(), 10);
47                 var fixMe = (s > offsetTop);
48                 var repositionMe = (s < originalOffset);
49                 if (fixMe) {
50                     tbh.css({
51                         'position': 'fixed',
52                             'top': '0'
53                     });
54                     tbh.addClass("floating");
55                 }
56                 if (repositionMe) {
57                     tbh.css({
58                         'position': 'absolute',
59                             'top': originalOffset
60                     });
61                     tbh.removeClass("floating");
62                 }
63             });
64         }
65     };
66 })(jQuery, window);