diff --git a/bower.json b/bower.json new file mode 100644 index 0000000..c110d06 --- /dev/null +++ b/bower.json @@ -0,0 +1,31 @@ +{ + "name": "jquery-lockfixed", + "version": "0.9.1", + "description": "lockfixed is a jQuery plugin which allows for DOM elements to be positioned anywhere on the page, and lock within the user's viewport when scrolling.", + "main": "jquery.lockfixed.js", + "dependencies": { + "jquery": ">=1.5" + }, + "keywords": [ + "jquery-plugin", + "lockfixed", + "lock", + "fixed", + "sticky", + "css" + ], + "authors": [ + { "name": "Yvo Schaap", + "homepage": "http://www.directlyrics.com/code/lockfixed/" + } + ], + "license": "MIT", + "homepage": "http://www.directlyrics.com/code/lockfixed/", + "ignore": [ + "**/.*", + "node_modules", + "bower_components", + "test", + "tests" + ] +} diff --git a/jquery.lockfixed.js b/jquery.lockfixed.js index 04eb963..72f6934 100644 --- a/jquery.lockfixed.js +++ b/jquery.lockfixed.js @@ -2,57 +2,54 @@ * jQuery lockfixed plugin * http://www.directlyrics.com/code/lockfixed/ * - * Copyright 2012 Yvo Schaap + * Copyright 2012-2015 Yvo Schaap * Released under the MIT license * http://www.directlyrics.com/code/lockfixed/license.txt * - * Date: Sun March 4 2014 12:00:01 GMT + * Date: Wed April 1 2015 12:00:01 GMT */ -(function($, undefined){ +(function($, undefined) { $.extend({ /** * Lockfixed initiated * @param {Element} el - a jquery element, DOM node or selector string * @param {Object} config - offset - forcemargin */ - "lockfixed": function(el, config){ + "lockfixed": function(el, config) { if (config && config.offset) { - config.offset.bottom = parseInt(config.offset.bottom,10); - config.offset.top = parseInt(config.offset.top,10); - }else{ - config.offset = {bottom: 100, top: 0}; + config.offset.bottom = parseInt(config.offset.bottom, 10); + config.offset.top = parseInt(config.offset.top, 10); + } else { + config.offset = { + bottom: 100, + top: 0 + }; } var el = $(el); - if(el && el.offset()){ + if (el && el.offset()) { var el_position = el.css("position"), - el_margin_top = parseInt(el.css("marginTop"),10), + el_margin_top = parseInt(el.css("marginTop"), 10) || 0, el_position_top = el.css("top"), el_top = el.offset().top, pos_not_fixed = false; - - /* - * We prefer feature testing, too much hassle for the upside - * while prettier to use position: fixed (less jitter when scrolling) - * iOS 5+ + Android has fixed support, but issue with toggeling between fixed and not and zoomed view - */ - if (config.forcemargin === true || navigator.userAgent.match(/\bMSIE (4|5|6)\./) || navigator.userAgent.match(/\bOS ([0-9])_/) || navigator.userAgent.match(/\bAndroid ([0-9])\./i)){ + + + //We prefer feature testing, too much hassle for the upside + //while prettier to use position: fixed (less jitter when scrolling) + //iOS 5+ && Android does has fixed support, but results in issue with toggeling between fixed and viewport zoom + + if (config.forcemargin === true || navigator.userAgent.match(/\bMSIE (4|5|6)\./) || navigator.userAgent.match(/\bOS ([0-9])_/) || navigator.userAgent.match(/\bAndroid ([0-9])\./i)) { pos_not_fixed = true; } - /* - // adds throttle to scroll call - $(window).bind('scroll',el,function(e){ + // We wrap the element with the height of the lockfixed, because position: fixed removes the height leaving an empty area (and some jitter) + el.wrap("
"); - window.setTimeout(function(){ - $(document).trigger('lockfixed:pageupdate'); - },25); - }); - */ - - $(window).bind('DOMContentLoaded load scroll resize orientationchange lockfixed:pageupdate',el,function(e){ + // Bind to most comment events that will need to recalculate our lockfixed position + $(window).bind('DOMContentLoaded load scroll resize orientationchange lockfixed:pageupdate', el, function(e) { // if we have a input focus don't change this (for smaller screens) - if(pos_not_fixed && document.activeElement && document.activeElement.nodeName === "INPUT"){ - return; + if (pos_not_fixed && document.activeElement && document.activeElement.nodeName === "INPUT") { + return; } var top = 0, @@ -60,30 +57,43 @@ el_width = el.outerWidth(), max_height = $(document).height() - config.offset.bottom, scroll_top = $(window).scrollTop(); - + // if element is not currently fixed position, reset measurements ( this handles DOM changes in dynamic pages ) if (el.css("position") !== "fixed" && !pos_not_fixed) { el_top = el.offset().top; el_position_top = el.css("top"); } - if (scroll_top >= (el_top-(el_margin_top ? el_margin_top : 0)-config.offset.top)){ + if (scroll_top >= (el_top - (el_margin_top ? el_margin_top : 0) - config.offset.top)) { - if(max_height < (scroll_top + el_height + el_margin_top + config.offset.top)){ + if (max_height < (scroll_top + el_height + el_margin_top + config.offset.top) && + el_height + config.offset.top > $(window).height() + ) { top = (scroll_top + el_height + el_margin_top + config.offset.top) - max_height; - }else{ - top = 0; + } else { + top = 0; } - if (pos_not_fixed){ - el.css({'marginTop': (parseInt(scroll_top - el_top - top,10) + (2 * config.offset.top))+'px'}); - }else{ - el.css({'position': 'fixed','top':(config.offset.top-top)+'px','width':el_width +"px"}); + if (pos_not_fixed) { + el.css({ + 'marginTop': (parseInt(scroll_top - el_top - top, 10) + (2 * config.offset.top)) + 'px' + }); + } else { + el.css({ + 'position': 'fixed', + 'top': (config.offset.top - top) + 'px', + 'width': el_width + "px" + }); } - }else{ - el.css({'position': el_position,'top': el_position_top, 'width':el_width +"px", 'marginTop': (el_margin_top && !pos_not_fixed ? el_margin_top : 0)+"px"}); + } else { + el.css({ + 'position': el_position, + 'top': el_position_top, + 'width': el_width + "px", + 'marginTop': (el_margin_top && !pos_not_fixed ? el_margin_top : 0) + "px" + }); } - }); + }); } } }); diff --git a/jquery.lockfixed.min.js b/jquery.lockfixed.min.js index 0736814..396deb6 100644 --- a/jquery.lockfixed.min.js +++ b/jquery.lockfixed.min.js @@ -2,12 +2,12 @@ * jQuery lockfixed plugin * http://www.directlyrics.com/code/lockfixed/ * - * Copyright 2012 Yvo Schaap + * Copyright 2012-2015 Yvo Schaap * Released under the MIT license * http://www.directlyrics.com/code/lockfixed/license.txt * - * Date: Sun March 4 2014 12:00:01 GMT + * Date: Wed April 1 2015 12:00:01 GMT */ -(function(e,p){e.extend({lockfixed:function(a,b){b&&b.offset?(b.offset.bottom=parseInt(b.offset.bottom,10),b.offset.top=parseInt(b.offset.top,10)):b.offset={bottom:100,top:0};if((a=e(a))&&a.offset()){var n=a.css("position"),c=parseInt(a.css("marginTop"),10),l=a.css("top"),h=a.offset().top,f=!1;if(!0===b.forcemargin||navigator.userAgent.match(/\bMSIE (4|5|6)\./)||navigator.userAgent.match(/\bOS ([0-9])_/)||navigator.userAgent.match(/\bAndroid ([0-9])\./i))f=!0;e(window).bind("DOMContentLoaded load scroll resize orientationchange lockfixed:pageupdate", -a,function(k){if(!f||!document.activeElement||"INPUT"!==document.activeElement.nodeName){var d=0,d=a.outerHeight();k=a.outerWidth();var m=e(document).height()-b.offset.bottom,g=e(window).scrollTop();"fixed"===a.css("position")||f||(h=a.offset().top,l=a.css("top"));g>=h-(c?c:0)-b.offset.top?(d=m