dynatree - 如何滚动到活动节点?
我有具有固定高度和垂直滚动条的动态树。
节点.activate();选择我正在搜索的节点,但不移动垂直滚动条,因此我必须手动滚动才能看到活动节点。
我如何以编程方式滚动它?
感谢mar10,我解决了这个问题:
var activeLi = node.li;
$('#tree').animate({
scrollTop: $(activeLi).offset().top - $('#tree').offset().top + $('#tree').scrollTop()
}, 'slow');
I have dynatree with fixed height and vertical scrollbar.
node.activate(); selects the node i'm searching for, but doesnt move the vertical scrollbar, so i have to scroll manualy to see active node.
How do i scroll it programmaticlly?
Thanks to mar10, I solved this problem:
var activeLi = node.li;
$('#tree').animate({
scrollTop: $(activeLi).offset().top - $('#tree').offset().top + $('#tree').scrollTop()
}, 'slow');
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Dynatree 没有内置的scrollTo 功能。
但是,一旦您拥有想要使其可见的 DOM 元素,您应该能够使用现有的方法和插件之一。
在您的情况下,您似乎已经有一个
node
对象,因此您可以获得关联的和
使用
node.li
或node.span
标记。一般来说,您可以随时通过调用
then get 关联的 DOM 元素来获取活动节点:
或处理激活事件:
一旦获得该元素,请使用标准方法:
使用 jquery 滚动到 div ,
jQuery 滚动到元素 ,
如何滚动到 jQuery 中的元素? ,
...
编辑 2014-05 从 Fancytree 开始添加了 2.0 autoScroll作为标准选项。
Dynatree does not have a built-in scrollTo functionality.
But you should be able to use one of the existing methods and plugins, once you have the DOM element that you want to make visible.
In your case, you already seem to have a
node
object, so you can get the the associated<li>
and<span>
tag usingnode.li
ornode.span
.In general you can get the active node at any time by calling
then get the associated DOM element:
or handle the activation event:
Once you have the element, use a standard method:
Scroll to a div using jquery ,
jQuery scroll to element ,
How to scroll to an element in jQuery? ,
...
Edit 2014-05 Starting with Fancytree 2.0 autoScroll was added as a standard option.
我遇到了类似的问题,并且无法让scrollTop按照上面的示例中所述工作。我通过将选择器从“#tree”更改为“.dynatree-container”来修复它:
这应该可以让您继续前进并节省几个小时的挫败感(:
顺便说一句,我正在使用 dynatree 版本 1.22 和 jquery 1.8.3 和jquery 用户界面 1.9.2。
I had a similar problem, and have not been able to get the scrollTop to work as stated in the example above. I fixed it by changing the selector from '#tree' to '.dynatree-container':
This should get you going and save a few hours of frustrataion (:
By the way, I am using dynatree version 1.22 and jquery 1.8.3 and jquery ui 1.9.2.
当页面上有多于一棵树时,使用 $('.dynatree-container') 时会出现问题,因为这将尝试选择具有该类的每一棵树。我有很多树,所以我需要选择具有特定 id 的树...但我发现,就像前一个人一样,尝试通过 id (例如“#tree”)选择树是行不通的。那么,什么会..?做类似 =$("#prevPageTree").dynatree("getTree").offset 的事情也不起作用......
几分钟后:好的,弄清楚如何做到这一点。在 Chrome 调试器中进行探索表明 .dynatree-container 类实际上附加到
。 dynatree 在 #tree 元素下插入的元素,用于初始化 dynatree 实例。所以你需要做类似的事情
,如果像我一样,你不希望它将节点向右滚动到窗口顶部,那么
会很好地将你的节点向下 150 像素
There is a problem when using $('.dynatree-container') when you have more than one tree on the page, as this will try and select every tree with this class. I have lots of trees so I need to select the tree with a particular id... but I find, as did the previous person, that trying to select the tree by its id (eg '#tree') does not work. So, what will..? doing something like =$("#prevPageTree").dynatree("getTree").offset also does not work...
A few minutes later: ok, figured out how to do this. Spelunking around in the Chrome debugger shows that the .dynatree-container class is actually attached to the <ul> element inserted by dynatree under the #tree element used to initialize the dynatree instance. So you need to do something like
And if, like me, you don't want it to scroll the node right to the top of the window, then
will nicely put your node 150 pixels down