我想在该代码中添加一个fadeTo()函数,这样过渡不会立即发生。

// JavaScript Document

function initRollovers() {
if (!document.getElementById) return

var aPreLoad = new Array();
var sTempSrc;
var aImages = document.getElementsByTagName('img');

for (var i = 0; i < aImages.length; i++) {
    if (aImages[i].className == 'imgover') {
        var src = aImages[i].getAttribute('src');
        var ftype = src.substring(src.lastIndexOf('.'), src.length);
        var hsrc = src.replace(ftype, '_o'+ftype);

        aImages[i].setAttribute('hsrc', hsrc);

        aPreLoad[i] = new Image();
        aPreLoad[i].src = hsrc;

        aImages[i].onmouseover = function() {
            sTempSrc = this.getAttribute('src');
            this.setAttribute('src', this.getAttribute('hsrc'));
        }

        aImages[i].onmouseout = function() {
            if (!sTempSrc) sTempSrc = this.getAttribute('src').replace('_o'+ftype, ftype);
            this.setAttribute('src', sTempSrc);
        }
    }
}
}

window.onload = initRollovers;

最佳答案

尝试这个 :

// JavaScript Document
$(window).ready(function () {
initRollovers();
})

function initRollovers() {
if (!document.getElementById) return

var aPreLoad = new Array();
var sTempSrc;
var aImages = document.getElementsByTagName('img');

for (var i = 0; i < aImages.length; i++) {
    if (aImages[i].className == 'imgover') {
        var src = aImages[i].getAttribute('src');
        var ftype = src.substring(src.lastIndexOf('.'), src.length);
        var hsrc = src.replace(ftype, '_o' + ftype);

        aImages[i].setAttribute('hsrc', hsrc);

        aPreLoad[i] = new Image();
        aPreLoad[i].src = hsrc;

        $(aImages[i]).mouseenter(function () {
            sTempSrc = this.getAttribute('src');
            sCurrent = this;
            $(sCurrent).fadeOut(function () {
                this.setAttribute('src', this.getAttribute('hsrc'));
                $(sCurrent).fadeIn();
            });

        })

        $(aImages[i]).mouseleave(function () {
            $(sCurrent).fadeOut(function () {
                if (!sTempSrc) sTempSrc = this.getAttribute('src').replace('_o' + ftype, ftype);
                this.setAttribute('src', sTempSrc);
                $(sCurrent).fadeIn();
            })


        })
    }
}
}

关于jquery - jQuery滚动计时,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14868201/

10-09 13:55