简体   繁体   English

滚动一个 <div> 特定内容

[英]Scrolling a <div> to Specific Content

I'm creating a split page with a menu on the left, and the main content on the right. 我正在创建一个拆分页面,左侧有一个菜单,右侧是主要内容。 When I click on a menu item, I want to scroll the main content to that item. 当我单击菜单项时,我想将主要内容滚动到该项目。

I found JavaScript scrollTo() , which takes offset arguments. 我找到了带偏移量参数的JavaScript scrollTo()

Is there any way to determine the offset of a particular <p> or other element within a <div> ? 有什么方法可以确定特定<p><div>其他元素的偏移量吗? Or perhaps there is another way to scroll to an element without knowing its offset? 还是有另一种方式可以滚动到某个元素而不知道其偏移量?

EDIT 编辑

Thanks for the replies. 感谢您的答复。 Looks like everyone gave similar answers. 似乎每个人都给出了类似的答案。 However, I ran into a problems with this. 但是,我遇到了一个问题。 It seems that offset().top (or position().top ) return different values depending on the current scroll position. 似乎offset().top (或position().top )根据当前滚动位置返回不同的值。

My jsFiddle is here: http://jsfiddle.net/gBTW9/4/embedded/result/ 我的jsFiddle在这里: http : //jsfiddle.net/gBTW9/4/embedded/result/

If I scroll to the top and selection Section 4, it works as expected. 如果我滚动到顶部并选择“第4部分”,它将按预期工作。 But once I've scrolled, it stops working correctly. 但是,一旦我滚动,它就会停止正常工作。 Can anyone see what is happening. 谁能看到发生了什么事。

You can get the vertical offset of an element using $('p').offset().top . 您可以使用$('p').offset().top获取元素的垂直偏移量。 You can combine this with scrollTop() using this: 您可以使用以下方法将其与scrollTop()结合使用:

$('div').scrollTop($('p').offset().top);

You need to use position() rather than offset(). 您需要使用position()而不是offset()。 If you know the id of that 如果你知道那个的ID

you can easily find the position of that paragraph tag 您可以轻松找到该段落标签的位置

jQuery: Difference between position() and offset() jQuery:position()和offset()之间的区别

There are jquery methods offset and position stat can help there. 有jquery方法offsetposition stat可以帮助您。

Also there is good scrollTo plugin which accepts elements and much more. 还有一个很好的scrollTo插件,可以接受元素等等。

If i didn't misunderstood you just need an animated scrolling to a particular element, something similar on what I did on my portfolio . 如果我没有误会的话,您只需要动画滚动到一个特定的元素,就和我在投资组合中所做的类似。

Assuming that the menu on the left is fixed, then just scroll the page to the element you want to scroll to: 假设左侧菜单是固定的,则只需将页面滚动到要滚动到的元素:

$("html, body").animate({
   scrollTop: $('#element').offset().top 
});

If you need to move a particular element over another element then: 如果需要将特定元素移到另一个元素上,则:

$("#element1").animate({
   top: $('#element2').offset().top 
});

Why try it the hard way, when you can use a HTML native attribute?? 当您可以使用HTML本机属性时,为什么还要尝试一下呢?

call me old school, but i like to keep it simple. 叫我老学校,但我喜欢保持简单。

within your menu: 在您的菜单中:

use: 采用:

<ul>
    <li>
        <a href="#Paragraph1">Paragraph 1</a>
    </li>
</ul>

instead of 代替

<ul>
    <li>Paragraph 1</li>
</ul>

this will make your section jump to the # (id) that equals Paragraph1 这将使您的部分跳转到等于Paragraph1的#(id)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM