I need to pass a value to a style property inside data, but, because of how vuejs and how JS scopes work, it won't let me access through this.data.property:
Vue.component ('loader-component', {
template: '#loader-template',
mounted: function() {
this.animationTest();
},
data: function() {
return {
svg: true,
timer: 0,
// styles
position: {
marginLeft: '',
marginTop: '',
float: 'left'
}
};
},
methods: {
animation: function() {
let timer = 0,
itemWidth = 60,
domWidth = document.getElementById('awesome-body').clientWidth,
domHeight = document.getElementById('awesome-body').clientHeight,
marginL = -2 * itemWidth,
marginT = Math.floor((Math.random() * domHeight) + 1);
this.position.marginTop = marginT;
setInterval(function() {
marginL = marginL + timer * 5;
timer++;
// console.log(marginL);
this.position.marginLeft = marginL;
}, 1000); // time interval in milliseconds
}
} // methods finishes
});
This will trigger the next error:
Cannot set property 'marginLeft' of undefined.
What's the syntax to go directly from the setInterval function to data.marginTop?
Thanks!