簡體   English   中英

轉到另一個頁面並單擊滾動到特定的 div (Reactjs)

[英]Go to another page and scroll to a specific div on one click (Reactjs)

我的問題很簡單,但我不知道該怎么做,我正在使用 Reactjs 在網站上工作,這是我的問題,我需要在單擊按鈕時向下滾動到特定的 div,當我在同一個網站上時它工作正常頁面,但當我在不同的頁面上時,它只導航(因為按鈕在導航欄中,所以它出現在所有頁面中)。

我正在使用 react-scroll 的鏈接,這是代碼:

import { Link as Scrolllink, animateScroll as scroll } from 'react-scroll'

                <li className="has-children" >
                    <Scrolllink
                        onClick={() => props.history.push('/fashion')}
                        to="HowItWorks"
                        spy={true}
                        smooth={true}
                        offset={-70}
                        duration={1500}
                    >How it works</Scrolllink>
                </li>

那么我應該怎么做才能讓它導航然后滾動? 提前致謝

您可能會遇到這樣的情況,因為對滾動鏈接的點擊沒有被取消,它將對鏈接上的 href 進行整頁加載,而不僅僅是進行單頁導航。 將您的 onClick 更改為:

onClick={e => {
  e.preventDefault();
  props.history.push('/fashion');
}}

重定向到另一個頁面並滾動到特定頁面<div>沒有一起工作</div><div id="text_translate"><p>好的,這是我在這個論壇上發布的第一個問題,所以請善待.. :)</p><p> 我正在使用 ASP.NET MVC 5,並且在單擊圖標時嘗試執行兩步過程。</p><ol><li> Select 正確的頁面</li><li>向下滾動到該頁面上的某個部分。</li></ol><p> 這是我到目前為止得到的:</p><pre> &lt;a class="sidebar-brand d-flex align-items-center justify-content-start" &gt; &lt;div class="notification-bell" style="color:red"&gt; &lt;i class="fas fa-fw fa-bell fa-2x" title="Number of Unread Comments" alert-count=@ViewBag.TotalUnreadComments.ToString() onclick='scrollToElement("CommentSection");'&gt;&lt;/i&gt; &lt;/div&gt; &lt;/a&gt;</pre><p> 和 Javascript</p><pre> &lt;script type='text/javascript'&gt; function scrollToElement(id) { // Set correct page window.location.replace("/TodoListDashboard"); //Get target var target = document.getElementById(id).offsetTop; //Scrolls to that target location window.scrollTo(0, target); } &lt;/script&gt;</pre><p> 有趣的是,這些動作中的任何一個都可以單獨起作用,但它們不會一起起作用。</p><p> 任何想法將不勝感激!!!</p></div>

[英]Redirect to another page and scroll to specific <div> are not working together

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 單擊時,轉到另一個頁面並打開一個隱藏的 div 如何獲得鏈接以轉到另一頁上的特定div ReactJs 如何在點擊或提交時滾動到 div 的底部 重定向到另一個頁面並滾動到特定頁面<div>沒有一起工作</div><div id="text_translate"><p>好的,這是我在這個論壇上發布的第一個問題,所以請善待.. :)</p><p> 我正在使用 ASP.NET MVC 5,並且在單擊圖標時嘗試執行兩步過程。</p><ol><li> Select 正確的頁面</li><li>向下滾動到該頁面上的某個部分。</li></ol><p> 這是我到目前為止得到的:</p><pre> &lt;a class="sidebar-brand d-flex align-items-center justify-content-start" &gt; &lt;div class="notification-bell" style="color:red"&gt; &lt;i class="fas fa-fw fa-bell fa-2x" title="Number of Unread Comments" alert-count=@ViewBag.TotalUnreadComments.ToString() onclick='scrollToElement("CommentSection");'&gt;&lt;/i&gt; &lt;/div&gt; &lt;/a&gt;</pre><p> 和 Javascript</p><pre> &lt;script type='text/javascript'&gt; function scrollToElement(id) { // Set correct page window.location.replace("/TodoListDashboard"); //Get target var target = document.getElementById(id).offsetTop; //Scrolls to that target location window.scrollTo(0, target); } &lt;/script&gt;</pre><p> 有趣的是,這些動作中的任何一個都可以單獨起作用,但它們不會一起起作用。</p><p> 任何想法將不勝感激!!!</p></div> 單擊時平滑滾動到特定 div 從另一頁滾動到特定的div減去X距離 在Angular中(單擊)滾動到特定的div 單擊按鈕滾動到特定div 刷新頁面,然后轉到特定的div 嘗試單擊一個div並使用存儲在另一個div中的網址轉到登錄頁面
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM