一旦您滚动过 Logo ,我将使用 javascript 将导航栏固定在顶部。出于某种原因(仅在一页上),当我滚动经过 Logo 时,它不会让我移动并右移回到网页顶部,我不确定出了什么问题。 http://webinnovations.ie/fenuhealthnew/products.php
css 这是滚动到 180px 以下时添加的类
.fixed {
position: fixed;
display:block;
background-color: white;
top: 0;
left: 0;
right: 0;
z-index: 2000;
}
html
<!DOCTYPE html>
<html>
<head>
<title>Products - FenuHealth</title>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="styles.css">
<script type="text/javascript" src="products.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script type="text/javascript" src="jquery.js"></script>
<style>
.twitter-timeline {
height: 120px !important;
}
@media screen and (max-width: 870px) {
.twitter-timeline {
height: 400px !important;
}
}
</style>
</head>
<body>
<header>
<div class="div-center">
<img style="float: left" id="logo" src="img/test.jpg" />
<img style="float: left" id="logo" src="img/logo.jpg" />
</div>
<nav class="nav-menu">
<img class="menuButton" src="img/menu.png" alt="button to open menu"/>
<ul id="menu">
<li><a href="index.php">Home</a></li>
<li id="current"><a href="products.php">Products</a></li>
<li><a href="symptoms.php">Symptoms</a></li>
<li><a href="awards.php">Awards</a></li>
<li><a href="testimonials.php">Testimonials</a></li>
<li><a href="faq.php">FAQ</a></li>
<li><a href="docs.php">Docs</a></li>
<li><a href="buy-now.php">Buy Now</a></li>
<li><a href="contact-us.php">Contact Us</a></li>
</ul>
</nav>
</header>
<div class="container">
<div class="main">
<nav class="nav-products">
<ul>
<li style="background-color: rgb(23, 127, 67)" onclick="productData(0)">FenuSave</li>
<li style="background-color: rgb(195, 38, 47)" onclick="productData(1)">FenuCare</li>
<li style="background-color: rgb(42, 45, 130)" onclick="productData(2)">FenuFeast</li>
<li style="background-color: rgb(186, 148, 119)" onclick="productData(3)">FenuCamel</li>
<li style="background-color: rgb(251, 120, 44)" onclick="productData(4)">FenuJoint</li>
<li style="background-color: rgb(19, 127, 182)" onclick="productData(5)">FenuCalm</li>
<li style="background-color: black" onclick="productData(6)">FenuLyte</li>
<li style="background-color: purple" onclick="productData(7)">FenuFoal</li>
</ul>
</nav>
<div id="product-div">
<!-- Javascript -->
<p>There was an error loading the product data</p>
</div>
<script>productData(0)</script>
</div>
<div class="sidebar">
<?php include('sidebar.php'); ?>
</div>
</div>
<?php include('footer.php'); ?>
</body>
</html>
JavaScript
function productData(productId) {
'use strict';
console.log("productId: " + productId);
var products = [
{"name": "FenuSave", "img": "img/fenusave.jpg", "text": "FenuSave helps to prevent gastric ulcers"},
{"name": "FenuCare", "img": "img/fenucare.jpg", "text": "FenuCare helps cure gastric ulcers"},
{"name": "FenuFeast", "img": "img/fenufeast.jpg", "text": "FenuFeast provides a reward or a special treat"},
{"name": "FenuCamel", "img": "img/fenucamel.jpg", "text": "FenuCamel helps camels race to victory"},
{"name": "FenuJoint", "img": "img/test.jpg", "text": "FenuJoint is suitable for the older horse"},
{"name": "FenuCalm", "img": "img/test.jpg", "text": "FenuCalm helps horses relax"},
{"name": "FenuLyte", "img": "img/test.jpg", "text": "FenuLyte replaces electrolytes after exercise or travel"},
{"name": "FenuFoal", "img": "img/test.jpg", "text": "FenuFoal is specially formulated for the younger animal"}
];
if (productId < 8 && productId >= 0) {
document.getElementById("product-div").innerHTML = '<h1 class="text-center">' + products[productId].name + '</h1>';
if (products[productId].img != "img/test.jpg") {
document.getElementById("product-div").innerHTML += '\
<img class="img" style="max-width: 500px" src="' + products[productId].img + '" />\
<p class="para text-center">' + products[productId].text + '</p>\
';
} else {
document.getElementById("product-div").innerHTML += '\
<p class="para text-center" style="margin-top: 30px">' + products[productId].text + '</p>\
';
}
}
}
j查询
$(window).bind('scroll', function() {
if ($(window).scrollTop() > 180) {
$('nav').addClass('fixed');
} else {
$('nav').removeClass('fixed');
}
});
最佳答案
在你的JavaScript
您添加的代码 fixed
类到 <nav>
HTML 标签。
在所有其他 HTML 页面中,您只有一个 nav
每页标记。但是在您提供的 HTML 页面中有两个 nav
标签可用。因此 jQuery 将添加 fixed
二年级nav
元素,因此您看不到原始导航。要解决此问题,您可以更改 nav
标记为类似 div
的内容用于第二次导航。正在关注HTML
代码不会给您的页面添加任何问题。
<!DOCTYPE html>
<html>
<head>
<title>Products - FenuHealth</title>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="styles.css">
<script type="text/javascript" src="products.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script type="text/javascript" src="jquery.js"></script>
<style>
.twitter-timeline {
height: 120px !important;
}
@media screen and (max-width: 870px) {
.twitter-timeline {
height: 400px !important;
}
}
</style>
</head>
<body>
<header>
<div class="div-center">
<img style="float: left" id="logo" src="img/test.jpg" />
<img style="float: left" id="logo" src="img/logo.jpg" />
</div>
<nav class="nav-menu">
<img class="menuButton" src="img/menu.png" alt="button to open menu"/>
<ul id="menu">
<li><a href="index.php">Home</a></li>
<li id="current"><a href="products.php">Products</a></li>
<li><a href="symptoms.php">Symptoms</a></li>
<li><a href="awards.php">Awards</a></li>
<li><a href="testimonials.php">Testimonials</a></li>
<li><a href="faq.php">FAQ</a></li>
<li><a href="docs.php">Docs</a></li>
<li><a href="buy-now.php">Buy Now</a></li>
<li><a href="contact-us.php">Contact Us</a></li>
</ul>
</nav>
</header>
<div class="container">
<div class="main">
<div class="nav-products">
<ul>
<li style="background-color: rgb(23, 127, 67)" onclick="productData(0)">FenuSave</li>
<li style="background-color: rgb(195, 38, 47)" onclick="productData(1)">FenuCare</li>
<li style="background-color: rgb(42, 45, 130)" onclick="productData(2)">FenuFeast</li>
<li style="background-color: rgb(186, 148, 119)" onclick="productData(3)">FenuCamel</li>
<li style="background-color: rgb(251, 120, 44)" onclick="productData(4)">FenuJoint</li>
<li style="background-color: rgb(19, 127, 182)" onclick="productData(5)">FenuCalm</li>
<li style="background-color: black" onclick="productData(6)">FenuLyte</li>
<li style="background-color: purple" onclick="productData(7)">FenuFoal</li>
</ul>
</div>
<div id="product-div">
<!-- Javascript -->
<p>There was an error loading the product data</p>
</div>
<script>productData(0)</script>
</div>
<div class="sidebar">
<?php include('sidebar.php'); ?>
</div>
</div>
<?php include('footer.php'); ?>
</body>
</html>
关于javascript - 当我将 Navbar 固定在顶部时,网页不会让我滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46390904/