我有一个简单的导航栏来处理导航栏的粘性缩小。
HTML: -
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript" src="script.js"></script>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<header></header>
<nav id="nav" class="nav">
<div class="nav__inner">
</div>
</nav>
<div class="container">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Incidunt id error ut delectus a sapiente accusantium quam, laudantium, atque laborum, distinctio. Commodi perspiciatis ipsa ducimus labore, iure dignissimos fugit quae.</p>
</div>
<footer>This is footer</footer>
</body>
</html>
CSS: -
* {
box-sizing: border-box;
}
body {
font-family: 'helvetica neue';
letter-spacing: 0.03em;
color: #333;
}
header {
height: 150px;
background: black;
}
footer {
height: 50px;
background: black;
color: dodgerblue;
}
.nav {
position: sticky;
top: 0;
height: 80px;
background: dodgerblue;
transition: height 200ms ease;
&.is-shrunk {
height: 50px;
}
}
JS: -
const nav = document.getElementById('nav');
const navFromTop = nav.offsetTop;
const navHeight = nav.offsetHeight;
const distanceScrolled = () => {
return window.pageYOffset;
};
const isScrolledEnough = () => {
return (distanceScrolled() > navFromTop + navHeight);
};
const setIsShrunk = (value) => {
if (value && isScrolledEnough()) {
nav.classList.add('is-shrunk');
} else if (!value) {
nav.classList.remove('is-shrunk');
}
};
window.addEventListener('scroll', () => {
setIsShrunk(distanceScrolled() > navHeight);
});
我希望此代码在页面滚动时将蓝色导航栏从80px高度缩小到50px高度。
我所做的是从Sticky shrink复制代码。但我没有得到所提到的链接所示的效果。滚动页面时蓝色导航栏没有像链接中那样缩小。可能是什么问题和解决方案。
答案 0 :(得分:2)
只需更改:
&.is-shrunk {
height: 50px;
}
以下内容并将其从CSS中的嵌套.nav选择器中删除。
.nav.is-shrunk {
height: 50px;
}
&符号(&amp;)是一个SASS嵌套选择器,您也可以这样做 没有嵌套的操作如下所示。
const nav = document.getElementById('nav');
const navFromTop = nav.offsetTop;
const navHeight = nav.offsetHeight;
const distanceScrolled = () => {
return window.pageYOffset;
};
const isScrolledEnough = () => {
return (distanceScrolled() > navFromTop + navHeight);
};
const setIsShrunk = (value) => {
if (value && isScrolledEnough()) {
nav.classList.add('is-shrunk');
} else if (!value) {
nav.classList.remove('is-shrunk');
}
};
window.addEventListener('scroll', () => {
setIsShrunk(distanceScrolled() > navHeight);
});
* {
box-sizing: border-box;
}
body {
font-family: 'helvetica neue';
letter-spacing: 0.03em;
color: #333;
}
header {
height: 150px;
background: black;
}
footer {
height: 50px;
background: black;
color: dodgerblue;
}
.nav {
position: sticky;
top: 0;
height: 80px;
background: dodgerblue;
transition: height 200ms ease;
}
.nav.is-shrunk {
height: 50px;
}
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript" src="script.js"></script>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<header></header>
<nav id="nav" class="nav">
<div class="nav__inner">
</div>
</nav>
<div class="container">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Incidunt id error ut delectus a sapiente accusantium quam, laudantium, atque laborum, distinctio. Commodi perspiciatis ipsa ducimus labore, iure dignissimos fugit quae.</p>
<p>Nesciunt eos recusandae adipisci nostrum illum eius. Tempora nemo necessitatibus temporibus quaerat laudantium dolorem minima blanditiis sapiente earum fugiat commodi officiis, magni ducimus corporis, ad! Maiores perspiciatis aperiam quod nemo.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Incidunt id error ut delectus a sapiente accusantium quam, laudantium, atque laborum, distinctio. Commodi perspiciatis ipsa ducimus labore, iure dignissimos fugit quae.</p>
<p>Nesciunt eos recusandae adipisci nostrum illum eius. Tempora nemo necessitatibus temporibus quaerat laudantium dolorem minima blanditiis sapiente earum fugiat commodi officiis, magni ducimus corporis, ad! Maiores perspiciatis aperiam quod nemo.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Incidunt id error ut delectus a sapiente accusantium quam, laudantium, atque laborum, distinctio. Commodi perspiciatis ipsa ducimus labore, iure dignissimos fugit quae.</p>
<p>Nesciunt eos recusandae adipisci nostrum illum eius. Tempora nemo necessitatibus temporibus quaerat laudantium dolorem minima blanditiis sapiente earum fugiat commodi officiis, magni ducimus corporis, ad! Maiores perspiciatis aperiam quod nemo.</p>
<p>Quisquam magni doloribus deserunt rerum, expedita iste numquam ipsum velit beatae, dolores optio sapiente corporis voluptatum mollitia nemo excepturi eveniet provident dolorem facilis incidunt minus aliquam error quas. Distinctio, exercitationem!</p>
<p>Esse sapiente dolore labore, fuga nobis voluptates explicabo odio asperiores, quod repudiandae totam harum tempora praesentium natus. Blanditiis, sint id sit, ab impedit fuga aliquam distinctio harum, ex molestias voluptates!</p>
<p>Cumque laborum, ad sequi, reiciendis illum non vero voluptatum libero consequatur quod vitae voluptatem quibusdam incidunt inventore temporibus omnis optio sint quasi soluta. Reprehenderit omnis inventore, doloremque repudiandae minus recusandae.</p>
<p>Reiciendis hic, placeat laborum iure quo ratione ex perspiciatis, error perferendis sed facilis animi corrupti delectus soluta libero id eligendi iusto magnam itaque. Facere, ducimus. Eveniet officiis quod, vero asperiores.</p>
</div>
<footer>This is footer</footer>