Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vue-parallaxy",
"version": "1.1.1",
"version": "1.1.2",
"description": "Vue 2 component for parallax scrolling effects",
"author": "Jakub Juszczak <[email protected]>",
"license": "MIT",
Expand Down Expand Up @@ -99,5 +99,8 @@
"engines": {
"node": ">= 4.0.0",
"npm": ">= 3.0.0"
},
"dependencies": {
"yarn": "^1.10.1"
}
}
30 changes: 24 additions & 6 deletions src/components/Parallax.vue
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,19 @@
direction: {
type: String,
default: 'up'
},
customWrapperReference: {
type: String,
default: '',
required: false
}
},

data () {
return {
el: null,
mediaQuery: null
mediaQuery: null,
scrollWrapper: null
}
},

Expand All @@ -81,7 +87,13 @@
const parentHeight = this.$refs.block.offsetHeight
const parallaxHeight = this.$refs.parallax.offsetHeight
const availableOffset = parallaxHeight - parentHeight
let animationValue = (window.pageYOffset * this.speedFactor)
let animationValue

if (this.customWrapperReference) {
animationValue = (this.scrollWrapper.scrollTop * this.speedFactor)
} else {
animationValue = (this.scrollWrapper.pageYOffset * this.speedFactor)
}

if (animationValue <= availableOffset && animationValue >= 0) {
this.el.style.transform = `translate3d(0, ${animationValue * this.directionValue}px ,0)`
Expand All @@ -101,21 +113,27 @@

return (
rect.bottom >= 0 &&
rect.top <= (window.innerHeight || document.documentElement.clientHeight)
rect.top <= (this.scrollWrapper.innerHeight || document.documentElement.clientHeight)
)
},

setupListener () {
if (this.mediaQuery.matches) {
window.addEventListener('scroll', this.scrollHandler, false)
this.scrollWrapper.addEventListener('scroll', this.scrollHandler, false)
} else {
window.removeEventListener('scroll', this.scrollHandler, false)
this.scrollWrapper.removeEventListener('scroll', this.scrollHandler, false)
}
},

init () {
this.mediaQuery = window.matchMedia(this.breakpoint)

if (this.customWrapperReference) {
this.scrollWrapper = document.querySelector(this.customWrapperReference)
} else {
this.scrollWrapper = window
}

if (this.mediaQuery) {
this.mediaQuery.addListener(this.setupListener)
this.setupListener()
Expand All @@ -124,7 +142,7 @@
},

beforeDestroy () {
window.removeEventListener('scroll', this.scrollHandler, false)
this.scrollWrapper.removeEventListener('scroll', this.scrollHandler, false)
},

computed: {
Expand Down