I've created a VueJS component and have it displayed multiple times on the same page. Each component functions independently, but there's an issue with a method that calculates its width offset and applies it as a class (such as width-800).
Everything works fine when there is only one instance of the component on a page.
However, when there are multiple instances, only the last one receives the class. The correct class is calculated for all instances based on their width, but only the final one actually gets the class applied.
<template>
<div id="app-medialib" v-bind:class="[this.breakpoint]">
<p>{{ this.breakpoint }}</p>
The value this.breakpoint
holds the class name as a data property.
mounted: function(){
var self = this;
this.calculateBreakpoint();
window.onresize = function(event) {
self.calculateBreakpoint();
};
}
Here's the method for calculating the breakpoint:
calculateBreakpoint: function(){
var mediaLibraryWidth = null;
var elems = document.getElementsByClassName('header medialib');
for( var i=0; i<elems.length; i++ ){
if( elems[i].offsetParent !== null ){
console.log('elems[i]', elems[i]);
mediaLibraryWidth = elems[i].offsetWidth;
break;
}
}
console.log('calculateBreakpoint', mediaLibraryWidth);
if( mediaLibraryWidth > 956 ) {
this.breakpoint = 'md';
} else if( mediaLibraryWidth < 957 && mediaLibraryWidth > 700 ){
this.breakpoint = 'sm';
} else {
this.breakpoint = 'xs';
}
console.log('calculateBreakpoint', this.breakpoint);
},
Any assistance would be greatly appreciated in solving this issue. I've been trying to figure it out for some time now.
Thank you.