英文:
How to make React app scroll to a specific element on a newly rendered page?
问题
我正在努力寻找关于在单击相应元素后,如何使React应用程序滚动到新呈现页面上的特定元素的信息。我有一个主页,其中包含6个服务卡片。我还有一个包括有关每个服务的更详细描述的服务页面。我希望在主页上单击卡片后,浏览器能够自动滚动到服务页面中相应的服务。
以下是主页中的卡片组件:
const navigate = useNavigate()
return (
<div id={data.id} className="serviceCard slideUpContainer" onClick={() => navigate(data.link)}>
<img src={require('../visuals/' + data.img + '.png')} alt={data.title} />
<div className="slideUpContent">
<h2 className="slideUpTitle">{data.title}</h2>
<p className="slideUpText">{data.excerpt}</p>
<div className="linkContainer">
<a href={data.link}>{data.btn}<i className="glyphicon glyphicon-menu-right"></i></a>
</div>
</div>
</div>
)
以下是服务页面中的服务组件:
return (
<div id={data.id} className={data.className}>
<div id="imgBox">
<img src={require('../visuals/services/' + data.blockImg + '.png')} alt={data.alt} />
</div>
<div id="textBox">
<h2>{data.title}</h2>
<p>{data.description}</p>
<div>
<h3>{data.listName}</h3>
<ul>
{data.listItems.map(item =>
<ServiceListItem key={nanoid()} data={item} />
)}
</ul>
</div>
</div>
</div>
)
目前,我正在使用 navigate()
,我还尝试将 /#ElementID
添加到URL 中,但都没有得到期望的结果。我尝试在互联网上找更多信息,但我只能找到在同一页面上进行滚动的选项(这很容易实现)。
英文:
I am struggling to find information about how to make React app scroll to a specific element on a newly rendered page after clicking a corresponding element. I have a homepage with 6 service cards. I also have a services page that includes more detailed descriptions about each service. I want the browser to automatically scroll to a corresponding service in the services page after a card in homepage was clicked.
Here is the Card component in the homepage:
const navigate = useNavigate()
return (
<div id={data.id} className="serviceCard slideUpContainer" onClick={() => navigate(data.link)}>
<img src={require('../visuals/' + data.img + '.png')} alt={data.title} />
<div className="slideUpContent">
<h2 className="slideUpTitle">{data.title}</h2>
<p className="slideUpText">{data.excerpt}</p>
<div className="linkContainer">
<a href={data.link}>{data.btn}<i className="glyphicon glyphicon-menu-right"></i></a>
</div>
</div>
</div>
)
}
export default ServiceCard
And here is the Service component in the services page:
return (
<div id={data.id} className={data.className}>
<div id="imgBox">
<img src={require('../visuals/services/' + data.blockImg + '.png')}
alt={data.alt} />
</div>
<div id="textBox">
<h2>{data.title}</h2>
<p>{data.description}</p>
<div>
<h3>{data.listName}</h3>
<ul>
{data.listItems.map(item =>
<ServiceListItem key={nanoid()} data={item} />
)}
</ul>
</div>
</div>
</div>
)
}
For now I was using navigate()
, I also tried adding /#ElementID
to the url and neither of those gives me the desired result. I was trying to find more info on the internet, but all I get is the scrolling option on the same page (which is easy).
答案1
得分: 0
新渲染的页面意味着你可以使用一个空的依赖项 'useEffect' 作为回调,当你希望的组件渲染完成并在DOM中可用时:
useEffect(() => {
document.getElementById("ElementID").scrollIntoView();
// 或者,使用引用
elementRef.current.scrollIntoView()
}, [])
你还可以在希望进入视图的组件的任何父组件上使用,因为父组件的 'onEffect' 总是在子组件完全渲染后调用的(并且它自己的 'onEffect' 已经运行过)。
为了补充这一点,你还可以在你的CSS中使用:
- scroll-padding-top - 如果你有一个粘性标头或某种方式滚动不能正确居中元素
- scroll-behavior - 用于平滑滚动,如果你想要这个特性。如果你希望你的页面立即滚动到这个组件,但对于其他所有内容都有平滑滚动,你还可以:禁用平滑滚动 -> scrollIntoView() -> 重新启用平滑滚动。
英文:
Newly rendered page means that you can use an empty dependencies 'useEffect' as a callback for when the component you want into view finishes rendering and is available in the DOM:
useEffect(() => {
document.getElementById("ElementID").scrollIntoView();
// or, use a ref and
elementRef.current.scrollIntoView()
}, [])
You can also use on any parent component of the component you want into view, as parent's 'onEffect' is always called after a child has completely rendered (and its own 'onEffect' has already ran).
To complement this, you can also use in your CSS:
- scroll-padding-top - if you have a sticky header or somehow the scroll doesn't center the element correctly
- scroll-behavior - for smooth scrolling, if you want this feature. If you want your page to instant scroll to this component, but have smooth scroll for everything else, you can also: disable smooth scroll -> scrollIntoView() -> re-enable smooth scroll.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论