【问题标题】:Adding jQuery custom image tool tip to jsTree将 jQuery 自定义图像工具提示添加到 jsTree
【发布时间】:2015-07-14 21:55:23
【问题描述】:

这基本上是对this question的跟进

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

如果我只是简单地将工具提示添加到 img 标签或 a 标签,我怀疑这会是一个问题。但是,我正在尝试为隐藏在一堆 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);
        })

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

$(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=""/>' });
        })            
    });

我只知道我尝试过的任何方法都没有奏效。我阅读了 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);
        });

它的工作原理..表面上。现在我需要真正弄清楚如何获得鼠标指针坐标,而不是节点坐标。我将鼠标悬停在列表中的每个图像,工具提示都会越来越向右显示。我正在想办法在鼠标指针处显示它。

最后更新

    //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;

【问题讨论】:

  • 在jQuery .mouseenter 事件中,e 参数包含鼠标位置数据,但在jstree_hover 事件中似乎不是这样。
  • 查看stackoverflow.com/questions/4517198/…获取鼠标位置。
  • 那是我最初尝试的方法,但 e.pageX 和 e.pageY 由于某种原因返回 undefined。
  • 我是个笨蛋。如果我将正确的属性分配给正确的变量,将会有所帮助。我将顶部位置分配给 x,将左侧位置分配给 y。难怪它正在做它正在做的事情。

标签: jquery jquery-ui jquery-plugins jstree


【解决方案1】:

由于您无法将图像或其他自定义内容放入title 属性中,因此您需要自己创建工具提示。这可以通过简单地将div 定位在您悬停自定义内容的位置下方来完成。下面的 sn-p 显示了如何做到这一点。

$(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.
    });
});
<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>

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

【讨论】:

  • 你好,布兰特!呵呵,这正是我下定决心要做的。谢谢!在这一点上我唯一的问题是如何从 jstree_hover 事件中获取 x/y 位置,就是这样!你太棒了,谢谢。
  • 完成了,现在看起来很棒,我非常高兴。有趣的是,我昨天花了 8 个小时来解决这个问题……今天早上醒来,在半小时内解决它。所以这就是为什么我的朋友总是告诉我“如果你花了超过 x 的时间试图找出一个问题,那就走开。稍后再回来。”
猜你喜欢
  • 1970-01-01
  • 2012-10-21
  • 1970-01-01
  • 2015-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-09
  • 1970-01-01
相关资源
最近更新 更多