Div是根据窗口高度动态调整大小的,但在HTML中添加更多内容时失败
Div is dynamically sized to window height, but fails when more content is added to HTML
我有一个HTML5视频的网站,我的客户想要完全填充窗口(类似于这个网站))。我已经完成了这与CSS (object-fit: cover
, width: 100%
)和JQuery(容器部分的高度设置为窗口高度,在窗口调整大小重置)。
但是当我在视频下面添加另一个包含文本的section元素时(同样,类似于上面链接的站点),它就像JQuery甚至不存在一样。视频容器部分的高度大于它应有的高度,但不是默认的视频高度。另外,带文本的新部分被放在视频后面,所以它可能是半可见的。
这是怎么回事?很明显,新的部分在某种程度上搞乱了窗口高度的计算,但我不知道为什么或如何修复它。
$(document).ready(function() {
// Get window dimensions
var winWidth = $(window).width();
var winHeight = $(window).height();
var winRatio = winWidth / winHeight;
console.log("The window height is: " + winHeight);
console.log("The window width is: " + winWidth);
console.log("The window ratio is: " + winRatio);
// Get window dimensions again upon resize
$(window).resize(function() {
winWidth = $(window).width();
winHeight = $(window).height();
console.log("The window height is: " + winHeight);
console.log("The window width is: " + winWidth);
});
// Get video height (width will always be equal to window width)
var vidHeight = $("#home-video").height();
console.log("The video height is: " + vidHeight);
// Get video height again upon resize
$(window).resize(function() {
vidHeight = $("#home-video").height();
console.log("The video height is: " + vidHeight);
});
// If screen is at least 992px wide (most desktops), load video with page
if (winWidth > 992) {
$("video#home-video").attr("preload", "auto");
}
sizeToWindow(vidHeight, winHeight);
$(window).resize(function() {
sizeToWindow(vidHeight, winHeight);
})
});
function sizeToWindow(vidHeight, winHeight) {
// If screen height and header-vid section height are not equal, set section height to window height to fill screen
if (winHeight !== vidHeight) {
var heightString = winHeight.toString();
heightString = heightString.concat("px");
$("#header-vid").css({
"height": heightString
});
}
}
main {
margin: 0;
padding: 0;
position: absolute;
top: 0;
width: 100%;
z-index: 1;
}
main section.full-width {
width: 100%;
margin: 0;
padding: 0;
}
main .video-container {
margin: 0;
padding: 0;
background-image: url("/resources/img/atlanta-skyline.jpg");
background-image: url("/resources/img/atlanta-skyline.jpg");
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
main video#home-video {
width: 100%;
height: 100%;
object-fit: cover;
}
@media screen and (max-width: 991px) {
main video#home-video {
opacity: 0;
}
}
main h1.overlaid-text {
font-family: Merriweather, serif !important;
position: absolute;
color: white;
font-size: 5em;
top: 20%;
left: 30%;
opacity: 0.9;
}
main section#vision-container {
background: #f7a11a;
background: #f7a11a;
color: white;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<main>
<section class="full-width full-height video-container" id="header-vid">
<video autoplay loop preload="none" poster="/resources/img/atlanta-skyline.jpg" id="home-video">
<source src="/resources/vid/main-vid.mp4" type="video/mp4">
</video>
</section>
<section class="full-width" id="vision-container">
<h3>Within the next 5 years, we will grow to originate $400 million dollars a month in purchase money mortgages across 20 markets in the United States. In those markets we will be recognized as innovators of mortgages processing, supporteres of consumer transparency while maintaining our reputation as the mortgage bank with the highest employee satisfation among our peers.</h3>
</section>
</main>
我强烈建议摆脱<main>
和<section>
标签,因为它们与video
标签不友好。正如您在这段代码中看到的,我用简单的divs
修改了它,它工作得很好。
$(document).ready(function() {
// Get window dimensions
var winWidth = $(window).width();
var winHeight = $(window).height();
var winRatio = winWidth / winHeight;
console.log("The window height is: " + winHeight);
console.log("The window width is: " + winWidth);
console.log("The window ratio is: " + winRatio);
// Get window dimensions again upon resize
$(window).resize(function() {
winWidth = $(window).width();
winHeight = $(window).height();
console.log("The window height is: " + winHeight);
console.log("The window width is: " + winWidth);
});
// Get video height (width will always be equal to window width)
var vidHeight = $("#home-video").height();
console.log("The video height is: " + vidHeight);
// Get video height again upon resize
$(window).resize(function() {
vidHeight = $("#home-video").height();
console.log("The video height is: " + vidHeight);
});
// If screen is at least 992px wide (most desktops), load video with page
if (winWidth > 992) {
$("video#home-video").attr("preload", "auto");
}
sizeToWindow(vidHeight, winHeight);
$(window).resize(function() {
sizeToWindow(vidHeight, winHeight);
})
});
function sizeToWindow(vidHeight, winHeight) {
// If screen height and header-vid section height are not equal, set section height to window height to fill screen
if (winHeight !== vidHeight) {
var heightString = winHeight.toString();
heightString = heightString.concat("px");
$("#header-vid").css({
"height": heightString
});
}
}
.overlay {
position:absolute;
bottom:0;
left:0;
z-index:1;
}
.main {
margin: 0;
padding: 0;
position: absolute;
top: 0;
width: 100%;
z-index: 1;
}
.main .section .full-width {
width: 100%;
margin: 0;
padding: 0;
}
.main .video-container {
margin: 0;
padding: 0;
background-image: url("http://georgiainfo.galileo.usg.edu/images/uploads/gallery/atlanta1976.jpg");
background-image: url("http://georgiainfo.galileo.usg.edu/images/uploads/gallery/atlanta1976.jpg");
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
.main video#home-video {
width: 100%;
height: 100%;
object-fit: cover;
}
@media screen and (max-width: 991px) {
main video#home-video {
opacity: 0;
}
}
.main h1.overlaid-text {
font-family: Merriweather, serif !important;
position: absolute;
color: white;
font-size: 5em;
top: 20%;
left: 30%;
opacity: 0.9;
}
.main section#vision-container {
background: #f7a11a;
background: #f7a11a;
color: white;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="main">
<div class="section full-width full-height video-container" id="header-vid">
<video autoplay loop preload="none" poster="http://georgiainfo.galileo.usg.edu/images/uploads/gallery/atlanta1976.jpg" id="home-video">
<source src="http://techslides.com/demos/sample-videos/small.mp4" type="video/mp4">
</video>
</div>
<div class="section full-width overlay" id="vision-container">
<h3>Within the next 5 years, we will grow to originate $400 million dollars a month in purchase money mortgages across 20 markets in the United States. In those markets we will be recognized as innovators of mortgages processing, supporteres of consumer transparency while maintaining our reputation as the mortgage bank with the highest employee satisfation among our peers.</h3>
</div>
</div>
相关文章:
- 使用jQuery根据动态生成的html动态添加html
- 如何使用javascript选择字符串的部分,添加html标记或删除部分
- 单击按钮添加HTML表单
- 如何添加html<br>标记转换为javascript
- 动态添加 HTML 元素后创建 jQuery 事件
- 在具有特定ID的DIV中使用JavaScript动态添加HTML元素
- 在前面的输入元素之前添加html输入元素
- 程序添加html后点击不工作
- 使用appendchild或其他方法向ul添加html代码而不是纯文本
- 如何在JS函数中添加HTML代码
- Jquery从另一个页面添加HTML
- 如何在不添加html脚本标记的情况下使用google-maps-api-v3
- 正在尝试向jQuery对象添加HTML类属性
- 使用“添加用户”按钮添加HTML字段
- 在 JavaScript 中的 textNode 中添加 html
- jquery动态添加HTML字段遇到了神秘的问题
- AngularJS 如何动态添加 HTML 并绑定到控制器
- 使用 js 添加 HTML 元素
- 在 JavaScript 切换中添加 html / css
- 如何在 tinymce 的第一段中添加 html 链接