开发者

Manipulating page scrolling when returning a View in an ASP.NET MVC site

开发者 https://www.devze.com 2023-01-07 10:03 出处:网络
I\'m building an ASP.NET MVC site where I want one of the Views I return to be automatically scroll to a certain point.

I'm building an ASP.NET MVC site where I want one of the Views I return to be automatically scroll to a certain point.

The part of the site where I want this to occur works sort of like a forum - there are "threads" that contain "posts". A user can either browse to the whole paginated thread or can browse to a specific post, using its ID. When a user browses to a specific post, I want to show the regular thread interface, then browse to the page that the post is on and scroll down to the post.

Is it possible to somehow automatically scroll down to a certain point when returning a View from an action in an A开发者_开发知识库SP.NET MVC site? If so, how do I do this?

NOTE: One solution to this problem that I've found is how Stack Overflow and the other Stack Exchange sites do it: each answer to a question can be linked to by adding #ID to the URL. If it's impossible to automatically scroll down when returning a View, I would implement this instead, but I don't understand how to use such an approach when there are multiple pages and the post in question isn't on the current page.


UPDATE:

Based on Chris' answer, I'm currently planning to implement it with the URL looking like this: example.com/forum/[ForumID]/thread/[ThreadID]/post/[PostID]#[PostID]. In my Action, I figure out what page of the Thread the Post is on, and then I return all the Posts from that page to the View.

However, I noticed something special in how Stack Overflow solves this problem. Try going to: https://meta.stackexchange.com/questions/57170 - it ends up sending you to https://meta.stackexchange.com/questions/57155/gravatar-bugs-and-improvements-in-chat/57170#57170.

How is the above implemented? This is exactly what I want to accomplish.


You can use javascript in your View to achieve automatic scrolling, with something like the jQuery ScrollTo plugin (http://plugins.jquery.com/project/ScrollTo). Attach it to your page load event and target your desired post.

However, the simplest way (and most cross-browser compatible) would be like how StackOverflow does it, with the #ID in the URL.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号