问答详情
源自:2-2 JS透明度动画

请问下大神代码中的 alpha += speed;是什么意思?

 <style type="text/css">
        * {
            margin: 0px;
            padding: 0px;
        }

        div {
            position: relative;
            width: 200px;
            height: 200px;
            background-color: red;
            filter: alpha(opacity:30);
            opacity: 0.3;
            margin: 0 auto;
        }

        /*span {
            position: absolute;
            width: 20px;
            height: 50px;
            background-color: blue;
            left: 200px;
            top: 75px;
            font-weight: bold;
            cursor: pointer;
        }*/
    </style>

    <script>
        window.onload = function () {
            var odiv = document.getElementById('div1');
            odiv.onmouseover = function () {
                startMove(100);
            }

            odiv.onmouseout = function () {
                startMove(30);
            }
        }
        var timer = null;
        var alpha = 30;
        function startMove(iTarget) {
            clearInterval(timer);
            timer = setInterval(function () {
                var odiv = document.getElementById('div1');
                var speed = 0;
                if (alpha > iTarget) {
                    speed = -10;
                } else {
                    speed = 10;
                }

                if (alpha == iTarget) {
                    clearInterval(timer);
                }
                else {
                    alpha += speed;
                    odiv.style.opacity = alpha / 100;
                }

            }, 30);
        }

    </script>

提问者:疯狂的boy 2016-12-02 12:05

个回答

  • 古今之道
    2016-12-02 14:24:09
    已采纳

    alpha = alpha + speed; 表示如果当前运动未结束,就加上speed值,直到运动结束。

  • 青湛
    2016-12-02 14:21:09

    alpha = alpha + speed;