滚动到具有jQuery的特定元素 [英] Scroll to a particular element w/ jQuery

查看:74
本文介绍了滚动到具有jQuery的特定元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一长串嵌套的div。我在查询字符串中传递特定元素的ID(实际上是一个段落元素),并打开它的div和父onload。但是,列表太长了,有时打开的元素隐藏在窗口底部的下方。

I have a long list of nested divs. I am passing the ID of a particular element (actually a paragraph element) on the querystring and opening its div and parent onload. However, the list is so long, sometimes the element that opens is hidden below the bottom of the window.

如何自动滚动用户的浏览器窗口,以便显示的元素位于屏幕顶部?

你可能不需要这个,但为了记录......我的列表看起来像这样:

You probably don't need this, but for the record... my list looks like this:

<div id="div1">
    <p id="1"></p>   
    <div>stuff</div>
    <p id="2"></p>   
    <div>stuff</div>
    <p id="3"></p>   
    <div>stuff</div>
</div>
...
<div id="divN">
    <p id="997"></p>   
    <div>stuff</div>
    <p id="998"></p>   
    <div>stuff</div>
    <p id="999"></p>   
    <div>stuff</div>
</div>


推荐答案

您可以使用 scrollIntoView 函数。

You could use the scrollIntoView function.

$(document).ready(function() {
  $('#divN').get(0).scrollIntoView();
});

这篇关于滚动到具有jQuery的特定元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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