<div
id="overlay_block"
class="overlay-type overlay"
data-text-placement="{{ block.settings.overlay_position }}"
data-mobile-text-placement="{{ block.settings.overlay_mobile }}">
</div>
and this bit of javascript that should add/remove the div class on window resize.
var mq = window.matchMedia("(max-width: 767px)");
var overlayBlock = document.getElementById("overlay_block");
var textPlacement = overlayBlock.getAttribute("data-text-placement");
var mobileTextPlacement = overlayBlock.getAttribute("data-mobile-text-placement");
console.log("textPlacement: ", textPlacement);
console.log("mobileTextPlacement: ", mobileTextPlacement);
if (mq.matches) {
overlayBlock.classList.add(mobileTextPlacement);
overlayBlock.classList.remove(textPlacement);
} else {
overlayBlock.classList.add(textPlacement);
overlayBlock.classList.remove(mobileTextPlacement);
}
window.addEventListener("resize", function () {
if (window.innerWidth < 767) {
overlayBlock.classList.add(mobileTextPlacement);
overlayBlock.classList.remove(textPlacement);
} else {
overlayBlock.classList.add(textPlacement);
overlayBlock.classList.remove(mobileTextPlacement);
}
});
As you can see from the code I have logged to console the variables that contains the class that should be added to the div and they contain the correct values that are the css class i need to add:
textPlacement: position--left position--top
mobileTextPlacement: position--right position--bottom
Also if i replace one of the statements (e.g. overlayBlock.classList.add(textPlacement) with a string overlayBlock.classList.add("some text")
the string gets added correctly to the div class. Any hint on how to fix my code?
PS: I'd prefer to avoid using jquery
Any help appreciated
textPlacement: position--left position--top-classList.adddoes not work with one value that contains multiple class names. Each parameter needs to be a single valid class name.