这是我想添加100%而不是像素的代码。有谁能帮我这个代码?我在javascript上很糟糕
function init() {
window.addEventListener('scroll', function(e) {
var distanceY = window.pageYOffset || document.documentElement.scrollTop,
shrinkOn = 100,
header = document.querySelector("header");
if (distanceY > shrinkOn) {
classie.add(header, "smaller");
} else {
if (classie.has(header, "smaller")) {
classie.remove(header, "smaller");
}
}
});
}
window.onload = init();
<header>
<div class="navcontainer">
<div id="logo"></div>
<ul id="menu">
<li data-menuanchor="firstPage"><a href="#firstPage">POČETNA</a>
</li>
<li data-menuanchor="secondPage"><a href="#secondPage">BIOGRAFIJA</a>
</li>
<li data-menuanchor="3rdPage"><a href="#3rdPage">RURALNI MOTIVI</a>
</li>
<li data-menuanchor="4thPage"><a href="#4thPage">REČNI MOTIVI</a>
</li>
<li data-menuanchor="5thPage"><a href="#5thPage">GRADSKI MOTIVI</a>
</li>
<li data-menuanchor="lastPage"><a href="#lastPage">KONTAKT</a>
</li>
</ul>
</div>
</header>
答案 0 :(得分:1)
您无法使用100%
,但可以使用window.innnerHeight
function init() {
window.addEventListener('scroll', function(e){
var distanceY = window.pageYOffset || document.documentElement.scrollTop,
shrinkOn = window.innerHeight,
header = document.querySelector("header");
if (distanceY > shrinkOn) {
classie.add(header,"smaller");
} else {
if (classie.has(header,"smaller")) {
classie.remove(header,"smaller");
}
}
});
}
window.onload = init();