2017-05-06 134 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>