![](/img/trans.png)
[英]how to Redirect to a specific div on a different page with smooth scroll
[英]Redirect to another page and scroll to specific <div> are not working together
好的,這是我在這個論壇上發布的第一個問題,所以請善待.. :)
我正在使用 ASP.NET MVC 5,並且在單擊圖標時嘗試執行兩步過程。
這是我到目前為止得到的:
<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.