12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- <!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: 300px;
- height: 50px;
- background-color: red;
- }
- </style>
- </head>
- <body>
- <div class="box"></div>
- <script>
- var oBox = document.getElementsByClassName("box")[0];
- var timer1 = null;
- var timer2 = null;
- // 鼠标移入控制元素不断变长
- oBox.onmouseenter = function(){
- clearInterval(timer2);
- timer1 = setInterval(function(){
- oBox.style.width = (oBox.offsetWidth+5)+"px";
- if(oBox.offsetWidth>500){
- clearInterval(timer1);
- }
- },16)
- }
- // 鼠标移出后控制元素不断变短
- oBox.onmouseleave = function(){
- clearInterval(timer1);
- timer2 = setInterval(function(){
- oBox.style.width = (oBox.offsetWidth-5) + "px";
- if(oBox.offsetWidth<300){
- clearInterval(timer2)
- }
- },16)
- }
- </script>
- </body>
- </html>
|