gpt4 book ai didi

javascript - Scroll 上的视差图像和粘性标题无法正常工作

转载 作者:行者123 更新时间:2023-11-28 06:04:18 25 4
gpt4 key购买 nike

我使用 Html Css & Js 在 Scroll 上制作了一个简单的视差图像和 Sticky header ,除了包含 h1 标签的 ma​​in div 之外一切正常(正如您在下面的代码中看到的那样)确实在导航菜单下崩溃了!

这是我的代码:您应该使用随机图像来获得更好的结果来测试代码。

HTML:

    <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"></meta>
<title>Daygostar Homepage</title>
<link rel="stylesheet" type="text/css" href="css/styles.css"/>
</head>
<body>
<div id="wrapper">
<div id="header">
<img src="images/bg.png" id="bg" alt="Background Image"/>
</div>
<div id="content">
<div id="navbar">
<div id="logo">
Daygostar
</div>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Blogs</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>
</div>
<div id="main">
<h1>Daygostar Web Design</h1>
</div>
</div>
<script type="text/javascript" src="js/script.js"></script>
</body>
</html>

CSS:

    *{padding:0;margin:0;font-family:arial;}
#header{
height:91vh;
}
#header > img{
width:100%;
height:91vh;
position:fixed;
z-index:1;
}
#content{
height:100vh;
z-index:10;
background-color:#fff;
position:relative;
}
#content > #navbar{
position:absolute;
top:0;
width:100%;
height:80px;
background-color:#000;
}
#main{
height:100vh;
border:solid blue;
background-color:#fff;
position:relative;
padding-top:200px;
text-align:center;
font-size:25px;
z-index:10;
}
#main > *{
margin-top:30px;
}
#logo{
color:#fff;
font-size:35px;
float:left;
margin-top:15px;
margin-left:20px;
cursor:pointer;
}
#navbar > ul{
float:right;
width:700px;
list-style:none;
margin-top:25px;
}
#navbar > ul > li{
display:inline;
}
#navbar ul > li > a{
color:#fff;
font-size:20px;
text-decoration:none;
padding:10px 30px;
}
#navbar ul > li > a:hover{
text-decoration:underline;
}

Javascript:

    var header = document.getElementById("header");
var navBar = document.getElementById("navbar");
var bg = document.getElementById("bg");
var navbarHeight = navBar.offsetHeight;
var headerHeight = header.offsetHeight;

header.style.height = screen.height-navbarHeight;

function initParallex(){
if (window.pageYOffset > headerHeight){
navBar.style.position = "fixed";
navBar.style.top = "0";
}else{
navBar.style.position = "absolute";
navBar.style.top = "0";
}
bg.style.top = -(window.pageYOffset/5)+"px";
}

window.addEventListener("scroll", initParallex);

如您所见,我已经添加了一个 if/else 语句来检查 headerHeight 是否小于 window.pageYOffsetscript.js 中是否存在 文件,但是一旦网页到达主 div,折叠的导航栏就会隐藏!

如果你知道如何解决这个问题就太好了......

最佳答案

你可以在这里看看:

http://codepen.io/craigiswayne/pen/MyQVQB/

免责声明:

我确实使用了在此站点上找到的代码并添加了 jQuery。

HTML:

<div id="wrapper">
<div id="header">
<img src="http://orig13.deviantart.net/a8a7/f/2009/302/6/4/m_o_d_tile_background_by_viper_mod.png" id="bg" alt="Background Image" />
</div>
<div id="content">
<div id="navbar">
<div id="logo">
Daygostar
</div>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Blogs</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>
</div>
<div id="main">
<h1>Daygostar Web Design</h1>
</div>
</div>

CSS:

* {
padding: 0;
margin: 0;
font-family: arial;
}
#header {
height: 91vh;
}
#header > img {
width: 100%;
height: 91vh;
position: fixed;
z-index: 1;
}
#content {
height: 100vh;
z-index: 10;
background-color: #fff;
position: relative;
}
#content > #navbar {
width: 100%;
min-height: 80px;
background-color: #000;
}
#main {
height: 100vh;
border: solid blue;
background-color: #fff;
position: relative;
padding-top: 200px;
text-align: center;
font-size: 25px;
z-index: 1;
}
#main > * {
margin-top: 30px;
}
#logo {
color: #fff;
font-size: 35px;
float: left;
margin-top: 15px;
margin-left: 20px;
cursor: pointer;
}
/*#navbar {
position: absolute;
top: 0;
}*/

#navbar.sticky {
position: fixed;
top:0;
z-index: 100;
}

#navbar > ul {
/* float: right; */
width: 700px;
list-style: none;
margin-top: 25px;
white-space: nowrap;
}
#navbar > ul > li {
display: inline;
float: none;
}
#navbar ul > li > a {
color: #fff;
font-size: 20px;
text-decoration: none;
padding: 10px 30px;
}
#navbar ul > li > a:hover {
text-decoration: underline;
}

JavaScript:

document.addEventListener("DOMContentLoaded",function(){
window.scrollTo(0,0);
header.style.height = screen.height-navbar.offsetHeight;
navbar.position_from_top = getAbsoluteOffsetFromBody(navbar).top;
window.addEventListener("scroll", initParallex);
},false);

function initParallex(){
bg.style.top = -(window.pageYOffset/5)+"px";
if(window.scrollY >= navbar.position_from_top)
$(navbar).addClass('sticky');
else
$(navbar).removeClass('sticky');
}

//see here: http://stackoverflow.com/a/34014786/1654250
getAbsoluteOffsetFromBody = function(el)
{ // finds the offset of el from the body or html element
var _x = 0;
var _y = 0;
while( el && !isNaN( el.offsetLeft ) && !isNaN( el.offsetTop ) )
{
_x += el.offsetLeft - el.scrollLeft + el.clientLeft;
_y += el.offsetTop - el.scrollTop + el.clientTop;
el = el.offsetParent;
}
return { top: parseInt(_y), left: parseInt(_x) };
}

关于javascript - Scroll 上的视差图像和粘性标题无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36558760/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com