diff options
| author | Jacob Thornton <[email protected]> | 2012-07-22 18:28:39 -0700 |
|---|---|---|
| committer | Jacob Thornton <[email protected]> | 2012-07-22 18:28:39 -0700 |
| commit | dcf75697ecd243517b23d8ef440f772d91f699c0 (patch) | |
| tree | f0bd9a5a2bd9c0e8a2dd09ceb4606009c5051181 /js/bootstrap-affix.js | |
| parent | fa1e1e34dfd9e8501ffdbb92a282ff5550685f3c (diff) | |
| download | bootstrap-dcf75697ecd243517b23d8ef440f772d91f699c0.tar.xz bootstrap-dcf75697ecd243517b23d8ef440f772d91f699c0.zip | |
some progress on affix plugin
Diffstat (limited to 'js/bootstrap-affix.js')
| -rw-r--r-- | js/bootstrap-affix.js | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/js/bootstrap-affix.js b/js/bootstrap-affix.js new file mode 100644 index 000000000..a1cd10933 --- /dev/null +++ b/js/bootstrap-affix.js @@ -0,0 +1,102 @@ +/* ========================================================== + * bootstrap-affix.js v2.1.0 + * http://twitter.github.com/bootstrap/javascript.html#affix + * ========================================================== + * Copyright 2012 Twitter, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ========================================================== */ + + +!function ($) { + + "use strict"; // jshint ;_; + + + /* AFFIX CLASS DEFINITION + * ====================== */ + + var Affix = function (element, options) { + this.options = $.extend({}, $.fn.affix.defaults, options) + this.$window = $(window) + .on('scroll.affix.data-api', $.proxy(this.checkPosition, this)) + .on('resize.affix.data-api', $.proxy(this.refresh, this)) + this.$element = $(element) + this.refresh() + } + + Affix.prototype.refresh = function () { + this.position = this.$element.offset() + } + + Affix.prototype.checkPosition = function () { + if (!this.$element.is(':visible')) return + + var scrollLeft = this.$window.scrollLeft() + , scrollTop = this.$window.scrollTop() + , position = this.position + , offset = this.options.offset + , affix + + if (typeof offset != 'object') offset = { x: offset, y: offset } + + affix = (offset.x == null || (position.left - scrollLeft <= offset.x)) + && (offset.y == null || (position.top - scrollTop <= offset.y)) + + if (affix == this.affixed) return + + this.affixed = affix + + this.$element[affix ? 'addClass' : 'removeClass']('affix') + } + + + /* AFFIX PLUGIN DEFINITION + * ======================= */ + + $.fn.affix = function (option) { + return this.each(function () { + var $this = $(this) + , data = $this.data('affix') + , options = typeof option == 'object' && option + if (!data) $this.data('affix', (data = new Affix(this, options))) + if (typeof option == 'string') data[option]() + }) + } + + $.fn.affix.Constructor = Affix + + $.fn.affix.defaults = { + offset: 0 + } + + + /* AFFIX DATA-API + * ============== */ + + $(function () { + $('[data-spy="affix"]').each(function () { + var $spy = $(this) + , data = $spy.data() + + data.offset = data.offset || {} + + data.offsetX && (data.offset.x = data.offsetX) + data.offsetY && (data.offset.y = data.offsetY) + + $spy.affix(data) + }) + }) + + +}(window.jQuery);
\ No newline at end of file |
