2016-07-28 209 views
0

我知道有很多關於此的問題,但沒有一個答案似乎對我有用。我試圖寫一些jQuery讓我的bootstrap導航欄更改顏色,一旦有人滾動傳遞元素。有人可以告訴我我做錯了什麼。謝謝!更改滾動條上的引導導航欄的顏色

Github上鍊接:https://github.com/Sruthik22/Sruthik22.github.io的jQuery是在頭

<!DOCTYPE html> 
<html lang = "en"> 
    <head> 
     <meta charset="utf-8"> 
     <title>Sruthi Kurada</title> 
     <meta name="description" content="Sruthi Kurada's Personal Website"> 
     <meta name="author" content="Sruthi Kurada"> 
     <!-- CSS Stylesheets --> 
     <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet"> 
     <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> 
     <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script> 
     <!-- Include all compiled plugins (below), or include individual files as needed --> 
     <script src="bootstrap/js/bootstrap.min.js"></script> 
     <link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.4.0/css/font-awesome.min.css"> 
     <link rel="stylesheet" href="style.css"> 

     <script type="text/javascript"> 
      $(document).ready(function(){  
    var scroll_start = 0; 
    var startchange = $('#Home'); 
    var offset = startchange.offset(); 
    if (startchange.length){ 
    $(document).scroll(function() { 
    scroll_start = $(this).scrollTop(); 
    if(scroll_start > offset.top) { 
     $('nav.navbar-custom').css('background-color', '#f0f0f0'); 
    } else { 
     $('nav.navbar-custom').css('background-color', 'transparent'); 
    } 
}); 
    } 
}); 
    </script> 
    </head> 
    <body> 
     <nav class="navbar navbar-fixed-top navbar-custom"> 
      <div class="container-fluid"> 
       <!-- Brand and toggle get grouped for better mobile display --> 
       <div class="navbar-header page-scroll"> 
        <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false"> 
        <span class="sr-only">Toggle navigation</span> 
        <span class="icon-bar"></span> 
        <span class="icon-bar"></span> 
        <span class="icon-bar"></span> 
        </button> 
        <a class="navbar-brand" href="#Home">Sruthi Kurada</a> 
       </div> 
       <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> 
        <div class="nav navbar-nav navbar-right"> 
         <a class="navbar-brand nav-elements page-scroll" href="#Home">Home</a> 
         <a class="navbar-brand nav-elements page-scroll" href="#About">About</a> 
         <a class="navbar-brand nav-elements page-scroll" href="#Skills">Skills</a> 
         <a class="navbar-brand nav-elements page-scroll" href="#Portfolio">Portfolio</a> 
         <a class="navbar-brand nav-elements page-scroll" href="#Contact">Contact</a> 
        </div> 
       </div> 
       <!-- /.navbar-collapse --> 
      </div> 
      <!-- /.container-fluid --> 
     </nav> 
     <a name = "Home" id = "Home"></a> 
     <div id="carousel-example-generic" class="carousel slide" data-ride="carousel"> 
      <!-- Indicators --> 
      <ol class="carousel-indicators"> 
       <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li> 
       <li data-target="#carousel-example-generic" data-slide-to="1"></li> 
       <li data-target="#carousel-example-generic" data-slide-to="2"></li> 
      </ol> 
      <!-- Wrapper for slides --> 
      <div class="carousel-inner" role="listbox"> 
       <div class="item active"> 
        <img src="Images/Robotics.jpg" alt="FLL Robotics Robot"> 
        <div class="carousel-caption"> 
         <h1>Innovator</h1> 
        </div> 
       </div> 
       <div class="item"> 
        <img src="Images/Math.jpg" alt="Math on a chalkboard"> 
        <div class="carousel-caption"> 
         <h1>Math Enthusiast</h1> 
        </div> 
       </div> 
       <div class="item"> 
        <img src="Images/PythonCode.png" alt="Python Code on Computer"> 
        <div class="carousel-caption"> 
         <h1>Coder</h1> 
        </div> 
       </div> 
      </div> 
      <!-- Controls --> 
      <a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev"> 
      <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span> 
      <span class="sr-only">Previous</span> 
      </a> 
      <a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next"> 
      <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span> 
      <span class="sr-only">Next</span> 
      </a> 
     </div> 
     <!--About Me--> 
     <a name="About" id = "About"></a> 
     <div class = "About"> 
      <div class="container"> 
       <br> 
       <div id="sruthi-picture"></div> 
       <div class="row"> 
        <div class = "col-md-1 col-xs-1"></div> 
        <div class="col-md-10 col-xs-10 text-center" id="Intro"> 
         My name is Sruthi Kurada 
        </div> 
        <div class = "col-xs-1"></div> 
       </div> 
       <div class="row"> 
        <div class = "col-xs-1"></div> 
        <div class="col-xs-10 text-center" id = "Subtitle"> 
         I am a 7th Grader at the Advanced Math & Science Academy (AMSA). 
        </div> 
       </div> 
       <br> 
       <div class="row"> 
        <div class = "col-xs-1"></div> 
        <div class="col-xs-10 text-center" id = "about-descript"> 
         I am interested in Robotics, Math, and Coding. I participate in FLL a Lego Robotics Competition. I do many mathamatics competitions, and I have coded several Python and Java Projects. 
        </div> 
       </div> 
       <div class="row"> 
        <div class = "col-xs-1"></div> 
        <div class="col-xs-10 text-center" id = "about-descript"> 
         I enjoy designing technology for less fortunate people and recently designed a belt to help the blind. I also play basketball & do karate as extracuriculars. 
        </div> 
       </div> 
       <br> 
      </div> 
     </div> 
     <div class = "AMSA"> 
      <div class="row"> 
       <div class = "col-xs-1"></div> 
       <div class="col-xs-10 text-center vcenter" id = "amsaText"> 
        <p>My school the Advanced Math and Science Academy is where I participate in many activities with my friends.</p> 
       </div> 
      </div> 
     </div> 
     <a name="Skills"></a> 
     <div class="container"> 
      <div class="row text-center"> 
       <div class="col-lg-10 col-lg-offset-1"> 
        <h2 id = "SkillsTitle">Skills</h2> 
        <div class="row"> 
         <div class="col-md-3 col-sm-6"> 
          <div class="service-item"> 
           <span class="fa-stack fa-4x"> 
           <i class="fa fa-circle fa-stack-2x"></i> 
           <i class="fa fa-users fa-stack-1x text-primary portfolioIcon" aria-hidden="true"></i> 
           </span> 
           <h4> 
            <strong class = "PortfolioTextTitle">Leadership</strong> 
           </h4> 
           <br> 
           <p class = "PortfolioText"> Taught people on my FLL team how to build robots. Also have taught kids how to code</p> 
          </div> 
         </div> 
         <div class="col-md-3 col-sm-6"> 
          <div class="service-item"> 
           <span class="fa-stack fa-4x"> 
           <i class="fa fa-circle fa-stack-2x"></i> 
           <i class="fa fa-android fa-stack-1x text-primary portfolioIcon" aria-hidden="true"></i> 
           </span> 
           <h4> 
            <strong class = "PortfolioTextTitle">App Developement</strong> 
           </h4> 
           <br> 
           <p class = "PortfolioText"> Learning Android Development and currently working on Udacity Nanodegree </p> 
          </div> 
         </div> 
         <div class="col-md-3 col-sm-6"> 
          <div class="service-item"> 
           <span class="fa-stack fa-4x"> 
           <i class="fa fa-circle fa-stack-2x"></i> 
           <i class="fa fa-code fa-stack-1x text-primary portfolioIcon" aria-hidden="true"></i> 
           </span> 
           <h4> 
            <strong class = "PortfolioTextTitle">Python</strong> 
           </h4> 
           <br> 
           <p class = "PortfolioText"> Well versed in Python Application Development as well as Django Framework. </p> 
          </div> 
         </div> 
         <div class="col-md-3 col-sm-6"> 
          <div class="service-item"> 
           <span class="fa-stack fa-4x"> 
           <i class="fa fa-circle fa-stack-2x"></i> 
           <i class="fa fa-code fa-stack-1x text-primary portfolioIcon" aria-hidden="true"></i> 
           </span> 
           <h4> 
            <strong class = "PortfolioTextTitle">Java</strong> 
           </h4> 
           <br> 
           <p class = "PortfolioText"> Proficent in Java Developement. Currently learning Machine Learning.</p> 
          </div> 
         </div> 
        </div> 
        <br> 
       </div> 
      </div> 
     </div> 

     <a name="Portfolio"></a> 
     <div class = "Portfolio"> 
      <div class="row"> 
       <div class = "col-md-1 col-xs-1"></div> 
       <div class="col-md-10 col-xs-10 text-center" id="PortfolioTitle"> 
        Portfolio       
       </div> 
       <div class = "col-xs-1"></div> 
      </div> 
      <div class = "row"> 
       <div class = "col-md-2 col-sm-0"></div> 
       <div class="col-md-4 col-sm-6">      
        <div class = "portfolio-box"> 
         <a title = "Movie App Github Source Code" href = "https://github.com/Sruthik22/MoviesApp"> 
          <img class = "img-portfolio img-responsive" src = "Images/PopularMovies.PNG"/> 
          <p class = "imagetext">Movies App</p> 
          <div class = "portfolio-box-caption"> 
           <div class = "portfolio-box-caption-content"> 
            <div class = "project-category text-faded"> 
             Android Development 
            </div> 
            <div class = "project-name"> 
             An Android app I created that lets users find most popular movies and the top rated movies. Once you click on a movie specific movie poster then it will give you more specific details such as movie title and release date. 
            </div> 
           </div> 
          </div> 
         </a> 
        </div> 
       </div> 
       <div class="col-md-4 col-sm-6"> 
        <div class = "portfolio-box"> 
         <a title = "Clash of the Math Titans Github Source Code" href = "https://github.com/Sruthik22/Clash-of-the-Math-Titans"> 
          <img class = "img-portfolio img-responsive" src = "Images/ClashOfTheMathTitans.png" alt="Code of Clash of the Math Titans"/> 
          <p class = "imagetext">Clash of the Math Titans</p> 
          <div class = "portfolio-box-caption"> 
           <div class = "portfolio-box-caption-content"> 
            <div class = "project-category text-faded"> 
             Code 
            </div> 
            <div class = "project-name"> 
             At a Hackathon my teammate and I designed this python game that helps young children learn the Order of Operations by gamifying it. 
            </div> 
           </div> 
          </div> 
         </a> 
        </div> 
       </div> 
       <div class = "col-md-2 col-sm-0"></div> 
      </div> 

      <br> 

      <div class = "row"> 
       <div class = "col-md-2 col-sm-0"></div> 
       <div class="col-md-4 col-sm-6"> 
        <div class = "portfolio-box"> 
         <a title = "Battleship Github Source Code" href = "#"> 
          <img class = "img-portfolio img-responsive" src = "Images/Battleship.png" alt="The Battleship code"/> 
          <p class = "imagetext">Battleship</p> 
          <div class = "portfolio-box-caption"> 
           <div class = "portfolio-box-caption-content"> 
            <div class = "project-category text-faded"> 
             Code 
            </div> 
            <div class = "project-name"> 
             The game battleship coded in python. There is both a single player mode and a multiplayer mode. The game also has graphics with the help of the graphics module graphics.py 
            </div> 
           </div> 
          </div> 
         </a> 
        </div> 
       </div> 

       <div class="col-md-4 col-sm-6">      
        <div class = "portfolio-box"> 
         <a title = "Movie App Github Source Code" href = "https://www.youtube.com/watch?v=ogvRFzNWM2w"> 
          <img class = "img-portfolio img-responsive" src = "Images/BeltForTheBlind.PNG" alt="Picture of the Limits of what a Product like the Blind Belt could do"/> 
          <p class = "imagetext">Belt for the Blind</p> 
          <div class = "portfolio-box-caption"> 
           <div class = "portfolio-box-caption-content"> 
            <div class = "project-category text-faded"> 
             Hardware 
            </div> 
            <div class = "project-name"> 
             A product I designed along with another team mate that helps the blind traverse their surroundings. It guides the user by using a ultrasonic sensor and alerts the user by using a volume component on the side of the bot. We won 3rd place in ProjectCSGirls. 
            </div> 
           </div> 
          </div> 
         </a> 
        </div> 
       </div> 

       <div class = "col-md-2 col-sm-0"></div> 
      </div> 
      <br> 
     </div> 

     <a name="Contact"></a> 
     <div class = "Contact"> 
      <div class="row"> 
       <div class = "col-md-1 col-xs-1"></div> 
       <div class="col-md-10 col-xs-10 text-center"> 
        <h1 id="ContactTitle">Contact</h1> 
       </div> 
       <div class = "col-xs-1"></div> 
      </div> 
      <div class = "row"> 
       <div class = "col-md-4 col-sm-4"></div> 
       <div class = "col-md-1 col-sm-1"> 
        <a target="_blank" href = "https://github.com/Sruthik22" class = "social"><i class="fa fa-github-square social"></i></a> 
       </div> 
       <div class="col-md-2 center col-sm-2"> 
        <a target="_blank" href = "mailto:[email protected]" class = "social"><i class="fa fa-envelope icon social"></i></a> 
       </div> 
       <div class = "col-md-1 col-sm-1"> 
        <a target="_blank" href = "http://stackoverflow.com/users/5693112/sruthi-kurada" class = "social"><i class="fa fa-stack-overflow social"></i></a> 
       </div> 
       <div class = "col-md-4 col-sm-4"></div> 
      </div> 
     </div> 
    </body> 
</html> 
+0

問題是在CSS查詢,查詢只是'.navbar' –

回答

0

你只是指定了錯誤的元素應用到background-color。試試這個:

$(document).scroll(function() { 
    scroll_start = $(this).scrollTop(); 
    if(scroll_start > offset.top) { 
     $('nav.navbar-custom').css('background-color', '#f0f0f0'); 
    } else { 
     $('nav.navbar-custom').css('background-color', 'transparent'); 
    } 
}); 
+0

出於某種原因,當我嘗試這一點,並沒有正常工作。 – Coder

+0

您是否檢查過該功能是否正在運行,並且正在滿足條件?您可以使用瀏覽器的開發工具進行調試或使用'console.log' –

+0

您好,我嘗試使用console.log並要求它在滾動文檔時記錄消息。我注意到它沒有記錄任何意味着有什麼錯誤的東西。我不知道這是什麼,但你可以通過看看我的GitHub幫助。十分感謝你的幫助。 – Coder