diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..d004a9a --- /dev/null +++ b/.npmignore @@ -0,0 +1,2 @@ +/node_modules/ +.DS_Store \ No newline at end of file diff --git a/README.md b/README.md index 17d3d8a..76e7320 100644 --- a/README.md +++ b/README.md @@ -22,8 +22,14 @@ Find the [documentation](http://www.directlyrics.com/code/lockfixed/). ### Problems? -Just send either one of us a message on Twitter: @yvoschaap. +Just send me a message via @yvoschaap or yvo [@] yvoschaap.com. ### License -lockfixed is released under the MIT license. See the [license](http://www.directlyrics.com/code/lockfixed/license.txt) for details. \ No newline at end of file +lockfixed is released under the MIT license. See the [license](http://www.directlyrics.com/code/lockfixed/license.txt) for details. + + +## Keywords for repository search: + +lockfixed, sticky, scroll, set, element, lock, fixed, lock fixed + 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 aa6b8cf..72f6934 100644 --- a/jquery.lockfixed.js +++ b/jquery.lockfixed.js @@ -2,73 +2,98 @@ * 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 Jan 27 2013 12:00:00 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()){ - var el_top = el.offset().top, - el_left = el.offset().left, - el_height = el.outerHeight(true), - el_width = el.outerWidth(), - el_position = el.css("position"), - el_position_top = el.css("top"), - el_margin_top = parseInt(el.css("marginTop"),10), - max_height = $(document).height() - config.offset.bottom, - top = 0, - swtch = false, - 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+ + Andriud has fixed support, but issue with toggeling between fixed and not and zoomed view, is iOs only calls after scroll is done, so we ignore iOS 5 for now */ - if (config.forcemargin === true || navigator.userAgent.match(/\bMSIE (4|5|6)\./) || navigator.userAgent.match(/\bOS (3|4|5|6)_/) || navigator.userAgent.match(/\bAndroid (1|2|3|4)\./i)){ + var el = $(el); + if (el && el.offset()) { + var el_position = el.css("position"), + 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 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; } - - $(window).bind('scroll resize orientationchange load',el,function(e){ - var el_height = el.outerHeight(), + + // 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("
"); + + // 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; + } + + var top = 0, + el_height = el.outerHeight(), + el_width = el.outerWidth(), + max_height = $(document).height() - config.offset.bottom, scroll_top = $(window).scrollTop(); - //if we have a input focus don't change this (for ios zoom and stuff) - if(pos_not_fixed && document.activeElement && document.activeElement.nodeName === "INPUT"){ - return; - } + // 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(((el_margin_top ? el_margin_top : 0) + (scroll_top - el_top - top) + 2 * config.offset.top),10)+'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 ? 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 fa497d5..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 Jan 27 2013 12:00:00 GMT + * Date: Wed April 1 2015 12:00:01 GMT */ -(function(d){d.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=d(a))&&a.offset()){var h=a.offset().top;a.offset();a.outerHeight(!0);var j=a.outerWidth(),m=a.css("position"),n=a.css("top"),c=parseInt(a.css("marginTop"),10),k=d(document).height()-b.offset.bottom,f=0,g=!1;if(!0===b.forcemargin||navigator.userAgent.match(/\bMSIE (4|5|6)\./)||navigator.userAgent.match(/\bOS (3|4|5|6)_/)|| -navigator.userAgent.match(/\bAndroid (1|2|3|4)\./i))g=!0;d(window).bind("scroll resize orientationchange load",a,function(){var l=a.outerHeight(),e=d(window).scrollTop();if(!g||!(document.activeElement&&"INPUT"===document.activeElement.nodeName))e>=h-(c?c:0)-b.offset.top?(f=k");d(window).bind("DOMContentLoaded load scroll resize orientationchange lockfixed:pageupdate",a,function(k){if(!f||!document.activeElement||"INPUT"!==document.activeElement.nodeName){var c=0,c=a.outerHeight();k=a.outerWidth();var m=d(document).height()-b.offset.bottom,g=d(window).scrollTop();"fixed"===a.css("position")||f||(h=a.offset().top,l=a.css("top"));g>=h-(e?e:0)-b.offset.top?(c=md(window).height()?g+c+e+b.offset.top- +m:0,f?a.css({marginTop:parseInt(g-h-c,10)+2*b.offset.top+"px"}):a.css({position:"fixed",top:b.offset.top-c+"px",width:k+"px"})):a.css({position:n,top:l,width:k+"px",marginTop:(e&&!f?e:0)+"px"})}})}}})})(jQuery); \ No newline at end of file diff --git a/lockfixed.jquery.json b/lockfixed.jquery.json index 7c52ab7..46a9a3e 100644 --- a/lockfixed.jquery.json +++ b/lockfixed.jquery.json @@ -6,16 +6,17 @@ "fixed", "dom", "ui", - "scroll" + "scroll", + "lockfixed" ], - "version": "0.4.0", + "version": "0.8.4", "author": { "name": "Yvo Schaap", "url": "http://www.directlyrics.com/code/lockfixed/" }, "maintainers": [ { - "name": "Lyrics", + "name": "Directlyrics", "email": "yvo@yvoschaap.com", "url": "http://www.directlyrics.com/" } @@ -27,7 +28,7 @@ } ], "homepage": "http://www.directlyrics.com/code/lockfixed/", - "demp": "http://www.directlyrics.com/code/lockfixed/demo.html", + "demo": "http://www.directlyrics.com/code/lockfixed/demo.html", "docs": "http://www.directlyrics.com/code/lockfixed/", "dependencies": { "jquery": ">=1.5" diff --git a/package.json b/package.json new file mode 100644 index 0000000..727d9ab --- /dev/null +++ b/package.json @@ -0,0 +1,40 @@ +{ + "name": "lockfixed", + "title": "jQuery Lock Fixed", + "description": "jQuery plugin which allows for DOM elements to be positioned anywhere on the page, and lock within the user's viewport when scrolling. Common use case is a menu under a header, which on scrolling stays fixed within the viewport.", + "keywords": [ + "jquery-plugin", + "fixed", + "dom", + "ui", + "scroll", + "lockfixed", + "lock fixed", + "sticky", + "jquery" + ], + "version": "0.9.1", + "author": { + "name": "Yvo Schaap", + "url": "http://www.directlyrics.com/code/lockfixed/" + }, + "licenses": [ + { + "type": "MIT", + "url": "http://www.directlyrics.com/code/lockfixed/license.txt" + } + ], + "homepage": "http://www.directlyrics.com/code/lockfixed/", + "demo": "http://www.directlyrics.com/code/lockfixed/demo.html", + "docs": "http://www.directlyrics.com/code/lockfixed/", + "dependencies": { + "jquery": ">=1.5" + }, + "repository": { + "type": "git", + "url": "https://github.com/ymschaap/jquery-lockfixed" + }, + "bugs": { + "url": "https://github.com/ymschaap/jquery-lockfixed/issues" + } +} \ No newline at end of file