*{ margin: 0;}
#box{ height: 100px;width: 100px;background: red; position: absolute; top: 200px; left: 200px; opacity: 0.3;}
<div id="box" onmouseover="boxmove(1)" onmouseout="boxmove(0)"> </div>
let box =document.getElementById("box");
let time = null;1
function boxmove(a){
clearInterval(time);
time = setInterval(function(){
let opc = a>getComputedStyle(box,false)["opacity"]?0.05:-0.05;
box.style.opacity = Number(getComputedStyle(box,false)["opacity"])+opc;
},50)
}