![](/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.