Learning website development (HTML, CSS, JS, PHP, yadda yadda). I am trying to create a hover effect on an element via CSS, however after a JS function has set a value, it seems I cannot override without using the !important.
I have a scroll function in JS that sets opacity/color/etc. after the user has scrolled down. As it seems that JS affect the elements inline style directly, it will always have a specificity higher than anything in my stylesheet. As such, my :hover effect (increasing the opacity of the button) will be overwritten.
window.onscroll = function() {arcScrollEffect()}; // calls scroll function on scrolling
function arcScrollEffect() {
btn = document.getElementsByClassName("arcHeaderBtn")[0];
if (document.body.scrollTop > 50 || document.documentElement.scrollTop) {
btn.style.width = '20%';
btn.style.fontSize = '2em';
btn.style.backgroundColor = '#DDD';
btn.style.color = 'rgba(0, 0, 0, 1)';
btn.style.opacity = '0.3';
} else {
btn.innerHTML = 'RED';
}
}
.arcHeaderBtn {
background-color: inherit;
border: none;
position: fixed;
top: 10vh;
left: 0;
width: 100%;
font-size: 6em;
text-align: center;
opacity: 0.5;
border-radius: 0 0.6em 0.6em 0;
transition: color 1s, background-color 1s, width 1s, top 1s, font-size 1s, opacity 1s;
}
button.arcHeaderBtn:hover {
opacity: 0.5 !important;
}
<button class="arcHeaderBtn">Button</button>
Note that in this particular case I do have a workarounds, such as simply not setting opacity in JS or using the !important in CSS (which works fine). However, I've been told that !important is not good coding, something to the equivalent of a GOTO in older languages and I fear velociraptor attacks. Any suggestions?