Created
July 25, 2012 18:42
-
-
Save gherkins/3177804 to your computer and use it in GitHub Desktop.
some experimental minimal plugin for fix/unfix position of a container inside another container...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) { | |
$.fn.magnetic = function(options) { | |
var obj = this.find(options.selector); | |
var initialLeft = parseInt(obj.css('left')); | |
var mainOffset = this.offset().left; | |
var fixAt = (initialLeft - options.left ) + mainOffset; | |
this.on('scroll', function() { | |
var offset = $(this).scrollLeft(); | |
if (offset < options.left) { | |
obj.css('position', 'absolute').css('left', options.left+'px'); | |
} | |
else if (offset >= options.left && offset < options.right) { | |
obj.css('position', 'fixed').css('left', fixAt+'px'); | |
} | |
else { | |
obj.css('position', 'absolute').css('left', options.right+'px'); | |
} | |
}); | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment