javascript - JQuery - offset.top - 将 80px 添加到返回值
问题描述
所以我有一个网站,我实现了平滑滚动。当用户单击指向我页面上某个位置的链接时,动画将执行到目标,offset.top
我需要将 80px 添加到该值,然后滚动到该元素。这是我现在正在使用的代码:
$(document).ready(function() {
$("a").on('click', function(event) {
if (this.hash !== "") {
event.preventDefault();
var hash = this.hash;
$('html, body').animate({
scrollTop: $(hash).offset().top
}, 500, function() {
window.location.hash = hash;
});
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
解决方案
应该很简单
scrollTop: $(hash).offset().top + 80
推荐阅读
- sql - SQL Server Management Studio 插入嵌套选择,无效列
- css - 用 flexbox 包裹元素
- python - 如何在Pydev、Eclipse中实时查看Python交互控制台中定义的变量?
- firebase - Firebase 数据库中的 URL 到 Firebase 存储中的图像
- php - 如何查询单个表并比较列以获取用户列表
- javascript - 以自动方式提取地点列表的纬度/经度数据
- javascript - 基本反应多页应用程序更改标题内容
- cups - 从 TIFF 转换为 ESCP2
- angular - Angulartics2:“Angulartics2GoogleAnalytics”类型上不存在 startTracking()
- image - 通过哈希在 Internet 上搜索图像