javascript - Next.js - 使用 router.push() 时禁用跳转到锚点
问题描述
使用 next 10.2
,每次我们将带有哈希的 URL 推送到路由器时,next 都会跳转到锚元素。
import {useRouter} from 'next/router'
router.push('/contact#about-us');
如果我禁用也会发生这种情况scroll
router.push('/contact#about-us', undefined, { scroll: false });
我想平滑滚动到锚元素,而不是将其委托给 next.js。
import React from 'react'
import {useRouter} from 'next/router'
import PropTypes from "prop-types";
const LinkSmoothScroll = ({href, className, children}) => {
const router = useRouter()
const linkClicked = (e) => {
e.preventDefault();
router
.push(href, undefined, {scroll: false})
.then(() => {
const el = document.getElementById(href);
if ( typeof el !== "undefined" ) {
// Since header element is fixed, and document height is not being
// affected by fixed/absolute elements, we need to consider this
// on the margin to be applied.
const headerHeight = document.getElementById("nav-header")?.scrollHeight || 0;
const scrollTopY = el.top + window.scrollY - headerHeight;
window.scrollTo({
left: el.left,
top: scrollTopY,
behavior: 'smooth'
});
}
});
}
return (
<a href={href} className={className} onClick={linkClicked}>{children}</a>
)
}
LinkSmoothScroll.propTypes = {
href: PropTypes.any,
children: PropTypes.string,
className: PropTypes.string
}
export default LinkSmoothScroll;
我没有找到任何方法来禁用此行为,也没有找到避免它的调整。
解决方案
几个想法。
#1 - 使用 CSS
html {
scroll-behavior: smooth;
}
#2 - 不要在哈希中使用目标 ID
通过在哈希中为 ID 加上前缀来避免浏览器行为,这样在页面中就找不到匹配的元素(#about-us
变成#prefix-about-us
)。然后在导航上解析它并用JS滚动到它。
在我看来,这两种方法都不是很好,如果可能的话,最好保持默认行为不变。
推荐阅读
- ios - swift 非 void 函数应该在一个守卫中返回一个值
- c# - Asp.Net Core REST api:捕获模型序列化错误并返回显式消息
- google-analytics - GTM:“点击侦听器”标签类型的目的?
- java - 使用 IntelliJ 项目 lombok 注释
- windows - 使用带有缩短 url 的 bitsadmin
- server - 如何修改此 TCL 客户端/服务器程序以使服务器连续向端口写入数据?
- python - 需要正则表达式在字符串中查找 \n 或 \r
- javascript - Azure AD 编程登录
- c# - 如何从 JsonTextReader 获取详细的位置信息
- c - 手臂。将 .cantunwind 指令注入 C 函数