WP7 - 如何使用 javascript 在网络浏览器内垂直居中 div

发布于 2024-10-26 02:08:46 字数 1248 浏览 1 评论 0原文

我在 WP7 论坛上尝试过,但一无所获...

我正在生成一个相当简单的 html 文档,其所有内容都在一个 div 中。我需要将该 div 在网络浏览器控件中垂直居中。通常的方法是外部 div 带有“display: table”,内部 div 带有“display: table-cell”,这在 WP7-IE 中不起作用,所以我使用了 javascript。这个非常简单的 js 函数适用于桌面浏览器:

    function setContent() {
        var windowHeight = document.documentElement.clientHeight;
        if (windowHeight > 0) {
            var contentElement = document.getElementsByClassName('DivToCenter')[0];
            var contentHeight = contentElement.offsetHeight;

            if (windowHeight - contentHeight > 0) {
                contentElement.style.position = 'relative';
                contentElement.style.top = ((windowHeight / 2) - (contentHeight / 2)) + 'px';
                window.external.Notify('' + ((windowHeight / 2) - (contentHeight / 2)));
            }
            else {
                contentElement.style.position = 'static';
            }
        }
    }
    window.onload = function() {
        setContent();
    }
    window.onresize = function() {
        setContent();
    }

该函数的结果是 div 在 Web 浏览器控件中显得太低,就好像视口的高度比实际高度更高。我从 JavaScript 中提取了视口的大小,并注意到它与 WebBrowser.ActualHeight 属性相同。所以,我现在的工作假设是,报告给 javascript 的视口大小以 xaml 逻辑像素为单位,这会扰乱算术。这是我对正在发生的事情的最佳理论。

I tried this on the WP7 forums and got nothing...

I am generating a rather simple html document, with all of its contents in a div. I need to vertically center that div in a webbrowser control. The usual approach of an outer div with "display: table" and an inner div with "display: table-cell" doesn't work in WP7-IE, so I went with javascript. This very simple js function works on desktop browsers:

    function setContent() {
        var windowHeight = document.documentElement.clientHeight;
        if (windowHeight > 0) {
            var contentElement = document.getElementsByClassName('DivToCenter')[0];
            var contentHeight = contentElement.offsetHeight;

            if (windowHeight - contentHeight > 0) {
                contentElement.style.position = 'relative';
                contentElement.style.top = ((windowHeight / 2) - (contentHeight / 2)) + 'px';
                window.external.Notify('' + ((windowHeight / 2) - (contentHeight / 2)));
            }
            else {
                contentElement.style.position = 'static';
            }
        }
    }
    window.onload = function() {
        setContent();
    }
    window.onresize = function() {
        setContent();
    }

The result of this function is that the div appears too low in the webbrowser control, as if the viewport had a greater height than it actually does. I pulled the size of the viewport out of the javascript, and noticed that its identical to the WebBrowser.ActualHeight property. So, my working hypothesis right now is that the size of the viewport reported to the javascript is in xaml logical pixels, which messes up the arithmetic. Thats my best theory about what's hapenning.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巨坚强 2024-11-02 02:08:46

您可能应该看一下这样的帖子:http://blogs.msdn.com/b/iemobile/archive/2011/01/21/managing-the-browser-viewport-in-windows-phone- 7.aspx,讨论了WP7中的视口大小。

You should probably take a look at a post like this one: http://blogs.msdn.com/b/iemobile/archive/2011/01/21/managing-the-browser-viewport-in-windows-phone-7.aspx, which discusses the viewport size in WP7.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文