I'm working on a dynamic responsive ad which has various media rules to account for different possible ad sizes. Depending on the size of the page, different content will show.
I've set up some JS conditions which will apply different styles when there's no price, if a media rule which is showing a particular element is active.
This works fine, unless the page is resized after loading. When the page width is modified enough activate a different media rule, the JS styles from the old media rule still remain, which I don't want.
Here is a very simplified/generic example:
var item = document.getElementById("item");
var itemPrice = document.getElementById("price");
var itemIntro = document.getElementById("intro");
var priceDisplayValue = itemPrice.style.display;
var introAlpha = window.getComputedStyle(itemIntro).getPropertyValue("opacity");
if ((introAlpha == "1") && priceDisplayValue == "none") {
item.style.background = "#0099cc";
itemIntro.style.fontSize = "3em";
}
.ad{
position: absolute;
width: 100%;
height: 100%;
background: #ccc;
font-size: 2em;
text-align: center;
color: #333;
}
.item{
position: absolute;
width: 100%;
height: 100%;
background: #AAA;
}
#intro {
opacity: 0;
}
@media (min-width: 600px){
#intro{
opacity: 1;
}
}
<div class="ad">
<div class="item item1" id="item">
<p class="intro" id="intro">Item1 intro text</p>
<p class="price" id="price" style="display: none;">Price</p>
<p class="details" id="details">Learn more</p>
</div>
</div>
When the page is wider than 600px, if the price isn't displayed, the background color changes from gray to blue and the font size will increase.
I'm just trying to figure out how to reset the computed styles if the media rule changes and the original condition is no longer true.
Any insight is much appreciated.