I have a problem with transition property. Namely, it works only after first time. At the first time there is no transition effect.
JS code:
document.addEventListener("DOMContentLoaded", function() {
var second = document.querySelector('.square');
if(second) {
second.addEventListener("mouseenter", function() {
var maxX = Math.floor(Math.random() * (window.innerWidth - 200));
var maxY = Math.floor(Math.random() * (window.innerHeight - 200));
this.style.left = maxX + 'px';
this.style.top = maxY + 'px';
this.style.transition = 'left 2s, top 2s';
});
}
});
CSS code:
* {
margin: 0;
padding: 0;
}
main {
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
position: relative;
}
.second {
height: 200px;
width: 200px;
position: absolute;
display: flex;
justify-content: center;
align-items: center;
}
EDIT:
UPDATE:
Moving transition to CSS doesn't change anything. Square has to be centered and can't leave the window.