一旦滚动浏览徽标,我就会使用javascript将导航栏固定在顶部。出于某种原因(仅在一页上),当我滚动浏览徽标时,它不会让我直接回到网页的顶部,我不确定是什么问题。 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>\
';
}
}
}
jquery的
$(window).bind('scroll', function() {
if ($(window).scrollTop() > 180) {
$('nav').addClass('fixed');
} else {
$('nav').removeClass('fixed');
}
});
答案 0 :(得分:0)
在您的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>