将 DIV 居中并根据浏览器尺寸进行调整

发布于 2024-12-18 07:39:11 字数 502 浏览 0 评论 0原文

我正在构建一个网页来嵌入流媒体视频。我想将视频水平和垂直居中,同时调整视频大小(基于浏览器大小,而不是手动)以填充可用的屏幕空间。

这是棘手的部分;此页面的导航高度为 60px,视频必须为 16:9。我希望能够将最小宽度和最小高度设置为 800px 宽度和 450px 高度。视频不应与导航重叠,因此如果垂直方向稍微偏离中心也没关系。如果需要,我可以使用 jQuery,尽管 CSS(3) 更好。这个页面主要供私人使用,所以我不太担心跨浏览器兼容性。

我不知道如何在如此复杂的情况下实现这一目标。我的想法是创建一个填充页面其余部分的 div,并以绝对定位将其置于该 div 的中心。不幸的是,我不知道如何调整其大小并保持正确的宽高比。如果有帮助,我将在这个项目中同时使用 UStream 和 Justin.tv。

谢谢。

编辑1:我不知道如何使底部 div 填充页面的其余部分,同时减去导航。我认为 height:100%; 会覆盖它,但不幸的是,它覆盖了整个页面的 100%,包括 60px 导航。

I'm building a web page to embed a streaming video in. I want to center the video both horizontally and vertically, while resizing (based on browser size, not manually) the video to fill the available screen real estate.

Here's the tricky part; This page has a navigation with 60px height, and the video must be in 16:9. I would like to be able to set a min-width and min-height of 800px width and 450px height. The video should not be able to overlap the navigation, so if it's a bit off center vertically, that's fine. I can use jQuery if necessary, although CSS(3) would be preferable. This page is mostly for private use, so I'm not worried too much about cross-browser compatibility.

I have no idea how to accomplish this at this level of complexity. My thoughts are to create a div that fills the rest of the page, and center it within that div with absolute positioning. Unfortunately, I don't know how to make it resizable and stay in the correct aspect ratio. If it helps, I'll be using both UStream and Justin.tv for this project.

Thanks.

EDIT 1: I can't figure out how to make the bottom div fill the rest of the page while subtracting the navigation. I thought height:100%; would cover it, but unfortunately that does the entire 100% of the page, including the 60px navigation.

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

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

发布评论

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

评论(3

放赐 2024-12-25 07:39:11

使用此 jQuery 插件: FitVids

水平和垂直居中 CSS3 Flexbox (有限的浏览器支持,但正如你所说,这不是问题)

这是代码(没有供应商前缀)简化):

.parent{
      display: box;
      box-orient: horizontal;
      box-pack: center;
      box-align: center;
}

将其应用于视频的父级。

现在,导航不应中断居中,但在这种情况下,您始终可以绝对定位导航栏。顶部会有一些视频被剪掉,但 60 像素还不错。

Use this jQuery plugin: FitVids

Horizontal and vertical centering with CSS3 Flexbox (limited browser support, but as you said, that is not a problem)

Here is the code (without vender prefixes to simplify):

.parent{
      display: box;
      box-orient: horizontal;
      box-pack: center;
      box-align: center;
}

Apply that to the parent of the video.

Now the navigation shouldn't interrupt the centering, but in the case that it does, you can always absolutely position the navigation bar. There will be some video clipped at the top, but 60px isn't that bad.

孤独患者 2024-12-25 07:39:11

试试这个代码:

<div class="greenBorder" style="display: table; height: 400px; #position: relative; overflow: hidden;">
    <div style=" #position: absolute; #top: 50%;display: table-cell; vertical-align: middle;">
      <div class="greenBorder" style=" #position: relative; #top: -50%">
        any text<br>
        any height<br>
        any content, for example generated from DB<br>
        everything is vertically centered
      </div>
    </div>
  </div>

try this code:

<div class="greenBorder" style="display: table; height: 400px; #position: relative; overflow: hidden;">
    <div style=" #position: absolute; #top: 50%;display: table-cell; vertical-align: middle;">
      <div class="greenBorder" style=" #position: relative; #top: -50%">
        any text<br>
        any height<br>
        any content, for example generated from DB<br>
        everything is vertically centered
      </div>
    </div>
  </div>
浅紫色的梦幻 2024-12-25 07:39:11

这是我的做法:

var videoElement = $("#IDofYourVideo");

function SizeUpVideo(elm) {
    var W = parseInt($(window).width()),   //get browser width
        H = parseInt($(window).height()),  //get browser height
        ratio = 16/9,                      //set ratio
        videoH = H-60,                     //subtract 60px
        videoW = H * ratio;                //set width according to ratio

    if (videoW > W) {videoW=W; videoH=W*(1/ratio);} //if width is more than screen, do it the other way around
    if (videoW >= '800' || videoH >= '450') {
        elm.css({top: (H-videoH+60)/2, left: (W-videoW)/2, height: videoH, width: videoW});
    } else {
        elm.css({top: 60, height: 450, width: 800});
    }
};

SizeUpVideo(videoElement);

$(window).bind("resize", function() {
    SizeUpVideo(videoElement);
});

小提琴:http://jsfiddle.net/bAXwK/

Here's how I would do it :

var videoElement = $("#IDofYourVideo");

function SizeUpVideo(elm) {
    var W = parseInt($(window).width()),   //get browser width
        H = parseInt($(window).height()),  //get browser height
        ratio = 16/9,                      //set ratio
        videoH = H-60,                     //subtract 60px
        videoW = H * ratio;                //set width according to ratio

    if (videoW > W) {videoW=W; videoH=W*(1/ratio);} //if width is more than screen, do it the other way around
    if (videoW >= '800' || videoH >= '450') {
        elm.css({top: (H-videoH+60)/2, left: (W-videoW)/2, height: videoH, width: videoW});
    } else {
        elm.css({top: 60, height: 450, width: 800});
    }
};

SizeUpVideo(videoElement);

$(window).bind("resize", function() {
    SizeUpVideo(videoElement);
});

Fiddle : http://jsfiddle.net/bAXwK/

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