Skip to content

Commit

Permalink
Restore support for special targets. Fix scroll parent optimization.
Browse files Browse the repository at this point in the history
  • Loading branch information
zackbloom committed Dec 31, 2013
1 parent edad49f commit 29fb19e
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 42 deletions.
1 change: 1 addition & 0 deletions examples/facebook/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@

<script src="../resources/js/log.js"></script>
<script src="../resources/js/jquery.js"></script>
<script src="../../utils.js"></script>
<script src="../../tether.js"></script>
<script src="../../drop.js"></script>
<script src="../../constraint.js"></script>
Expand Down
3 changes: 2 additions & 1 deletion examples/simple/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
</div>

<script src="../resources/js/jquery.js"></script>
<script src="../../utils.js"></script>
<script src="../../tether.js"></script>
<script src="../../constraint.js"></script>
<script src="../../abutment.js"></script>
Expand All @@ -32,7 +33,7 @@
element: $('.element')[0],
attachment: 'middle right',
offset: '0 0',
target: 'scroll-handle',
target: 'viewport',
targetAttachment: 'middle center',
targetOffset: '0 0'
});
Expand Down
1 change: 1 addition & 0 deletions examples/tooltip/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@

<script src="../resources/js/log.js"></script>
<script src="../resources/js/jquery.js"></script>
<script src="../../utils.js"></script>
<script src="../../tether.js"></script>
<script src="/drop/drop.min.js"></script>
<script src="../../tooltip.js"></script>
Expand Down
29 changes: 18 additions & 11 deletions tether.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,14 @@ class _Tether

@options = extend defaults, @options

{@element, @target} = @options
{@element, @target, @targetModifier} = @options

if @target is 'viewport'
@target = document.body
@targetModifier = 'visible'
else if @target is 'scroll-handle'
@target = document.body
@targetModifier = 'scroll-handle'

for key in ['element', 'target']
if @[key].jquery?
Expand Down Expand Up @@ -143,19 +150,19 @@ class _Tether
@enable(position)

getTargetOffset: ->
if typeof @target is 'string'
switch @target
when 'viewport'
if @targetModifier?
switch @targetModifier
when 'visible'
{top: pageYOffset, left: pageXOffset}
when 'scroll-handle'
{top: pageYOffset + innerHeight * (pageYOffset / document.body.scrollHeight), left: innerWidth - 15}
else
getOffset @target

getTargetSize: ->
if typeof @target is 'string'
switch @target
when 'viewport'
if @targetModifier?
switch @targetModifier
when 'visible'
{height: innerHeight, width: innerWidth}
when 'scroll-handle'
{height: innerHeight * 0.98 * (innerHeight / document.body.scrollHeight), width: 15}
Expand Down Expand Up @@ -278,7 +285,7 @@ class _Tether
right: pageXOffset - left - width + innerWidth
}

if @options.optimizations?.moveElement isnt false# and @$target?
if @options.optimizations?.moveElement isnt false and not @targetModifier?
offsetParent = @cache 'target-offsetparent', => getOffsetParent @target
offsetPosition = @cache 'target-offsetparent-offset', -> getOffset offsetParent
offsetParentStyle = getComputedStyle offsetParent
Expand Down Expand Up @@ -306,8 +313,8 @@ class _Tether
next.offset =
top: next.page.top - offsetPosition.top + scrollTop + offsetBorder.top
left: next.page.left - offsetPosition.left + scrollLeft + offsetBorder.left
right: next.page.right - offsetPosition.right - scrollLeft + offsetBorder.right
bottom: next.page.bottom - offsetPosition.bottom - scrollTop + offsetBorder.bottom
right: next.page.right - offsetPosition.right + offsetParent.scrollWidth - scrollLeft + offsetBorder.right
bottom: next.page.bottom - offsetPosition.bottom + offsetParent.scrollHeight - scrollTop + offsetBorder.bottom

# We could also travel up the DOM and try each containing context, rather than only
# looking at the body, but we're gonna get diminishing returns.
Expand Down Expand Up @@ -373,7 +380,7 @@ class _Tether

offset = extend {}, position.offset
for side in ['top', 'left', 'bottom', 'right']
offset[side] -= offsetParentStyle["border-#{ side }-width"]
offset[side] -= parseFloat offsetParentStyle["border-#{ side }-width"]

transcribe same.offset, offset

Expand Down
29 changes: 18 additions & 11 deletions tether.js

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

Loading

0 comments on commit 29fb19e

Please sign in to comment.