Javascript 无法滚动到按id查看(平滑)-HTML/JS
我有这个点击锚标签: 但是点击后,它应该会转到id是谁的地方,但实际上它什么都不做,它会跳回到页面的开头,当然这不是预期的行为 我知道我的JS连接正确,因为我可以使用同一脚本中的onclick方法启动其他函数。所以,一定是函数工作不正常。也许我做错了 编辑 如果我将函数更改为如下所示:Javascript 无法滚动到按id查看(平滑)-HTML/JS,javascript,html,Javascript,Html,我有这个点击锚标签: 但是点击后,它应该会转到id是谁的地方,但实际上它什么都不做,它会跳回到页面的开头,当然这不是预期的行为 我知道我的JS连接正确,因为我可以使用同一脚本中的onclick方法启动其他函数。所以,一定是函数工作不正常。也许我做错了 编辑 如果我将函数更改为如下所示: function ScrollToView(id) { var elmnt = document.getElementById(id); alert(elmnt); elmnt.scro
function ScrollToView(id)
{
var elmnt = document.getElementById(id);
alert(elmnt);
elmnt.scrollIntoView(true);
}
警报只是说:[object htmldevelment]
但它似乎没有包含正确的标签。或者这是正常的吗?如果您想让代码保持普通,可以尝试:
function ScrollToView()
{
var elmnt = document.getElementById("whoweare");
elmnt.scrollIntoView();
}
如果您还使用jQuery,则可以改用此版本:
$("#whoweare").click(function() {
$('html, body').animate({
scrollTop: $("#whoweare").offset().top // You can change #whoweare to the id where you want to scroll to
}, 2000);
});
我发现它更有用,因为您可以滚动到页面的任何id,也可以通过调整数字来决定速度。要滚动到页面顶部,请使用true参数:
function ScrollToView() {
var elmnt = document.getElementById("whoweare");
elmnt.scrollIntoView(true);
}
请参阅文档…如果可以使用带有按钮的JQuery,则滚动似乎可以正常工作。签出此代码
没有对该功能的完全支持。。。也许这就是问题所在?elmnt.scrollIntoView-请不要使用false,只使用空括号。我刚刚在回答Hello时提供了我的建议。第一个是我已经做了很多次了,没有结果。第二个对我来说是新的。我将如何实现它?您在页面中使用jQuery吗?如果是,您可以将代码粘贴到html的底部进行测试。在关闭主体之前,添加一个脚本标记,并将代码放入其中,以便进行测试。以前从未使用过jQ。如何让我的a href调用此函数?好的,我也使用了它,但没有滚动发生。。。这可能与我使用最新的firefox有关吗?或bc所有内容都存储在本地?您的javascript控制台中是否有任何错误?检查您使用的浏览器是否支持该功能,正如@YAMM所建议的……同时请在Chrome和Mozilla上打开开发者工具F12,检查您的js代码中是否有问题中未显示的错误……在这个小提琴上,它看起来正在工作。。。可能您期望的行为与规范不同?
function ScrollToView()
{
var elmnt = document.getElementById("whoweare");
elmnt.scrollIntoView();
}
$("#whoweare").click(function() {
$('html, body').animate({
scrollTop: $("#whoweare").offset().top // You can change #whoweare to the id where you want to scroll to
}, 2000);
});
function ScrollToView() {
var elmnt = document.getElementById("whoweare");
elmnt.scrollIntoView(true);
}
<button>Click to Bottom</button>
<div align="center" class="box-2">
<div align="center>
<font color=" #534f4f " size=" +1 ">
<h1>Who we are</h1>
</font>
</div>
<div style=" padding-left: 15px; padding-right: 15px ">
<table border=" 0 ">
<tr> <!--tablerow-->
<th width=400px>
<div align=" center ">
<img
src=" ./img/me.png "
width=60%
</img>
</div>
</th>
<td width=400px>
<div align=" justify ">
<font color=" #534f4f " size=" +2 " >
<h3>Julius Tolksdorf</h3>
<p>CEO of innomotion media and head of software development.<br>
He will be your primary contact during the planning and development processes.
Julius has already finished about 20 apps & games & web pages and has years of experience being an Android & Unity developer.
</p>
</font>
</div>
</td>
</tr> <!--for padding-->
<tr height=20px/>
</tr>
</table>
</div>
<div align="center" class="box-2">
<div align="center>
<font color=" #534f4f " size=" +1 ">
<h1>Who we are</h1>
</font>
</div>
<div style=" padding-left: 15px; padding-right: 15px ">
<table border=" 0 ">
<tr> <!--tablerow-->
<th width=400px>
<div align=" center ">
<img
src=" ./img/me.png "
width=60%
</img>
</div>
</th>
<td width=400px>
<div align=" justify ">
<font color=" #534f4f " size=" +2 " >
<h3>Julius Tolksdorf</h3>
<p>CEO of innomotion media and head of software development.<br>
He will be your primary contact during the planning and development processes.
Julius has already finished about 20 apps & games & web pages and has years of experience being an Android & Unity developer.
</p>
</font>
</div>
</td>
</tr> <!--for padding-->
<tr height=20px/>
</tr>
</table>
</div>
<div align="center" class="box-2">
<div align="center>
<font color=" #534f4f " size=" +1 ">
<h1>Who we are</h1>
</font>
</div>
<div style=" padding-left: 15px; padding-right: 15px ">
<table border=" 0 ">
<tr> <!--tablerow-->
<th width=400px>
<div align=" center ">
<img
src=" ./img/me.png "
width=60%
</img>
</div>
</th>
<td width=400px>
<div align=" justify ">
<font color=" #534f4f " size=" +2 " >
<h3>Julius Tolksdorf</h3>
<p>CEO of innomotion media and head of software development.<br>
He will be your primary contact during the planning and development processes.
Julius has already finished about 20 apps & games & web pages and has years of experience being an Android & Unity developer.
</p>
</font>
</div>
</td>
</tr> <!--for padding-->
<tr height=20px/>
</tr>
</table>
</div>
<div align="center" class="box-2">
<div align="center>
<font color=" #534f4f " size=" +1 ">
<h1>Who we are</h1>
</font>
</div>
<div style=" padding-left: 15px; padding-right: 15px ">
<table border=" 0 ">
<tr> <!--tablerow-->
<th width=400px>
<div align=" center ">
<img
src=" ./img/me.png "
width=60%
</img>
</div>
</th>
<td width=400px>
<div align=" justify ">
<font color=" #534f4f " size=" +2 " >
<h3>Julius Tolksdorf</h3>
<p>CEO of innomotion media and head of software development.<br>
He will be your primary contact during the planning and development processes.
Julius has already finished about 20 apps & games & web pages and has years of experience being an Android & Unity developer.
</p>
</font>
</div>
</td>
</tr> <!--for padding-->
<tr height=20px/>
</tr>
</table>
</div>
<div id="whoweare" align=" center " class=" box-2 ">
<div align=" center>
<font color="#534f4f" size="+1">
<h1>Who we are</h1>
</font>
</div>
<div style="padding-left: 15px; padding-right: 15px">
<table border="0">
<tr>
<!--tablerow-->
<th width=400px>
<div align="center">
<img src="./img/me.png" width=60% </img> </div> </th> <td width=400px>
<div align="justify">
<font color="#534f4f" size="+2">
<h3>Julius Tolksdorf</h3>
<p>CEO of innomotion media and head of software development.<br> He will be your primary
contact during the planning and development processes. Julius has already finished
about 20 apps & games & web pages and has years of experience being
an Android & Unity developer.
</p>
</font>
</div>
</td>
</tr>
<!--for padding-->
<tr height=20px />
</tr>
</table>
</div>
<div id="" align="center" class="box-2">
<div align="center>
<font color=" #534f4f " size=" +1 ">
<h1>Who we are</h1>
</font>
</div>
<div style=" padding-left: 15px; padding-right: 15px ">
<table border=" 0 ">
<tr> <!--tablerow-->
<th width=400px>
<div align=" center ">
<img
src=" ./img/me.png "
width=60%
</img>
</div>
</th>
<td width=400px>
<div align=" justify ">
<font color=" #534f4f " size=" +2 " >
<h3>Julius Tolksdorf</h3>
<p>CEO of innomotion media and head of software development.<br>
He will be your primary contact during the planning and development processes.
Julius has already finished about 20 apps & games & web pages and has years of experience being an Android & Unity developer.
</p>
</font>
</div>
</td>
</tr> <!--for padding-->
<tr height=20px/>
</tr>
</table>
</div>
<button>Click to Top</button>
$(document).ready(function(){
$("button").click(function() {
$('html, body').animate({
scrollTop: $("#whoweare").offset().top - document.body.clientHeight + $(document).height()
}, 1000);
});
});