根据浏览器大小/调整大小更改高度

发布于 2024-07-19 02:50:37 字数 99 浏览 1 评论 0原文

我想知道是否有办法确定浏览器的高度/宽度。 我想要做的是,当浏览器尺寸为 1024x768 时,将 div 的高度设置为 500px,对于任何较低的高度,我想将其设置为 400px。

I was wondering if there was a way to determine the height/width of a browser.
What i'm trying to do is set a height on a div to 500px when the browser size is 1024x768, and for anything lower i'd like to set it to 400px.

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

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

发布评论

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

评论(5

洋洋洒洒 2024-07-26 02:50:37

如果您使用的是 jQuery 1.2 或更新版本,您可以简单地使用这些:

$(window).width();
$(document).width();
$(window).height();
$(document).height();

从那里决定元素的高度是一件简单的事情。

If you are using jQuery 1.2 or newer, you can simply use these:

$(window).width();
$(document).width();
$(window).height();
$(document).height();

From there it is a simple matter to decide the height of your element.

夜灵血窟げ 2024-07-26 02:50:37
$(function(){
    $(window).resize(function(){
        var h = $(window).height();
        var w = $(window).width();
        $("#elementToResize").css('height',(h < 768 || w < 1024) ? 500 : 400);
    });
});

滚动条等会对窗口大小产生影响,因此您可能需要调整到所需的大小。

$(function(){
    $(window).resize(function(){
        var h = $(window).height();
        var w = $(window).width();
        $("#elementToResize").css('height',(h < 768 || w < 1024) ? 500 : 400);
    });
});

Scrollbars etc have an effect on the window size so you may want to tweak to desired size.

明月夜 2024-07-26 02:50:37

基于 Chad 的答案,您还希望将该函数添加到 onload 事件中,以确保在页面加载时也调整其大小。

jQuery.event.add(window, "load", resizeFrame);
jQuery.event.add(window, "resize", resizeFrame);

function resizeFrame() 
{
    var h = $(window).height();
    var w = $(window).width();
    $("#elementToResize").css('height',(h < 768 || w < 1024) ? 500 : 400);
}

Building on Chad's answer, you also want to add that function to the onload event to ensure it is resized when the page loads as well.

jQuery.event.add(window, "load", resizeFrame);
jQuery.event.add(window, "resize", resizeFrame);

function resizeFrame() 
{
    var h = $(window).height();
    var w = $(window).width();
    $("#elementToResize").css('height',(h < 768 || w < 1024) ? 500 : 400);
}
洒一地阳光 2024-07-26 02:50:37

注意,Jquery 1.8.0 有一个错误,$(window).height() 返回所有文档的高度!

Pay attention, there is a bug with Jquery 1.8.0, $(window).height() returns the all document height !

究竟谁懂我的在乎 2024-07-26 02:50:37

我感觉支票应该是不同的

新的:
h< 第768章 w< 1024

I have the feeling that the check should be different

new:
h < 768 || w < 1024

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