Javascript只在firefox中不起作用

时间:2013-03-18 16:38:02

标签: javascript firefox mozilla onmouseover

我有一个将鼠标悬停在缩略图上的脚本,它会显示放大的图像。这项工作在IE,Chrome和Safari中都很好用。但在Firefox中,它无法正常工作。它将显示图像,但不会正确地悬停在图像旁边。它将保留在页面上的绝对位置,而不是遵循真正的身体价值。它应该位于IE或Chrome中的固定位置。

我想知道是否需要添加Mozilla或Firefox特定的异常。这是我的代码:

//  Simple Image Trail script- By JavaScriptKit.com
var offsetfrommouse = [15, 10]; //image x,y offsets from cursor position in pixels.
var myimageheight = 250;
var myimagewidth = 250;
if (document.getElementById || document.all) {
    document.write('<div id="DynPreviewPlace"></div>');
}

function gettrailobj() {
    if (document.getElementById)
        return document.getElementById("DynPreviewPlace").style
    else if (document.all)
        return document.all.DynPreviewPlace.style
}

function gettrailobjnostyle() {
    if (document.getElementById)
        return document.getElementById("DynPreviewPlace")
    else if (document.all)
        return document.all.DynPreviewPlace
}

function truebody() {
    if (window.getComputedStyle && !window.globalStorage && !window.opera) {
        return (!window.chrome && window.getComputedStyle && document.compatMode != "CSS1Compat") ? document.documentElement : document.body
    } else if () {} else {
        return (!window.chrome && document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body
    }
}

function showtrail(imagename, title, width, height) {
    document.onmousemove = followmouse;
    (height == 0) ? height = myimageheight : '';
    width += 15
    height += 30
    myimageheight = height
    myimagewidth = width
    newHTML = '<div class="DynPreviewWraper" style="width:' + width + 'px;"><div id="DynPreviewContainer"><div class="DynPreviewLoader"><div align="center">Loading preview...</div><div class="DynPreviewLoaderBg"><div id="DynProgress"> </div></div></div></div>';
    newHTML = newHTML + '<h2 class="DynPreviewTitle">' + ' ' + title + '</h2>'
    newHTML = newHTML + '<img onload="javascript:remove_loading();" src="' + imagename + '" class="DynPreviewTempLoad" alt="" />';
    newHTML = newHTML + '<!--[if lte IE 6.5]><iframe></iframe><![endif]--></div>';
    gettrailobjnostyle().innerHTML = newHTML;
    gettrailobj().display = "block";

}

function hidetrail() {
    gettrailobj().innerHTML = " ";
    gettrailobj().display = "none"
    document.onmousemove = ""
    gettrailobj().left = "-500px"
}

function followmouse(e) {
    var xcoord = offsetfrommouse[0]
    var ycoord = offsetfrommouse[1]
    var docwidth = document.all ? truebody().scrollLeft + truebody().clientWidth : pageXOffset + window.innerWidth - 15
    var docheight = document.all ? Math.min(truebody().scrollHeight, truebody().clientHeight) : Math.min(window.innerHeight)
    if (typeof e != "undefined") {
        if (docwidth - e.pageX < myimagewidth + 2 * offsetfrommouse[0]) {
            xcoord = e.pageX - xcoord - myimagewidth;
        } else {
            xcoord += e.pageX;
        }
        if (docheight - e.pageY < (myimageheight + 110)) {
            ycoord += e.pageY - Math.max(0, (110 + myimageheight + e.pageY - docheight - truebody().scrollTop));
        } else {
            ycoord += e.pageY;
        }
    } else if (typeof window.event != "undefined") {
        if (docwidth - event.clientX < myimagewidth + 2 * offsetfrommouse[0]) {
            xcoord = event.clientX + truebody().scrollLeft - xcoord - myimagewidth;
        } else {
            xcoord += truebody().scrollLeft + event.clientX
        }
        if (docheight - event.clientY < (myimageheight + 110)) {
            ycoord += event.clientY + truebody().scrollTop - Math.max(0, (110 + myimageheight + event.clientY - docheight));
        } else {
            ycoord += truebody().scrollTop + event.clientY;
        }
    }
    var docwidth = document.all ? truebody().scrollLeft + truebody().clientWidth : pageXOffset + window.innerWidth - 15
    var docheight = document.all ? Math.max(truebody().scrollHeight, truebody().clientHeight) : Math.max(document.body.offsetHeight, window.innerHeight)
    if (ycoord < 0) {
        ycoord = ycoord * -1;
    }
    gettrailobj().left = xcoord + 'px';
    gettrailobj().top = ycoord + 'px';
}
var t_id = setInterval(animate, 20);
var pos = 0;
var dir = 2;
var len = 0;

function animate() {
    var elem = document.getElementById('DynProgress');
    if (elem != null) {
        if (pos == 0) len += dir;
        if (len > 32 || pos > 79) pos += dir;
        if (pos > 79) len -= dir;
        if (pos > 79 && len == 0) pos = 0;
    }
}

function remove_loading() {
    this.clearInterval(t_id);
    var targelem = document.getElementById('DynPreviewContainer');
    targelem.style.display = 'none';
    targelem.style.visibility = 'hidden';
    var t_id = setInterval(animate, 60);
}

这让我发疯了。我一直在尝试一切。您可以在此处查看问题所在的页面:https://www.woodenduckshoppe.com/shoppe/christmas-decorations/

1 个答案:

答案 0 :(得分:0)

  

您的truebody函数在不同的代码中运行不同的代码   浏览器。你有没有尝试过那样做? - Boris Zbarsky 15小时前

我一直想弄清楚要写些什么。我只是不知道要为firefox / mozilla添加什么。这是唯一有问题的浏览器。

我发现在Firefox中没有阅读真人,我只是不知道为什么或如何修复它。