2017-09-24 78 views
0

我使用JavaScript將導航欄粘在頂部,一旦你滾動過去的標誌。出於某種原因(只在一頁上),當我滾動過去的標誌,它不會讓我,並移動到網頁的頂部,我不知道什麼是錯的。 http://webinnovations.ie/fenuhealthnew/products.php網頁不會讓我滾動當我修復導航欄頂部

CSS這是當滾動低於180像素

.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

包括您所使用這裏的代碼,但聽起來像你無意中做一些調用來完成滾動,像window.scrollTo –

+0

請附上您的代碼。 –

+0

對不起,我只是把代碼放在那裏 – user3408117

回答

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> 
相關問題