简体   繁体   English

移至可滚动内容中的元素

[英]move to an element in scrollable content

I have a standard scrollable content with some block elements. 我有一些带有块元素的标准可滚动内容。

I want to find out position of the div (relative to content top) and move scroll to that possiton. 我想找出div的位置(相对于内容顶部),然后将滚动移动到该位置。 Is it possible? 可能吗? Maybe with jquery. 也许与jQuery。

Yes, in jQuery it is possible. 是的,在jQuery中是可能的。

You can get the position of any id like this: $(divId).position().top and use scrollTop to set where the scrollbar goes. 您可以像这样获取任何id的位置: $(divId).position().top并使用scrollTop设置滚动条的位置。

Here's a fiddle , does that help? 这是一个小提琴 ,有帮助吗?

You can use JQuery to do something like that: 您可以使用JQuery执行类似的操作:

function scroll(element, parent){
    $(parent).animate({ scrollTop: $(element).offset().top - $(parent).offset().top }, { duration: 'slow', easing: 'swing'});
}

Check the demo . 检查演示

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

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