使用网址滚动到某个div [英] Scroll to certain div with url

查看:54
本文介绍了使用网址滚动到某个div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以发送请求加载某个div的URL只有像abc.com/#header1这样的东西导致我正在使用的方法我必须为我的任务制作5个单独的文件并更新所有这些文件。
如果可以通过在我的一个文件中包含任何代码来完成,那么它对我来说就是最好的。

Can i send the request to load certain div with a url only something like abc.com/#header1 cause the approach i am using i have to make 5 separate files for my task and update all of them. If it can be done by including any piece of code in my one file then it'll be best for me.

我想要div上的滚动网址请求类似于abc.com/#def,其中def是页面应加载的div的id。

I want the scroll on div with the url request something like abc.com/#def where def is the id of the div the page should load.

推荐答案

In与您的上一个问题的关系,您可以这样做:

In relation to your previous question, this is how you can do it:

$(function(){
  // get hash value
  var hash = window.location.hash;
  // now scroll to element with that id
  $('html, body').animate({ scrollTop: $(hash).offset().top });
});

您需要将该代码放在要滚动元素的页面中。因此,如果您访问 yourdomain.com/#foo ,它将滚动到 id 设置为<$ c的元素$ c> foo 。

You need to put that code in page where you want element to scroll. So if you visit yourdomain.com/#foo, it would scroll to an element with id set to foo.

这篇关于使用网址滚动到某个div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆