javascript - 如何在绝对定位元素上正确定位工具提示?

标签 javascript jquery css css-position qtip2

我有一堆绝对定位的图像,我试图根据元素上指定的数据属性来显示工具提示。由于某种原因,工具提示的定位始终处于关闭状态。我已经包含了一个演示。有人能发现这个问题吗?

$("div.thing").each(function (i) {
  var objthis = this;
  $(this).qtip({
    show: 'click',
    hide: 'click',
    content: {
      text: 'hey'
    },
    position: {
      corner: {
        tooltip: 'topLeft',
        target: 'topLeft'
      },
      adjust: {
        x: $(objthis).data('left'),
        y: $(objthis).data('top')
      }
    }
  });
});
div#container {
  position: relative;
  width: 100px;
  height: 100px;
  background-color: black;
}

div.thing {
  position: absolute;
  z-index: 1;
  border: 0;
  height: 10px;
  width: 10px;
  top: 50px;
  left: 20px;
  background-color: red;
}
<link href="http://cdn.jsdelivr.net/qtip2/2.2.1/jquery.qtip.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://cdn.jsdelivr.net/qtip2/2.2.1/jquery.qtip.min.js"></script>
<div id="container">
  <div class="thing" data-top="50" data-left="20"></div>
</div>

最佳答案

通过在 qtip 设置中设置 offset,您会将工具提示移离图像太远。尝试删除偏移量:

$("div.thing").each(function (i) {
  var objthis = this;
  $(this).qtip({
    show: 'click',
    hide: 'click',
    content: {
      text: 'hey'
    },
    position: {
      corner: {
        tooltip: 'topLeft',
        target: 'topLeft'
      },
    }
  });
});
div#container {
  position: relative;
  width: 100px;
  height: 100px;
  background-color: black;
}

div.thing {
  position: absolute;
  z-index: 1;
  border: 0;
  height: 10px;
  width: 10px;
  top: 50px;
  left: 20px;
  background-color: red;
}
<link href="http://cdn.jsdelivr.net/qtip2/2.2.1/jquery.qtip.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://cdn.jsdelivr.net/qtip2/2.2.1/jquery.qtip.min.js"></script>
<div id="container">
  <div class="thing" data-top="50" data-left="20"></div>
</div>

关于javascript - 如何在绝对定位元素上正确定位工具提示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25915905/

相关文章:

html - 如何制作三图像网格系统?

ios - IOS (Safari) 上的 Bootstrap hamburgermenu(折叠)可见性问题

javascript - 允许查询字符串有两个参数: `` ?``

处理程序附加到的 Javascript 获取元素

java - 使用 Spring3 mvc 时出现 ajax 问题。返回对象列表时出现 406 错误

javascript - 对于每个项目,item[if].id 不起作用。有任何想法吗?

javascript - 如何实现类似 facebook 的头像上传?

css - JSF dataTable 重新呈现丢失 CSS 样式

javascript - 响应式导航不起作用(React)

javascript - 当前基于网络的 ePub 阅读器