I created a basic CSS box and am attempting to fade it by adjusting its opacity dynamically using the setInterval object.
Click here for the code on jsFiddle
CSS
#box {
margin:0px auto;
margin-top:10px;
height:50px;
width:50px;
background:black;
}
JAVASCRIPT
var fade;
function select(id) {
return document.getElementById(id);
}
function disBox() {
fade=setInterval(function(){
select("box").style.opacity-=0.1;
},300);
if (select("box").style.opacity==0) {
clearInterval("fade");
select("box").style.display="none";
}
else {
select("box").style.display="block";
}
}
The issue arises when the "-=" operator starts subtracting the opacity from 0 rather than 1. I'm seeking clarification on why this behavior occurs. Can someone provide an explanation?