JS implementation of multi object buffer motion instance code

  • 2020-03-30 00:40:03
  • OfStack

Effect:

< img border = 0 SRC = "/ / img.jbzj.com/file_images/article/201311/20131129164920343.gif" >

Ideas:

Using the setInterval timer to move, offsetWidth to change the width, and using onmouseover to buffer the end point and the selected DIV into the parameters.

Code:


<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;           //Buy a DIV to make a tag to close the corresponding DIV timer
                oDiv[i].onmouseover = function () {
                    move(this, 400);        //Output parameters to timer
                }
                oDiv[i].onmouseout = function () {
                    move(this, 100);
                }
            }
        };
        function move(div, end) {
            clearInterval(div.timer);
            div.timer = setInterval(function () {
                var speed = (end - div.offsetWidth) / 5;        //(endpoint - width to travel)/scaling factor =DIV speed
                speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);   //If you round the decimal, you round the carry
                if (div.offsetWidth == end) {       //Turn off the timer when you reach the finish line
                    clearInterval(div.timer);
                }
                else {
                    div.style.width = div.offsetWidth + speed + 'px';   //Move the width of DIV
                }
            }, 30)
        }
    </script>
</head>
<body>
    <div>
    </div>
    <div>
    </div>
    <div>
    </div>
</body>


Related articles: