Skip to content

Commit e53a3ab

Browse files
committed
Add option to configure the scrollable container
Note: some calculations might require tweaking
1 parent f306732 commit e53a3ab

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

sticky-kit.coffee

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ $ = window.jQuery
77
win = $ window
88
doc = $ document
99

10+
scroll_container = win;
11+
1012
$.fn.stick_in_parent = (opts={}) ->
1113
{
1214
sticky_class
@@ -16,6 +18,7 @@ $.fn.stick_in_parent = (opts={}) ->
1618
offset_top
1719
spacer: manual_spacer
1820
bottoming: enable_bottoming
21+
container
1922
} = opts
2023

2124
win_height = win.height()
@@ -28,6 +31,8 @@ $.fn.stick_in_parent = (opts={}) ->
2831

2932
enable_bottoming = true unless enable_bottoming?
3033

34+
scroll_container = $ container if container?
35+
3136
# we need this because jquery's version (along with css()) rounds everything
3237
outer_width = (el) ->
3338
if window.getComputedStyle
@@ -131,7 +136,7 @@ $.fn.stick_in_parent = (opts={}) ->
131136
recalc()
132137
recalced = true
133138

134-
scroll = win.scrollTop()
139+
scroll = scroll_container.scrollTop()
135140
if last_pos?
136141
delta = scroll - last_pos
137142
last_pos = scroll
@@ -230,9 +235,9 @@ $.fn.stick_in_parent = (opts={}) ->
230235

231236
detach = ->
232237
detached = true
233-
win.off "touchmove", tick
234-
win.off "scroll", tick
235-
win.off "resize", recalc_and_tick
238+
scroll_container.off "scroll", tick
239+
scroll_container.off "touchmove", tick
240+
scroll_container.off "resize", recalc_and_tick
236241

237242
$(document.body).off "sticky_kit:recalc", recalc_and_tick
238243
elm.off "sticky_kit:detach", detach
@@ -255,9 +260,10 @@ $.fn.stick_in_parent = (opts={}) ->
255260

256261
elm.removeClass sticky_class
257262

258-
win.on "touchmove", tick
259-
win.on "scroll", tick
260-
win.on "resize", recalc_and_tick
263+
scroll_container.on "scroll", tick
264+
scroll_container.on "touchmove", tick
265+
scroll_container.on "resize", recalc_and_tick
266+
261267
$(document.body).on "sticky_kit:recalc", recalc_and_tick
262268
elm.on "sticky_kit:detach", detach
263269

0 commit comments

Comments
 (0)