Effect:
Idea:
Use the setInterval timer for movement, offsetWidth to change the width, and use onmouseover to put the end point and the selected DIV into parameters before buffering the movement.
Code:
Copy the code code as follows:
<head runat="server">
<title></title>
<style type="text/css">
div
{
width: 100px;
height: 50px;
background: #0000FF;
margin: 10px;
}
</style>
<script type="text/javascript">
window.onload = function () {
var oDiv = document.getElementsByTagName('div');
for (var i = 0; i < oDiv.length; i++) {
oDiv[i].timer = null; //Mark a DIV to close the timer of the corresponding DIV
oDiv[i].onmouseover = function () {
move(this, 400); //Give timer output parameters
}
oDiv[i].onmouseout = function () {
move(this, 100);
}
}
};
function move(div, end) {
clearInterval(div.timer);
div.timer = setInterval(function () {
var speed = (end - div.offsetWidth) / 5; // (end - width to go) / scaling factor = speed of DIV movement
speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed); // Decimal rounding, that is, rounding with carry
if (div.offsetWidth == end) { //Close the timer when the end is reached
clearInterval(div.timer);
}
else {
div.style.width = div.offsetWidth + speed + 'px'; //Move the width of the DIV
}
}, 30)
}
</script>
</head>
<body>
<div>
</div>
<div>
</div>
<div>
</div>
</body>