JavaScript渐入渐出

window.onload = function() {
    var oImg = document.getElementById('img1');

    //鼠标移开
    oImg.onmouseover = function() {
        //透明度
        startMove(100);
    }
    //鼠标移入
    oImg.onmouseout = function() {
        //透明度
        startMove(30);
    }
}
var timer = null;

var alpha = 30;

function startMove(iTarget) {
    var oImg = document.getElementById('img1');

    clearInterval(timer);
    timer = setInterval(function() {
        var iSpeed = 0;

        if (alpha < iTarget) {
            iSpeed = 1;
        } else {
            iSpeed = -1;
        }

        if (alpha == iTarget) {
            clearInterval(timer);
        } else {
            alpha += iSpeed;

            oImg.style.filter = 'alpha(opacity:' + alpha + ')';
            oImg.style.opacity = alpha / 100;

            document.title = alpha;
        }
    },
    30);
}


<img id="img1" src="img/360截图1633010710411099.png" />

猜你喜欢

转载自blog.csdn.net/qq_37995109/article/details/85178184