Skip to content

Commit

Permalink
updated parallax image load, added jumbotron, updated deprecated test
Browse files Browse the repository at this point in the history
  • Loading branch information
johnleider committed Sep 3, 2017
1 parent 9e3837a commit 33c3ef6
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 32 deletions.
5 changes: 2 additions & 3 deletions src/components/VParallax/VParallax.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import { test } from '~util/testing'
import VParallax from '~components/VParallax'

test('VParallax.js', ({ mount }) => {
it('should abandon init if already destroyed', async () => {
it('should render', async () => {
const wrapper = mount(VParallax, {
attachToDocument: true
})

wrapper.vm.$destroy()
wrapper.vm.init()
expect(wrapper.html()).toMatchSnapshot()
})
})
41 changes: 26 additions & 15 deletions src/components/VParallax/VParallax.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,38 @@
mixins: [Translatable],
data () {
return {
isBooted: false
}
},
props: {
height: {
type: [String, Number],
default: 500
},
jumbotron: Boolean,
src: String
},
computed: {
styles () {
return {
display: 'block',
opacity: this.isBooted ? 1 : 0,
transform: `translate3d(-50%, ${this.parallax}px, 0)`
}
}
},
methods: {
init () {
if (!this.$refs.img) return
watch: {
parallax () {
this.isBooted = true
}
},
if (this.$refs.img.complete) {
this.translate()
this.listeners()
} else {
this.$refs.img.addEventListener('load', () => {
this.translate()
this.listeners()
}, false)
}
},
methods: {
objHeight () {
return this.$refs.img.naturalHeight
},
Expand All @@ -63,13 +64,23 @@
staticClass: 'parallax__content'
}, this.$slots.default)
let directives = this.directives()
if (this.jumbotron) {
directives = directives.filter(d => d.name !== 'scroll')
}
return h('div', {
staticClass: 'parallax',
style: {
height: `${parseInt(this.normalizedHeight)}px`
minHeight: isNaN(this.height) ? this.height : `${this.height}px`
},
directives,
on: this.$listeners
}, [container, content])
}, [
container,
content
])
}
}
</script>
Expand Down
17 changes: 17 additions & 0 deletions src/components/VParallax/__snapshots__/VParallax.spec.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`VParallax.js should render 1`] = `
<div class="parallax"
style="min-height: 500px;"
>
<div class="parallax__image-container">
<img class="parallax__image"
style="display: block; opacity: 0;"
>
</div>
<div class="parallax__content">
</div>
</div>
`;
39 changes: 25 additions & 14 deletions src/mixins/translatable.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import Resize from '../directives/resize'
import Scroll from '../directives/scroll'

export default {
directives: {
Resize,
Scroll
},

data () {
return {
normalizedHeight: null,
parallax: null,
parallaxDist: null,
percentScrolled: null,
Expand All @@ -11,28 +20,29 @@ export default {
},

computed: {
normalizedHeight () {
return Number(this.height.toString().replace(/(^[0-9]*$)/, '$1'))
},

imgHeight () {
return this.objHeight()
}
},

mounted () {
this.$vuetify.load(this.init)
},

beforeDestroy () {
window.removeEventListener('scroll', this.translate, false)
window.removeEventListener('resize', this.translate, false)
watch: {
height () {
this.$nextTick(this.translate)
}
},

methods: {
listeners () {
window.addEventListener('scroll', this.translate, false)
window.addEventListener('resize', this.translate, false)
directives () {
return [
{
name: 'scroll',
value: this.translate
},
{
name: 'resize',
value: this.translate
}
]
},

translate () {
Expand All @@ -53,6 +63,7 @@ export default {
calcDimensions () {
const offset = this.$el.getBoundingClientRect()

this.normalizedHeight = offset.height
this.scrollTop = window.pageYOffset
this.parallaxDist = this.imgHeight - this.normalizedHeight
this.elOffsetTop = offset.top + this.scrollTop
Expand Down
1 change: 1 addition & 0 deletions src/stylus/components/_parallax.styl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
min-height: 100%
display: none
transform: translate3d(-50%, 0, 0)
transition: .3s opacity $transition.swing
z-index: 1

&__content
Expand Down

0 comments on commit 33c3ef6

Please sign in to comment.