【问题标题】:jQuery Alignment - Relative to Browser Window?jQuery对齐 - 相对于浏览器窗口?
【发布时间】:2019-10-03 15:48:06
【问题描述】:

在各种网站的帮助下,我使用 javascript 创建了一个非常简单的弹出框,其中包含我的联系信息。我对它的工作方式很满意,除了出现的弹出窗口是绝对定位的,我想让它相对于浏览器窗口出现(即我希望弹出窗口出现在浏览器窗口的中心,不管当您单击信息图标时,您在页面上的位置)。

我对 HTML 很满意,但对 javascript 不满意。我知道相对定位在 javascript 中的工作方式非常不同,但我无法弄清楚如何解决这个问题。任何建议将不胜感激。

网页在这里:http://www.thirstlabmedia.com/

脚本如下:

<script type="text/javascript">
function toggle( div_id ) {
    var el = document.getElementById( div_id );
    if( el.style.display == 'none' ) {
        el.style.display = 'block';
    }
    else {
        el.style.display = 'none';
    }
}
function blanket_size( popUpDivVar ) {
    if( typeof window.innerWidth != 'undefined' ) {
        viewportheight = window.innerHeight;
    }
    else {
        viewportheight = document.documentElement.clientHeight;
    }
    if( ( viewportheight > document.body.parentNode.scrollHeight ) && ( viewportheight > document.body.parentNode.clientHeight ) ) {
        blanket_height = viewportheight;
    }
    else {
        if( document.body.parentNode.clientHeight > document.body.parentNode.scrollHeight ) {
            blanket_height = document.body.parentNode.clientHeight;
        }
        else {
            blanket_height = document.body.parentNode.scrollHeight;
        }
    }
    var blanket = document.getElementById( 'blanket' );
    blanket.style.height = blanket_height + 'px';
    var popUpDiv = document.getElementById( popUpDivVar );
    popUpDiv_height = window.innerHeight / 2 - 200;
    popUpDiv.style.top = popUpDiv_height + 'px';
}
function window_pos( popUpDivVar ) {
    if( typeof window.innerWidth != 'undefined' ) {
        viewportwidth = window.innerHeight;
    }
    else {
        viewportwidth = document.documentElement.clientHeight;
    }
    if( ( viewportwidth > document.body.parentNode.scrollWidth ) && ( viewportwidth > document.body.parentNode.clientWidth ) ) {
        window_width = viewportwidth;
    }
    else {
        if( document.body.parentNode.clientWidth > document.body.parentNode.scrollWidth ) {
            window_width = document.body.parentNode.clientWidth;
        }
        else {
            window_width = document.body.parentNode.scrollWidth;
        }
    }
    var popUpDiv = document.getElementById( popUpDivVar );
    window_width = window_width / 2 - 200;
    popUpDiv.style.left = window_width + 'px';
}
function popup( windowname ) {
    blanket_size( windowname );
    window_pos( windowname );
    toggle( 'blanket' );
    toggle( windowname );
}
</script>

(很抱歉将所有内容放在一行上;该网站是通过 Cargo Collective 创建的,除非全部放在一行上,否则它不接受脚本)。

【问题讨论】:

    标签: javascript jquery css popup css-position


    【解决方案1】:

    使用 CSS position : fixed:

    #my-element {
        position : fixed;
        top      : 50%;
        left     : 50%;
        margin   : -100px 0 0 -250px;
        width    : 500px;
        height   : 200px;
        z-index  : 1000;
    }
    

    这是一个演示:http://jsfiddle.net/huRcV/1/

    这会在viewport 中居中一个 500x200px 的元素。负边距用于使元素相对于其尺寸居中。如果用户滚动页面,该元素将保持在viewport 的中心。

    position 的文档:https://developer.mozilla.org/en/CSS/position

    固定

    不要为元素留出空间。相反,将其放置在 相对于屏幕视口的指定位置并且不移动 滚动时。打印时,将其放置在 每一页。

    可以使用 JavaScript 做到这一点,但使用 CSS 版本可能会更好。如果你确实想使用 jQuery,这里有一个简单的例子:

    var $myElement = $('#my-element');
    $(window).on('scroll resize', function () {
        $myElement.css({
            top : ($(this).scrollTop() + ($(this).height() / 2)),
        });
    });
    

    这是一个演示:http://jsfiddle.net/huRcV/(请注意,此演示将position : fixed 更改为position : absolute

    【讨论】:

    • fixed 不受支持 IE8。只是说
    • IE7 支持position : fixed,上周我在一个项目上测试过。 caniuse.com/#search=fixed
    • 奇怪,当我通过 IE7 运行测试时,它对我没有任何帮助。在 IE8 中,该元素变为位置 fixed,但在 IE7 中没有。
    • 似乎是doctype 问题之类的。正在使用什么doctype?在标准中它应该可以正常工作。
    • 我有 IE7 标准(页面默认),但在 jsfiddle 中它似乎正在工作。 jsfiddle.net/huRcV/3。耸肩
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 2015-08-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-21
    • 2017-12-23
    相关资源
    最近更新 更多