簡體   English   中英

重定向到另一個頁面並滾動到特定頁面<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

好的,這是我在這個論壇上發布的第一個問題,所以請善待.. :)

我正在使用 ASP.NET MVC 5,並且在單擊圖標時嘗試執行兩步過程。

  1. Select 正確的頁面
  2. 向下滾動到該頁面上的某個部分。

這是我到目前為止得到的:

<a class="sidebar-brand d-flex align-items-center justify-content-start" >
   <div class="notification-bell" style="color:red">
   <i class="fas fa-fw fa-bell fa-2x" title="Number of Unread Comments" alert-count=@ViewBag.TotalUnreadComments.ToString() onclick='scrollToElement("CommentSection");'></i>
   </div>
</a>

和 Javascript

<script type='text/javascript'>
    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); 
    }
</script>

有趣的是,這些動作中的任何一個都可以單獨起作用,但它們不會一起起作用。

任何想法將不勝感激!!!

好吧,我覺得有點愚蠢,但我想出了一個簡單的解決方法......

對於這個問題,我剛剛創建了一個 javascript function 並將這兩個項目添加在一起,如下所示:

<script type='text/javascript'>
    function scrollToComments() {
        window.location.replace("/TodoListDashboard#CommentSection");
    }
</script>

然后我只是將我的 onclick 調用更改為:

<i class="fas fa-fw fa-bell fa-2x" title="Number of Unread Comments" alert-count=@ViewBag.TotalUnreadComments.ToString() onclick='scrollToComments();'></i>

暫無
暫無

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM