Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure affix target element #13342

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/_includes/js/affix.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ <h3>Options</h3>
<td>10</td>
<td>Pixels to offset from screen when calculating position of scroll. If a single number is provided, the offset will be applied in both top and bottom directions. To provide a unique, bottom and top offset just provide an object <code>offset: { top: 10 }</code> or <code>offset: { top: 10, bottom: 5 }</code>. Use a function when you need to dynamically calculate an offset.</td>
</tr>
<tr>
<td>target</td>
<td>selector | node | jQuery element</td>
<td>window</td>
<td>The selector | node | jQuery element that specifies the target element of the affix. If not specified the <code>window</code> object will be used.</td>
</tr>

</tbody>
</table>
</div><!-- /.table-responsive -->
Expand Down
10 changes: 6 additions & 4 deletions js/affix.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

var Affix = function (element, options) {
this.options = $.extend({}, Affix.DEFAULTS, options)
this.$window = $(window)

this.$target = $(this.options.target)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you dont need this var definition to be a separate line

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

look how $window was done above for ref

.on('scroll.bs.affix.data-api', $.proxy(this.checkPosition, this))
.on('click.bs.affix.data-api', $.proxy(this.checkPositionWithEventLoop, this))

Expand All @@ -30,13 +31,14 @@
Affix.RESET = 'affix affix-top affix-bottom'

Affix.DEFAULTS = {
offset: 0
offset: 0,
target: window
}

Affix.prototype.getPinnedOffset = function () {
if (this.pinnedOffset) return this.pinnedOffset
this.$element.removeClass(Affix.RESET).addClass('affix')
var scrollTop = this.$window.scrollTop()
var scrollTop = this.$target.scrollTop()
var position = this.$element.offset()
return (this.pinnedOffset = position.top - scrollTop)
}
Expand All @@ -49,7 +51,7 @@
if (!this.$element.is(':visible')) return

var scrollHeight = $(document).height()
var scrollTop = this.$window.scrollTop()
var scrollTop = this.$target.scrollTop()
var position = this.$element.offset()
var offset = this.options.offset
var offsetTop = offset.top
Expand Down