| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- <style>
- .box {
- width: 200px;
- height: 50px;
- background-color: red;
- margin-top: 20px;
- }
- </style>
- </head>
- <body>
- <div class="box"></div>
- <div class="box"></div>
- <div class="box"></div>
- <script>
- var aBox = document.getElementsByClassName("box");
- var timer1 = null;
- for (var i = 0; i < aBox.length; i++) {
- aBox[i].index = i;
- aBox[i].onmouseover = function () {
- var _index = this.index;
- clearInterval(aBox[_index].timer2)
- aBox[_index].timer1 = setInterval(function () {
- var _width = aBox[_index].offsetWidth;
- if (_width >= 600) {
- clearInterval(aBox[_index].timer1)
- } else {
- _width += 10;
- aBox[_index].style.width = _width + "px";
- }
- }, 16)
- }
- aBox[i].onmouseout = function () {
- var _index = this.index;
- clearInterval(aBox[_index].timer1)
- aBox[_index].timer2 = setInterval(function () {
- var _width = aBox[_index].offsetWidth;
- if (_width <= 200) {
- clearInterval(aBox[_index].timer2)
- } else {
- _width -= 10;
- aBox[_index].style.width = _width + "px";
- }
- }, 16)
- }
- }
- </script>
- </body>
- </html>
|