将jQuery自定义图像工具提示添加到jsTree

tak*_*llo 4 jquery jquery-ui jquery-plugins jstree

这基本上是此问题的后续措施

我试图将自定义工具提示添加到jstree中,如果鼠标悬停在它们上,它们会显示图像文件的缩略图。它应使用节点的href值标记缩略图并将其粘贴在工具提示中。参考上述文章,我设法使它显示一些任意的文本工具提示,但这显然不是我想要的。

如果我只是将工具提示单独添加到img标签或标签,我怀疑这将是一个问题。但是,我试图为埋在一堆jstree节点中的链接创建自定义工具提示。

例如:

    .on('hover_node.jstree',function(e,data){
            var $node = $("#" + data.node.id);
            var url = $node.find('a').attr('href');

            $("#" + data.node.id).prop('title', url);
        })
Run Code Online (Sandbox Code Playgroud)

做的很好...只是给我一个文本工具提示。但是我真的不知道从这里走到哪里,我已经将头撞墙了好几个小时了,在互联网上搜寻可行的解决方案。

$(function () {
        $(document).tooltip();

        $('#treeView').jstree({
            'core': {
                'multiple': false,
                'check_callback': true,
                'data': {
                    'url': 'Temp/ajax.html',
                    'data': function (node) {
                        return { 'id': node.id };
                    }
                }
            },
            'checkbox': {
                'three_state': false,
                'whole_node': false
            },
            'plugins': ["checkbox"]
        }).on('hover_node.jstree',function(e,data){
            var $node = $("#" + data.node.id);
            var url = $node.find('a').attr('href');

            $("#" + data.node.id).prop({ 'title': url, 'content': '<img src="' + url + '" alt=""/>' });
        })            
    });
Run Code Online (Sandbox Code Playgroud)

我所知道的只是我尝试过的所有方法都没有奏效。我阅读了有关JQuery工具提示插件的所有API文档,但我在这方面还是很新的,而且很显然,我无法以暴力方式和无知的方式解决方案。

更新

因此,我将代码修改如下:

.on('hover_node.jstree', function (e, data) {
            var $node = $("#" + data.node.id);
            var url = $node.find('a').attr('href');

            if (url != '#') {
                debugger
                //get the mouse position
                var x = $node.position().top + 20;
                var y = $node.position().left;
                $('.tooltip').css({ 'top': y + 'px', 'left': x + 'px' });
                $('.tooltip').find('img').attr('src', url);
                $('.tooltip').fadeIn(300, 'easeOutSine');
            }
        }).on('dehover_node.jstree', function () {
            $('.tooltip').fadeOut(200);
        });
Run Code Online (Sandbox Code Playgroud)

而且它工作..表面上。现在,我需要真正弄清楚如何获取MOUSE POINTER坐标,而不是NODE坐标。我将鼠标悬停在列表上的每个图像,工具提示都在右侧越来越远地显示。我在想办法在鼠标指针上显示它。

最后更新

    //assigning the right properties to the right
    //variables would work wonders for my cause.
    var x = $node.position().left;
    var y = $node.position().top + 20;
Run Code Online (Sandbox Code Playgroud)

Bra*_*sen 6

由于您不能在title属性中放置图像或其他自定义内容,因此需要自己创建工具提示。这可以通过简单地将a定位div在您将鼠标悬停在自定义内容的下方来完成。以下代码段显示了如何完成此操作。

$(document).ready(function() {
  var mouse_x = 0;
  var mouse_y = 0;
  $(document).mousemove(function(event) {
    mouse_x = event.pageX;
    mouse_y = event.pageY;
  });

  $('#custom_image_content').hide(); // Make sure the custom content does not show by default.

  $('#jsTreeTest').jstree({
      'core': {
        'check_callback': true,

      },
      'plugins': ["checkbox", "dnd", "contextmenu"]
    })
    .on('hover_node.jstree', function(e, data) {
      var $node = $("#" + data.node.id);
      var url = $node.find('a').attr('href');

      //            $("#" + data.node.id).prop('title', url);
      $('#custom_image_content').find('img').attr('src', url);
      $('#custom_image_content')
        .css('position', 'absolute')
        //.css('top', $node.position().top + 20) // Add about 20 to ensure the div is not hovered when we re-position it.
        //.css('left', $node.position().left)
        .css('top', mouse_y)
        .css('left', mouse_x)
        .show();
    })
    .on('dehover_node.jstree', function() {
      $('#custom_image_content').hide(); // Need to hide tooltip after we change hover targets.
    });
});
Run Code Online (Sandbox Code Playgroud)
<link href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.1.1/themes/default/style.min.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.1.1/themes/default-dark/style.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.1.1/jstree.min.js"></script>

<body>
  <form id="form1" runat="server">
    <div>
      <div id="jsTreeTest">
        <ul>
          <li>Test
            <ul>
              <li>SubDir1
                <ul>
                  <li><a href='https://www.google.com/images/srpr/logo11w.png'>File1.txt</a>
                  </li>
                </ul>
              </li>
              <li>SubDir2
                <ul>
                  <li>SubSubDir1
                    <ul>
                      <li><a href='https://www.google.com/images/srpr/logo11w.png'>File1.txt</a>
                      </li>
                      <li><a href='#'>File2.txt</a>
                      </li>
                    </ul>
                    <li><a href='https://www.google.com/images/srpr/logo11w.png'>File2.txt</a>
                    </li>
                    <li><a href='https://www.google.com/images/srpr/logo11w.png'>File3.txt</a>
                    </li>
                </ul>
                </li>
                <li><a href='https://www.google.com/images/srpr/logo11w.png'>File4.txt</a>
                </li>
                <li><a href='https://s.yimg.com/rz/l/yahoo_en-US_f_p_142x37.png'>File5.txt</a>
                </li>
            </ul>
            </li>
        </ul>
      </div>
    </div>
  </form>

  <div id="custom_image_content">This is my custom content
    <img src="" />
  </div>
</body>
Run Code Online (Sandbox Code Playgroud)

编辑:更新了鼠标y和x位置的工具提示位置。