Skip to content

Commit

Permalink
added a reusable check for when an el isFullyVisible
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Howard committed Mar 31, 2016
1 parent 00b9cd6 commit 19f7308
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 30 deletions.
18 changes: 17 additions & 1 deletion index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ module.exports =
type: 'string'
default: 'in-viewport'

# Whether to only update in-viewport class once

This comment has been minimized.

Copy link
@weotch

weotch Mar 31, 2016

Member

@thelucre Can you make this comment distinctive?

inViewportEntrelyClass:
type: 'string'
default: 'in-viewport-entirely'

# A positive offset triggers "late" when scrolling down
inViewportOffsetTop:
type: Number
Expand All @@ -51,7 +56,9 @@ module.exports =
default: 0

# Boolean stores whether component is in viewport
data: -> inViewport: false
data: ->
inViewport: false
inViewportEntirely: false

# Add handlers when vm is added to dom unless init is false
ready: -> @addInViewportHandlers() if @inViewportActive
Expand All @@ -71,6 +78,10 @@ module.exports =
@removeInViewportHandlers() if @inViewportOnce and visible
$(@$el).toggleClass(@inViewportClass, visible) if @inViewportClass

inViewportEntirely: (visible) ->
$(@$el).toggleClass(@inViewportEntrelyClass, visible) if @inViewportEntrelyClass


# Public API
methods:

Expand All @@ -79,6 +90,7 @@ module.exports =
@inViewport = @isInViewport @$el,
offsetTop: @inViewportOffsetTop
offsetBottom: @inViewportOffsetBottom
@inViewportEntirely = @isInViewportEntirely @$el

# Add listeners
addInViewportHandlers: ->
Expand All @@ -97,3 +109,7 @@ module.exports =

# Public API for invoking visibility test
isInViewport: (el, options) -> visibility.isInViewport(el, options)

# Public API for invoking visibility test

This comment has been minimized.

Copy link
@weotch

weotch Mar 31, 2016

Member

@thelucre Can you make this comment distinctive?

isInViewportEntirely: (el, options) ->
visibility.isFullyVisible el
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vue-in-viewport-mixin",
"version": "0.4.0",
"version": "0.4.1",
"description": "Vue mixin to determine when a DOM element is visible in the client window",
"main": "index.coffee",
"scripts": {
Expand Down
77 changes: 49 additions & 28 deletions visibility.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,52 @@
# @param DOMELement el
# @param object options
# @return bool
module.exports = isInViewport: (el, options) ->

# Require an el
return false if !el

# Default options
options = {} if !options
options.offsetTop = 0 if !options.offsetTop
options.offsetBottom = 0 if !options.offsetBottom

# Get Viewport dimensions
# http://ryanve.com/lab/dimensions/
vp = {}
vp.height = document.documentElement.clientHeight
vp.width = document.documentElement.clientWidth

# Support percentage offsets
for key in ['offsetTop', 'offsetBottom']
options[key] = vp.height * options[key] if 0 < Math.abs(options[key]) < 1

# Get element dimensions with offsets
vm = el.getBoundingClientRect()

# Test if in viewport
vm.top + options.offsetTop <= vp.height and
vm.bottom + options.offsetBottom >= 0 and
vm.left <= vp.width and
vm.right >= 0
module.exports =
isInViewport: (el, options) ->

# Require an el
return false if !el

# Default options
options = {} if !options
options.offsetTop = 0 if !options.offsetTop
options.offsetBottom = 0 if !options.offsetBottom

# Get Viewport dimensions
# http://ryanve.com/lab/dimensions/
vp = {}
vp.height = document.documentElement.clientHeight
vp.width = document.documentElement.clientWidth

# Support percentage offsets
for key in ['offsetTop', 'offsetBottom']
options[key] = vp.height * options[key] if 0 < Math.abs(options[key]) < 1

# Get element dimensions with offsets
vm = el.getBoundingClientRect()

# Test if in viewport
vm.top + options.offsetTop <= vp.height and
vm.bottom + options.offsetBottom >= 0 and
vm.left <= vp.width and
vm.right >= 0

isFullyVisible: (el) ->

# Require an el
return false if !el

# Get Viewport dimensions
# http://ryanve.com/lab/dimensions/
vp =
height: document.documentElement.clientHeight
width: document.documentElement.clientWidth

# Get element dimensions with offsets
vm = el.getBoundingClientRect()

# Test if in entirely in viewport
vm.top >= 0 and
vm.bottom <= vp.height and
vm.left >= 0 and
vm.right <= vp.width

0 comments on commit 19f7308

Please sign in to comment.