javascript - 如何使此滚动内容高于内容 120 像素?
问题描述
当我应用此脚本时,它会转到链接,但我希望它比链接到的内容高 120 像素。
这是代码:
<script>
$(document).ready(function(){
// Add smooth scrolling to all links
$("a").on('click', function(event) {
// Make sure this.hash has a value before overriding default behavior
if (this.hash !== 0) {
// Prevent default anchor click behavior
event.preventDefault();
// Store hash
var hash = this.hash;
// Using jQuery's animate() method to add smooth page scroll
// The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
$('html, body').animate({
scrollTop: $(hash).offset().top
}, 800, function(){
// Add hash (#) to URL when done scrolling (default click behavior)
window.location.hash = hash;
});
} // End if
});
});
</script>
编辑#02
这是我的 HTML、CSS 和 Java,供那些想知道的人使用:
解决方案
只需从计算中减去像素数量即可。
$(document).ready(function(){
// Add smooth scrolling to all links
$("a").on('click', function(event) {
// Make sure this.hash has a value before overriding default behavior
if (this.hash !== 0) {
// Prevent default anchor click behavior
event.preventDefault();
// Store hash
var hash = this.hash;
// Using jQuery's animate() method to add smooth page scroll
// The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
$('html, body').animate({
scrollTop: $(hash).offset().top -120
}, 800, function(){
// Add hash (#) to URL when done scrolling (default click behavior)
window.location.hash = hash;
});
} // End if
});
});
推荐阅读
- c# - 将串行端口字节(RS232)转换为文本 C#
- google-cloud-platform - 出现错误“创建函数“function-1”失败。错误:从控制台创建谷歌函数时请求失败,出现未知错误
- python - Python:合并字符串并转换为浮点数列表
- spring - PostgresSQL + Spring JPA:org.postgresql.util.PSQLException:错误:无法将类型 bytea 转换为没有时区的时间戳
- javascript - 在for循环的if语句中使用promise中的resolve值
- java - 代码对于 android studio 中的方法来说太大了
- mysql - SQL查询现有表内的现有表结果
- python - Python - 使用 Pandas 计算每个唯一标识符的日期之间的时间
- vba - 如何使用 Microsoft 访问创建多列报表/交叉表查询
- node.js - Node/Express 应用程序没有响应 post 方法