dynatree - 如何滚动到活动节点?

发布于 2024-12-28 03:57:00 字数 315 浏览 1 评论 0原文

我有具有固定高度和垂直滚动条的动态树。

节点.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

懵少女 2025-01-04 03:57:00

Dynatree 没有内置的scrollTo 功能。
但是,一旦您拥有想要使其可见的 DOM 元素,您应该能够使用现有的方法和插件之一。

在您的情况下,您似乎已经有一个 node 对象,因此您可以获得关联的

  • 使用 node.linode.span 标记。
  • 一般来说,您可以随时通过调用

    var node = $("#tree").dynatree("getActiveNode");
    // or
    var node = $("#tree").dynatree("getTree").getActiveNode();
    

    then get 关联的 DOM 元素来获取活动节点:

    var activeLI = node && node.li;
    

    或处理激活事件:

    onActivate: function(node) {
        var activeLI = node.li;
        ...
    }
    

    一旦获得该元素,请使用标准方法:

    使用 jquery 滚动到 div ,
    jQuery 滚动到元素
    如何滚动到 jQuery 中的元素? ,
    ...

    编辑 2014-05Fancytree 开始添加了 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 using node.li or node.span.

    In general you can get the active node at any time by calling

    var node = $("#tree").dynatree("getActiveNode");
    // or
    var node = $("#tree").dynatree("getTree").getActiveNode();
    

    then get the associated DOM element:

    var activeLI = node && node.li;
    

    or handle the activation event:

    onActivate: function(node) {
        var activeLI = node.li;
        ...
    }
    

    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.

    吲‖鸣 2025-01-04 03:57:00

    我遇到了类似的问题,并且无法让scrollTop按照上面的示例中所述工作。我通过将选择器从“#tree”更改为“.dynatree-container”来修复它:

    $('.dynatree-container').animate({ // animate the scrolling to the node
      scrollTop: $(activeLi).offset().top - $('.dynatree-container').offset().top + $('.dynatree-container').scrollTop()
    }, 'slow');
    

    这应该可以让您继续前进并节省几个小时的挫败感(:

    顺便说一句,我正在使用 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':

    $('.dynatree-container').animate({ // animate the scrolling to the node
      scrollTop: $(activeLi).offset().top - $('.dynatree-container').offset().top + $('.dynatree-container').scrollTop()
    }, 'slow');
    

    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.

    不忘初心 2025-01-04 03:57:00

    当页面上有多于一棵树时,使用 $('.dynatree-container') 时会出现问题,因为这将尝试选择具有该类的每一棵树。我有很多树,所以我需要选择具有特定 id 的树...但我发现,就像前一个人一样,尝试通过 id (例如“#tree”)选择树是行不通的。那么,什么会..?做类似 =$("#prevPageTree").dynatree("getTree").offset 的事情也不起作用......

    几分钟后:好的,弄清楚如何做到这一点。在 Chrome 调试器中进行探索表明 .dynatree-container 类实际上附加到

      。 dynatree 在 #tree 元素下插入的元素,用于初始化 dynatree 实例。所以你需要做类似的事情

    $("#tree ul").animate({ // animate the scrolling to the node
         scrollTop: $(activeLi).offset().top - $('#tree ul').offset().top + $('#tree ul').scrollTop()
    }, 'slow');
    

    ,如果像我一样,你不希望它将节点向右滚动到窗口顶部,那么

    scrollTop: $(activeLi).offset().top - $('#prevPageTree ul').offset().top + $('#prevPageTree ul').scrollTop() - 150
    

    会很好地将你的节点向下 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

    $("#tree ul").animate({ // animate the scrolling to the node
         scrollTop: $(activeLi).offset().top - $('#tree ul').offset().top + $('#tree ul').scrollTop()
    }, 'slow');
    

    And if, like me, you don't want it to scroll the node right to the top of the window, then

    scrollTop: $(activeLi).offset().top - $('#prevPageTree ul').offset().top + $('#prevPageTree ul').scrollTop() - 150
    

    will nicely put your node 150 pixels down

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文