通过 JavaScript 设置 2 div 的高度
Set height of 2 div's via JavaScript
在我的网站上,我有一个 header(这是一张背景图片),在它下面有一个导航栏。我喜欢 header 和导航栏填充 100% 的屏幕高度。
如果我将 header 的高度更改为 80% 或类似值,它仅适用于某些桌面尺寸。在较大的屏幕上,您可以看到导航栏下方的内容区域,或者在较小的屏幕上,您只能看到导航栏的一部分。
我认为应该可以通过 JavaScript 一起定义 header 和导航栏的大小(header+navbar= 高度 100%)。问题是,我以前从未真正使用过 JavaScript,也找不到可以做到这一点的代码。
有人知道怎么做吗?
您可以使用以下语句设置任何元素的高度。
document.getElementById('id').style.height = "100px";
您可以使用上面的代码并添加您的逻辑并且应该在页面加载时执行代码。
您可以像这样对导航栏和 header 使用 css 和 javascript:
@media screen and (max-width:680px) {
ul.navbar li:not(:first-child) {display: none;}
ul.navbar li.icon {
float: right;
display: inline-block;
}
}
/* The "responsive" class is added to the navbar with JavaScript when the user clicks on the icon. This class makes the navbar look good on small screens */
@media screen and (max-width:680px) {
ul.navbar.responsive {position: relative;}
ul.navbar.responsive li.icon {
position: absolute;
right: 0;
top: 0;
}
ul.navbar.responsive li {
float: none;
display: inline;
}
ul.navbar.responsive li a {
display: block;
text-align: left;
}
}
然后使用这个脚本:
function myFunction() {
var x = document.getElementById("IdOfnavbar");
if (x.className === "navbar") {
x.className += " responsive";
} else {
x.className = "navbar";
}
}
// jQuery solution
// ---------------
/*$(function() {
$(window).on('load resize', function() {
// set header height minus nav height
$('header').height($(this).height()-$('nav').height());
});
});*/
// vanillaJS
// ---------
// calculate height of header
/*var calculateHeight = function() {
document.querySelector('header').style.height = window.innerHeight- document.querySelector('nav').clientHeight+'px';
}
// add event listener to window
window.addEventListener('load', calculateHeight);
window.addEventListener('resize', calculateHeight);*/
$(function() {
var $nav = $('nav'),
$header = $('header'),
$content = $('.content'),
navOffsetTop = 0;
$(window).on('load resize', function() {
// set header height minus nav height
$header.height($(this).height()-$nav.height());
navOffsetTop = $nav.offset().top;
});
$(window).on('scroll', function() {
if($(this).scrollTop() >= navOffsetTop) {
if(!$nav.hasClass('fixed')) {
$nav.addClass('fixed');
$content.css({
marginTop: $nav.height()
})
}
} else {
if($nav.hasClass('fixed')) {
$nav.removeClass('fixed');
$content.removeAttr('style')
}
}
});
});
body {
margin: 0;
}
header {
background: url('https://unsplash.it/1200/1200');
background-size: cover;
}
nav {
background-color: #ddd;
height: 60px;
}
nav.fixed {
position: fixed;
top: 0;
left: 0;
width: 100%;
transition: all .3s;
background-color: #999;
}
.content {
padding: 30px;
height: 900px;
}
.content.scrolled {
padding-top: 60px;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
<header></header>
<nav></nav>
<div class="content">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Obcaecati quae, aliquam autem incidunt delectus consequatur accusamus quibusdam adipisci facilis sapiente praesentium. Error quibusdam officiis perferendis, eius saepe hic dolores atque!
</div>
</body>
</html>
好的,
我使用 CSS 解决方案,将 vh 而不是 % 设置为 header 和导航栏。这不是漂亮的解决方案(从设计师的角度来看),因为在更大的屏幕上它会以丑陋的方式拉伸导航栏。
我将尝试阅读更多关于 java 脚本的信息,但现在 vh 高度有效!
谢谢大家!
在我的网站上,我有一个 header(这是一张背景图片),在它下面有一个导航栏。我喜欢 header 和导航栏填充 100% 的屏幕高度。 如果我将 header 的高度更改为 80% 或类似值,它仅适用于某些桌面尺寸。在较大的屏幕上,您可以看到导航栏下方的内容区域,或者在较小的屏幕上,您只能看到导航栏的一部分。
我认为应该可以通过 JavaScript 一起定义 header 和导航栏的大小(header+navbar= 高度 100%)。问题是,我以前从未真正使用过 JavaScript,也找不到可以做到这一点的代码。
有人知道怎么做吗?
您可以使用以下语句设置任何元素的高度。
document.getElementById('id').style.height = "100px";
您可以使用上面的代码并添加您的逻辑并且应该在页面加载时执行代码。
您可以像这样对导航栏和 header 使用 css 和 javascript:
@media screen and (max-width:680px) {
ul.navbar li:not(:first-child) {display: none;}
ul.navbar li.icon {
float: right;
display: inline-block;
}
}
/* The "responsive" class is added to the navbar with JavaScript when the user clicks on the icon. This class makes the navbar look good on small screens */
@media screen and (max-width:680px) {
ul.navbar.responsive {position: relative;}
ul.navbar.responsive li.icon {
position: absolute;
right: 0;
top: 0;
}
ul.navbar.responsive li {
float: none;
display: inline;
}
ul.navbar.responsive li a {
display: block;
text-align: left;
}
}
然后使用这个脚本:
function myFunction() {
var x = document.getElementById("IdOfnavbar");
if (x.className === "navbar") {
x.className += " responsive";
} else {
x.className = "navbar";
}
}
// jQuery solution
// ---------------
/*$(function() {
$(window).on('load resize', function() {
// set header height minus nav height
$('header').height($(this).height()-$('nav').height());
});
});*/
// vanillaJS
// ---------
// calculate height of header
/*var calculateHeight = function() {
document.querySelector('header').style.height = window.innerHeight- document.querySelector('nav').clientHeight+'px';
}
// add event listener to window
window.addEventListener('load', calculateHeight);
window.addEventListener('resize', calculateHeight);*/
$(function() {
var $nav = $('nav'),
$header = $('header'),
$content = $('.content'),
navOffsetTop = 0;
$(window).on('load resize', function() {
// set header height minus nav height
$header.height($(this).height()-$nav.height());
navOffsetTop = $nav.offset().top;
});
$(window).on('scroll', function() {
if($(this).scrollTop() >= navOffsetTop) {
if(!$nav.hasClass('fixed')) {
$nav.addClass('fixed');
$content.css({
marginTop: $nav.height()
})
}
} else {
if($nav.hasClass('fixed')) {
$nav.removeClass('fixed');
$content.removeAttr('style')
}
}
});
});
body {
margin: 0;
}
header {
background: url('https://unsplash.it/1200/1200');
background-size: cover;
}
nav {
background-color: #ddd;
height: 60px;
}
nav.fixed {
position: fixed;
top: 0;
left: 0;
width: 100%;
transition: all .3s;
background-color: #999;
}
.content {
padding: 30px;
height: 900px;
}
.content.scrolled {
padding-top: 60px;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
<header></header>
<nav></nav>
<div class="content">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Obcaecati quae, aliquam autem incidunt delectus consequatur accusamus quibusdam adipisci facilis sapiente praesentium. Error quibusdam officiis perferendis, eius saepe hic dolores atque!
</div>
</body>
</html>
好的,
我使用 CSS 解决方案,将 vh 而不是 % 设置为 header 和导航栏。这不是漂亮的解决方案(从设计师的角度来看),因为在更大的屏幕上它会以丑陋的方式拉伸导航栏。 我将尝试阅读更多关于 java 脚本的信息,但现在 vh 高度有效! 谢谢大家!