scrollIntoView() 没有将节点滚动到树的顶部

scrollIntoView() not scrolling node to top of tree

我遇到了 scrollIntoView() 函数的问题。 我有一棵树和两个按钮:

  1. 第一个将树滚动到底部节点
  2. 第二个将树滚动到顶部节点

我不知道我应该怎么做,这个滚动操作总是将节点滚动到树的顶部。我的意思是这个 fiddle:

的情况

http://jsfiddle.net/presto41/xzb62pw5/8/

如果您单击按钮 #button1 (Scroll to bottom node),那么树将滚动到底部节点 - 但底部节点不会位于树的顶部。它的工作方式为 "just show node. Done".

我希望在这种情况下滚动到底部节点将像滚动到顶部节点一样工作:

  1. 点击#button1 (Scroll to bottom node)
  2. 点击#button2 (Scroll to top node)
  3. topNode 在树的顶端

有什么想法吗?我试图通过 options 来操纵这个 scrollIntoView() 函数,但要么我做错了什么,要么它不能像我预期的那样工作。


Edit: I don't understand why anyone give me minus for that question. I made a research, I was trying to solve that problem by myself and I put clear description of my problem. It's irritating.


Edit 2: I haven't ids of these nodes. I've got just node as Fancytree.FancytreeNode object so I need a solution without operation on the node id.


Edit 3: this is my pseudocode witch is the most similar with original: http://jsfiddle.net/presto41/xzb62pw5/27/

使用scrollIntoView()是为了将id="content"的元素滚动到浏览器的可见区域window:

    var el = document.getElementById("content");
    el.scrollIntoView();

但是你可以使用

    function scrollToBottom(){
       var eldiv = document.getElementById("content");
       eldiv.scrollTop = div.scrollHeight - div.clientHeight;
    }
    function scrollToTop(){
       var eldiv = document.getElementById("content");
       eldiv.scrollTop = 0;
    }

我找到了解决方案。 解决方案是函数:

function expandAndScrollTo(node) {
       node.setExpanded(true).then(()=>{
       node.span.scrollIntoView({behavior:'smooth', inline:'start'});
  });}