2017-05-06 187 views
1

我犯了一個導航欄(例如引導)滾動通過DIV的JavaScript

<div class="collapse navbar-collapse" id="navbarNav"> 
    <ul class="navbar-nav"> 
     <li class="nav-item active"> 
     <a class="nav-link" href="#" id="button">Home</a> 
     </li> 
    </ul> 
    </div> 

上面下面的代碼是項「家」,我試圖滾動到使用JavaScript其他項目。我已經包含的JavaScript這種方式轉換成HTML <script src="scripts/javascript.js"></script>

這是我試圖滾動到div:

<div class="bgimg-2" id="myDiv"> 
    <div class="caption"> 
    <a href="#"> 
    <span class="border" style="background-color:transparent;font-size:25px;color: #f7f7f7;">PROJECTS</span> 
    </a> 
    </div> 
</div> 

而且我寫的JavaScript是以下幾點:

$("#button").click(function() { 
    $('html, body').animate({ 
     scrollTop: $("#myDiv").offset().top 
    }, 2000); 
}); 

不知何故此代碼不起作用。有誰能幫我解決這個問題嗎?

+0

你可以分享的HTML? – brk

回答

0

不知道哪裏是你的代碼的位置,但如果你把你的代碼裏面的文件準備好它應該只是罰款:

而且第一次使用下面的把你的HTML結束,看看你的JS是工作,那麼你可以將js腳本移動到一個js文件,並像你一樣加載到HTML的末尾。請記住,在加載代碼之前需要加載jquery,並且所有html都要比js代碼更好。

<script> 
$(document).ready(function() { 
    $("#button").click(function() { 
    $('html, body').animate({ 
     scrollTop: $("#myDiv").offset().top 
    }, 2000); 
    }); 
}); 
</script> 

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="collapse navbar-collapse" id="navbarNav"> 
 
    <ul class="navbar-nav"> 
 
    <li class="nav-item active"> 
 
     <a class="nav-link" href="#" id="button">Home</a> 
 
    </li> 
 
    </ul> 
 
</div> 
 
<pre> 
 

 
Lorem Ipsum 
 
"Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit..." 
 
"There is no one who loves pain itself, who seeks after it and wants to have it, simply because it is pain..." 
 

 
What is Lorem Ipsum? 
 
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. 
 

 
Why do we use it? 
 
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). 
 

 

 
Where does it come from? 
 
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32. 
 

 
Lorem Ipsum 
 
"Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit..." 
 
"There is no one who loves pain itself, who seeks after it and wants to have it, simply because it is pain..." 
 

 
What is Lorem Ipsum? 
 
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. 
 

 
Why do we use it? 
 
It is a long established fact that a reader will be distracted by the readable content of a page 
 

 
when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). 
 

 

 
Where does it come from? 
 
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32. 
 

 
Lorem Ipsum 
 
"Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit..." 
 
"There is no one who loves pain itself, who seeks after it and wants to have it, simply because it is pain..." 
 

 
What is Lorem Ipsum? 
 
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. 
 

 
Why do we use it? 
 
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). 
 

 

 
Where does it come from? 
 
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32. 
 
</pre> 
 
<div class="bgimg-2" id="myDiv"> 
 
    <div class="caption"> 
 
    <a href="#"> 
 
     <span class="border" style="background-color:transparent;font-size:25px;color: RED;">PROJECTS</span> 
 
    </a> 
 
    </div> 
 
</div> 
 

 
<script> 
 
$(document).ready(function() { 
 
    $("#button").click(function() { 
 
    $('html, body').animate({ 
 
     scrollTop: $("#myDiv").offset().top 
 
    }, 2000); 
 
    }); 
 
}); 
 
</script>