vuetify
Version:
Vue.js 2 Semantic Component Framework
61 lines (50 loc) • 1.66 kB
JavaScript
export default {
name: 'translatable',
data: function data() {
return {
parallax: null,
parallaxDist: null,
percentScrolled: null,
scrollTop: null,
windowHeight: null,
windowBottom: null
};
},
computed: {
normalizedHeight: function normalizedHeight() {
if (this.jumbotron) {
return isNaN(this.height) ? this.height : this.height + 'px';
}
return Number(this.height.toString().replace(/(^[0-9]*$)/, '$1'));
},
imgHeight: function imgHeight() {
return this.objHeight();
}
},
beforeDestroy: function beforeDestroy() {
window.removeEventListener('scroll', this.translate, false);
window.removeEventListener('resize', this.translate, false);
},
methods: {
listeners: function listeners() {
window.addEventListener('scroll', this.translate, false);
window.addEventListener('resize', this.translate, false);
},
translate: function translate() {
this.calcDimensions();
this.percentScrolled = (this.windowBottom - this.elOffsetTop) / (this.normalizedHeight + this.windowHeight);
this.parallax = Math.round(this.parallaxDist * this.percentScrolled);
if (this.translated) {
this.translated();
}
},
calcDimensions: function calcDimensions() {
var offset = this.$el.getBoundingClientRect();
this.scrollTop = window.pageYOffset;
this.parallaxDist = this.imgHeight - this.normalizedHeight;
this.elOffsetTop = offset.top + this.scrollTop;
this.windowHeight = window.innerHeight;
this.windowBottom = this.scrollTop + this.windowHeight;
}
}
};