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

feat(sticky): add option to select a different triggering element [hasTests] [mergeMe] #529

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions src/shortcuts/sticky.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
var originalHandler = this.options.handler

this.waypoint = new Waypoint($.extend({}, this.options, {
element: this.wrapper,
element: this.options.triggerElement || this.wrapper,
handler: $.proxy(function(direction) {
var shouldBeStuck = this.options.direction.indexOf(direction) > -1
var wrapperHeight = shouldBeStuck ? this.$element.outerHeight(true) : ''
Expand Down Expand Up @@ -56,7 +56,8 @@
Sticky.defaults = {
wrapper: '<div class="sticky-wrapper" />',
stuckClass: 'stuck',
direction: 'down right'
direction: 'down right',
triggerElement: false
}

Waypoint.Sticky = Sticky
Expand Down
8 changes: 7 additions & 1 deletion test/fixtures/sticky.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<div class="wp-container">
<div class="before"></div>
<div class="sticky"></div>
<div class="triggerElement"></div>
</div>

<style type="text/css">
.wp-container {
position: relative;
height:3000px;
}

Expand All @@ -15,4 +17,8 @@
.sticky {
height:50px;
}
</style>
.triggerElement {
position: absolute;
top: 1000px;
}
</style>
30 changes: 29 additions & 1 deletion test/sticky-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
describe('Waypoint Sticky Shortcut', function() {
var $ = window.jQuery
var $scroller = $(window)
var $sticky, waypoint, handlerSpy
var $sticky, $triggerElement, waypoint, handlerSpy

beforeEach(function() {
loadFixtures('sticky.html')
$sticky = $('.sticky')
$triggerElement = $('.triggerElement')
})

describe('with default options', function() {
Expand Down Expand Up @@ -114,4 +115,31 @@ describe('Waypoint Sticky Shortcut', function() {
expect(parent).toBe(waypoint.wrapper)
})
})

describe('with triggerElement', function() {
beforeEach(function() {
waypoint = new Waypoint.Sticky({
element: $sticky[0],
triggerElement: $triggerElement[0]
})
})
afterEach(function() {
waypoint.destroy()
})

it('when scrolling', function() {
runs(function() {
$scroller.scrollTop($triggerElement.offset().top)
})
waitsFor(function() {
return $sticky.hasClass('stuck')
}, 'gets stuck at triggerElement')
runs(function() {
$scroller.scrollTop($triggerElement.offset().top - 1)
})
waitsFor(function() {
return !$sticky.hasClass('stuck')
}, 'gets unstuck when scrolling back')
})
})
})