javascript - 模态弹出窗口适用于 FireFox,但不适用于其他浏览器

标签 javascript jquery html popup modal-dialog

我开发了一个模式弹出窗口,当单击按钮时,它会生成一个弹出屏幕。

它可以是 AddToBasketButton 单击事件,也可以是打开窗口的 AskqlnkBtn 单击事件。

我的代码在 FireFox 中运行良好,但在 Internet Explorer/Chrome 中运行不佳。

运行代码时没有可见的 JavaScript 错误(据我所知)。

在旧浏览器中单击链接时,屏幕会变暗,因此 loadPopup 必须以某种方式工作。

亲自看看问题:

如果你想体验我网站上的代码,you can see it here. 。如果您点击“Læg i kurv”按钮,那么您可以亲自尝试一下。

有什么想法可能是错误的吗?我已经看了几个小时了,但没有任何线索!

我的代码:

function loadPopup() {
        //loads popup only if it is disabled  
        if ($('#<%=bgPopup.ClientID %>').data("state") == 0) {
            $('#<%=bgPopup.ClientID %>').css({
                "opacity": "0.7"
            });
            $('#<%=bgPopup.ClientID %>').fadeIn("medium");
            $('#<%=ReportError.ClientID%>').fadeIn("medium");
            $('#<%=bgPopup.ClientID %>').data("state", 1);
        }
    }

    function disablePopup() {
        if ($('#<%=bgPopup.ClientID %>').data("state") == 1) {
            $('#<%=bgPopup.ClientID %>').fadeOut("medium");
            $('#<%=ReportError.ClientID %>').fadeOut("medium");
            $('#<%=bgPopup.ClientID %>').data("state", 0);
        }
    }

    function setOrdering() {
        $("#contact-headline").text('Bestil produkt');
        $("#contact-messagelbl").text('Evt. kommentar');
        $('#<%=ContactTypeHiddenLbl.ClientID %>').val("bestil");
    }

    function setQuestions() {
        $("#contact-headline").text('Stil spørgsmål');
        $("#contact-messagelbl").text('Indtast dit spørgsmål');
        $('#<%=ContactTypeHiddenLbl.ClientID %>').val("spørgsmål");
    }

    function centerPopup() {
        $('#<%=ReportError.ClientID%>').center();
    }

    function resetContactControls() {
        $('#<%=ContactMailBox.ClientID %>').val('');
        $('#<%=ContactPhoneBox.ClientID %>').val('');
        $('#<%=ReportMessageBox.ClientID %>').val('');
        $('#<%=AskQuestionProductBtn.ClientID %>').show();
        $('#contact-statuslbl').val('');
    }

    jQuery.fn.center = function () {
        this.css("position", "absolute");
        this.css("top", Math.max(0, (($(window).height() - this.outerHeight()) / 2) +
            $(window).scrollTop()) + "px");
        this.css("left", Math.max(0, (($(window).width() - this.outerWidth()) / 2) +
            $(window).scrollLeft()) + "px");
        return this;
    };

    $(document).ready(function() {
        var mouseIsInside = true;

        $('#<%=ReportError.ClientID%>').hover(function () {
            mouseIsInside = true;
        }, function () {
            mouseIsInside = false;
        });

        $("body").mouseup(function () {
            if (!mouseIsInside) {
                disablePopup();
            }
        });

    });

        $('#<%=bgPopup.ClientID %>').data("state", 0);

        $('#<%=AddToBasketButton.ClientID %>').click(function () {
            resetContactControls();
            centerPopup();
            loadPopup();
            setOrdering();
        });

        $('#<%=AskqlnkBtn.ClientID %>').click(function () {
            resetContactControls();
            centerPopup();
            loadPopup();
            setQuestions();
        });

        $('#<%=PopupCloseLnk.ClientID %>').click(function () {
            disablePopup();
        });

        $(document).keypress(function (e) {
            if (e.keyCode == 27) {
                disablePopup();
            }
        });


    $(window).resize(function () {
        centerPopup();
    });

最佳答案

问题是由您放置元素的方式引起的。您将其定位为就好像它应该被修复一样。尝试使用它作为中心代码。

jQuery.fn.center = function () {
    this.css("position", "fixed");
    this.css("top", Math.max(0, (($(window).height() - this.outerHeight()) / 2) + "px");
    this.css("left", Math.max(0, (($(window).width() - this.outerWidth()) / 2) + "px");
    return this;
};

我更改为position:fixed并删除了scrollTop。您不希望它绝对定位,因为这意味着它会随着页面滚动。

编辑:如果您仍然无法使其正常工作,请使用开发工具检查元素和相关样式以查看发生了什么,或者将其添加到函数中以查看您的情况topleft 设置为:

alert(Math.max(0, (($(window).height() - this.outerHeight()) / 2) + "px");
alert(Math.max(0, (($(window).width() - this.outerWidth()) / 2) + "px");

关于javascript - 模态弹出窗口适用于 FireFox,但不适用于其他浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15452127/

相关文章:

javascript - 无法读取 null 的属性 'getElementsByTagName'

javascript - 当python仍在运行时在node.js上显示python数据

javascript - Promise 链中的 .then(console.log()) 和 .then(() => console.log()) 在执行上有何不同

javascript - 禁用 NgRepeat 中 NgOptions 中的选项

javascript - jquery-3-3-1 中已弃用的 .live() 语句的正确替代是什么

javascript - 验证 (HTML5) : Attribute 'X' is not a valid attribute of element 'Y'

javascript - 已为单个图像创建点击效果,但希望其他图像具有相同效果

javascript - 苹果浏览器 : drag on element with click handler sometimes triggers click outside of element

javascript - bootstrap-wysihtml5 集成

javascript - 使用 JavaScript 仅替换 DIV 中的文本内容