Created
February 11, 2014 21:31
-
-
Save ryun/8944613 to your computer and use it in GitHub Desktop.
Floating headers - jquery.floater.js
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(){ | |
| function Floater(el, opt) { | |
| this.el = el; | |
| this.$el = $(el); | |
| opt = opt||{}; | |
| this.init(); | |
| }; | |
| Floater.prototype.init = function() { | |
| console.info('Init'); | |
| var _this = this; | |
| this.targetClass = this.$el.data('target')||'.float-header'; | |
| // if ( ! this.targetClass) | |
| // { | |
| // this.targetClass = ; | |
| // } | |
| this.$target = $(this.targetClass, this.$el); | |
| this.cloneHeader = this.$target.clone() | |
| .css("width", this.$target.width()) | |
| .addClass("floatingHeader"); | |
| this.$target | |
| .before(this.cloneHeader); | |
| $(window).scroll(function() { | |
| var offset = _this.$el.offset(), | |
| scrollTop = $(this).scrollTop(), | |
| floatingHeader = _this.cloneHeader; | |
| if ((scrollTop > offset.top) && (scrollTop < offset.top + _this.$el.height())) { | |
| floatingHeader.css({"visibility": "visible"}); | |
| } else { | |
| floatingHeader.css({"visibility": "hidden"}); | |
| }; | |
| }).trigger("scroll"); | |
| }; | |
| $.fn.floatHead = function(options) { | |
| return this.each(function () { | |
| var $this = $(this), | |
| data = $this.data('floater') | |
| if (!data) $this.data('floater', (data = new Floater(this, options))) | |
| if (typeof option == 'string') data[option].call($this) | |
| }) | |
| }; | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment