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

Add offset_bottom support, test and small fixes #62

Open
wants to merge 2 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

jquery.sticky-kit.min.js: jquery.sticky-kit.js
closure --language_in=ECMASCRIPT5 $< > $@
closure-compiler --language_in=ECMASCRIPT5 $< > $@

jquery.sticky-kit.js: jquery.sticky-kit.coffee
coffee -c $<
Expand Down
22 changes: 17 additions & 5 deletions jquery.sticky-kit.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ $ = @jQuery

win = $ window
$.fn.stick_in_parent = (opts={}) ->
{ sticky_class, inner_scrolling, parent: parent_selector, offset_top } = opts
# add offset_bottom option
{ sticky_class, inner_scrolling, parent: parent_selector, offset_top, offset_bottom } = opts
offset_top ?= 0
offset_bottom ?= 0
parent_selector ?= undefined
inner_scrolling ?= true
sticky_class ?= "is_stuck"
Expand All @@ -24,7 +26,8 @@ $.fn.stick_in_parent = (opts={}) ->
fixed = false
bottomed = false
spacer = $("<div />")
spacer.css('position', elm.css('position'))
# this breaks test two.html needed for something else?
# spacer.css('position', elm.css('position'))

recalc = ->
border_top = parseInt parent.css("border-top-width"), 10
Expand Down Expand Up @@ -70,6 +73,10 @@ $.fn.stick_in_parent = (opts={}) ->

tick = ->
scroll = win.scrollTop()
win_height = win.height()
# add check for over-scroll as in chrome
if (scroll + win_height > $('body').height())
scroll = $('body').height()-win_height
if last_pos?
delta = scroll - last_pos
last_pos = scroll
Expand Down Expand Up @@ -104,11 +111,16 @@ $.fn.stick_in_parent = (opts={}) ->

# updated offset
if inner_scrolling
win_height = win.height()
if height > win_height # bigger than viewport

# defined earlier
# win_height = win.height()

# account for offset_bottom and offset_top
if height + offset_bottom + offset_top > win_height # bigger than viewport
unless bottomed
offset -= delta
offset = Math.max win_height - height, offset
# add offset_bottom
offset = Math.max win_height - height - offset_bottom, offset
offset = Math.min offset_top, offset

if fixed
Expand Down
22 changes: 14 additions & 8 deletions jquery.sticky-kit.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions jquery.sticky-kit.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

122 changes: 122 additions & 0 deletions test/offset_bottom.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
.container {
border: 2px gray dashed;
padding: 10px;
}

.outer {
border: 2px solid red;
padding: 10px;
overflow: hidden;
width: 600px;
margin: 0 auto;
}

.left {
float: left;
height: 5000px;
border: 1px solid green;
}

.right {
float: right;
border: 1px solid blue;
position: relative;
}

.right div {
margin: 20px 0;
}

.header {
border: 1px solid red;
width: 600px;
margin: 0 auto;
margin-bottom: 10px;
height: 200px;
}
.footer {
border: 1px solid red;
width: 600px;
margin: 0 auto;
margin-bottom: 10px;
height: 200px;
}
.fixed {
height: 95px;
position: fixed;
background: rgba(255, 0, 0, 0.5);
top:0;
left:0;
right:0;
}

</style>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="../jquery.sticky-kit.js"></script>
</head>
<body>
<div class="fixed">
hello
<button class="detach">Detach</button>
<button class="attach">Attach</button>
</div>

<div class="container" id="container">
<div class="header">
The Header
</div>

<div class="outer">
<div class="left">Hello world</div>
<div class="right">
<div>Piss World</div>
<div>Piss World</div>
<div>Piss World</div>
<div>Piss World</div>
<div>Piss World</div>
<div>Piss World</div>
<div>Piss World</div>
<div>Piss World</div>
<div>Piss World</div>
<div>Piss World</div>
<div>Piss World</div>
<div>Piss World</div>
<div>Piss World</div>
<div>Piss World</div>
<div>Piss World</div>
<div>Piss World</div>
<div>Piss World</div>
<div>Piss World</div>
<div>Piss World</div>
<div>Piss World</div>
<div>Piss World</div>
<div>Piss World</div>
<div>Piss World</div>
<div>Piss World</div>
</div>
</div>
<div class="footer">
The Footer
</div>
</div>
<script type="text/javascript">
function attach() {
$(".right").stick_in_parent({ offset_top: 100, offset_bottom: 100});
}

$(document.body).on("click", ".detach", function(e) {
$(".right").trigger("sticky_kit:detach");
});

$(document.body).on("click", ".attach", attach);
attach();
</script>

</body>
</html>