javascript - jQuery animate scrollTop 不会在任何浏览器上滚动
问题描述
我正在制作一个网站,当用户单击导航栏的某种按钮时,整个网页应滚动到包含用户选择的信息的部分。但它只是由于某种原因根本行不通。它曾经是,但有什么东西坏了。
导航栏代码:
<ul id="outJS">
<li>
<a href="#section0"> Infomratica </a>
</li>
</ul>
部分代码:
<section id="section0" class="sezione0">
<div class="container-fluid" style="text-align: center;">
<div id="heading" class="row" style="margin-top: 125px;">
<div class="col-md" style="margin-bottom: 80px;">Infomatica</div>
</div>
<div class="row justify-content-center">
<div class="button" id="button-3">
<div id="circle"></div><a>Infomatica</a>
</div>
</div>
</div>
</section>
应该提供滚动功能的代码是这样的:
$('a[href*="#"]').on("click", function (e) {
e.preventDefault();
let target = $($(this).attr("href"));
console.log(target.length);
if (target.length) {
$('html, body').animate({
scrollTop: $(target).offset().top
}, 1000);
}
});
我已经尝试了所有的可能性,但都没有奏效。
您知道是什么原因造成的,以及如何解决吗?
编辑:
我已经导入了 jQuery
解决方案
您的开始锚标记似乎缺少>
. 它应该是:
<a href="#section0">Informatica</a>
下面的工作示例:
$('a[href*="#"]').on("click", function(e) {
e.preventDefault();
let target = $($(this).attr("href"));
if (target.length) {
$('html, body').animate({
scrollTop: $(target).offset().top
}, 1000);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul id="outJS">
<li>
<a href="#section0">Informatica</a>
</li>
</ul>
<section id="section0" class="sezione0">
<div class="container-fluid" style="text-align: center;">
<div id="heading" class="row" style="margin-top: 125px;">
<div class="col-md" style="margin-bottom: 80px;">Informatica</div>
</div>
<div class="row justify-content-center">
<div class="button" id="button-3">
<div id="circle"></div><a>Informatica</a>
</div>
</div>
</div>
</section>
推荐阅读
- javascript - 延迟重试承诺,直到在 NodeJS 中解决
- javascript - 在我对问题进行分类后切换不起作用
- javascript - 如何以角度触发window.onerror?
- python - 如何在 python 中将单个字符串和多个字符串转换为有效的 set()?
- mitmproxy - mitmproxy:数据中不存在前导 0 的 HTTP 请求
- raspberry-pi - java.lang.UnsatisfiedLinkError: com.slytechs.library.NativeLibrary.dlopen(Ljava/lang/String;)J
- ruby - Ruby在ubuntu上作为服务(systemd)运行时执行系统命令
- python - Python selenium webdriver click() 无法正常工作
- django - django rest框架中的用户外键
- python - 如何使用 Python 在 Windows 中禁用触摸板?