Javascript获取浏览器视口大小并输出到主体CSS中

发布于 2024-12-13 12:03:20 字数 1872 浏览 1 评论 0原文

我正在使用 Andy Langton javascript 来获取我的 veiwport 大小。如果您还没有听说过,请参阅下面的链接。

http://andylangton.co.uk/articles/javascript/get- viewport-size-javascript/

该脚本有效,但我不确定如何让脚本将“宽度”和“高度”输出到我的正文 CSS 或任何其他 CSS 选择器中。

请参阅下面我蹩脚的尝试,尽管我认为混合 jQuery 会出错,

<script>

    <!--

    var viewportwidth;
    var viewportheight;

    // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight

    if (typeof window.innerWidth != 'undefined')
    {
        viewportwidth = window.innerWidth,
        viewportheight = window.innerHeight
    }

    // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)

    else if (typeof document.documentElement != 'undefined'
        && typeof document.documentElement.clientWidth !=
        'undefined' && document.documentElement.clientWidth != 0)
    {
        viewportwidth = document.documentElement.clientWidth,
        viewportheight = document.documentElement.clientHeight
    }

    // older versions of IE

    else
    {
        viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
        viewportheight = document.getElementsByTagName('body')[0].clientHeight
    }

    $(document).ready(function() {

        $("body").css({
            width   : "+viewportwidth+",
            height  : "+viewportheight+"
        });

    });

    //-->

</script>

有人可以帮助我解决这个难题吗?

如果有一种方法可以通过 PHP 标签将其添加到正文 html 中,那也很酷,请参见下面我的示例...

<div style="width: <?php veiwportwidth(); ?>px; height: <?php veiwportheight(); ?>px;"> </div>

如果这是有道理的,如果没有也不必担心,只是 javascript 位就会非常有帮助。

提前致谢。

I'm using Andy Langton javascript to get my veiwport size. See link below if you haven't heard of it.

http://andylangton.co.uk/articles/javascript/get-viewport-size-javascript/

The script works but I'm not sure hows the best way to get the script to output the 'width' and 'height' into my body CSS, or any other CSS selector for that matter.

See my lame attempt below, though I think I'm going wrong by mixing in jQuery

<script>

    <!--

    var viewportwidth;
    var viewportheight;

    // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight

    if (typeof window.innerWidth != 'undefined')
    {
        viewportwidth = window.innerWidth,
        viewportheight = window.innerHeight
    }

    // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)

    else if (typeof document.documentElement != 'undefined'
        && typeof document.documentElement.clientWidth !=
        'undefined' && document.documentElement.clientWidth != 0)
    {
        viewportwidth = document.documentElement.clientWidth,
        viewportheight = document.documentElement.clientHeight
    }

    // older versions of IE

    else
    {
        viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
        viewportheight = document.getElementsByTagName('body')[0].clientHeight
    }

    $(document).ready(function() {

        $("body").css({
            width   : "+viewportwidth+",
            height  : "+viewportheight+"
        });

    });

    //-->

</script>

Can any one help me with this conundrum?

If there's a way to add it into body html via PHP tag, that would be cool too, see below my example...

<div style="width: <?php veiwportwidth(); ?>px; height: <?php veiwportheight(); ?>px;"> </div>

If that makes sense, no worry if not, just the javascript bit would be hugely helpful.

Thanks in advance.

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

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

发布评论

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

评论(2

故事灯 2024-12-20 12:03:20

您已经熟悉了 jQuery:

$( 'body' ).css( {
    'width'   : viewportwidth + 'px',
    'height'  : viewportheight + 'px'
} );

您还可以向 DOM 添加样式部分,这对于类重用很有帮助(但在本例中没有那么多):

演示:http://jsfiddle.net/ThinkingStiff/Pa9k3/

脚本:

var bodyStyle =
    'body'
    + '{'
    + 'width: ' + viewportwidth + 'px;'
    + 'height: ' + viewportheight + 'px;'
    + '}';

$( '<style />' ).append( 
    document.createTextNode( bodyStyle )
).appendTo( 'head' );

You were close with your jQuery:

$( 'body' ).css( {
    'width'   : viewportwidth + 'px',
    'height'  : viewportheight + 'px'
} );

You can also add a style section to the DOM, which is helpful with class reuse (but not so much in this case):

Demo: http://jsfiddle.net/ThinkingStiff/Pa9k3/

Script:

var bodyStyle =
    'body'
    + '{'
    + 'width: ' + viewportwidth + 'px;'
    + 'height: ' + viewportheight + 'px;'
    + '}';

$( '<style />' ).append( 
    document.createTextNode( bodyStyle )
).appendTo( 'head' );
风透绣罗衣 2024-12-20 12:03:20

以下是您需要修复的内容:

$("body").css({
    width   : "+viewportwidth+",
    height  : "+viewportheight+"
});

至:

$("body").css({
    "width"   : viewportwidth + "px",
    "height"  : viewportheight + "px"
});

Here is what you need to fix:

$("body").css({
    width   : "+viewportwidth+",
    height  : "+viewportheight+"
});

To:

$("body").css({
    "width"   : viewportwidth + "px",
    "height"  : viewportheight + "px"
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文