javascript – 悬停时的jQuery工具提示

前端之家收集整理的这篇文章主要介绍了javascript – 悬停时的jQuery工具提示前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要一个非常轻量级的工具提示,类似于这里发现的1,当你在“热门视频”下悬停视频链接时,工具提示逐渐消失,它停留在那里你甚至可以选择文本,直到你移动光标关闭它.当您将鼠标悬停在标签上时,Facebook和Google也有类似的样式工具提示以及堆栈溢出.有人可以提供轻量级的方法来做到这一点.

搜索并查看了许多现有的“插件”,但它们都有些臃肿.谢谢你的帮助

解决方法

这是一个非常简单的方法,你可以完成这个:
  1. var timeout;
  2.  
  3. function hide() {
  4. timeout = setTimeout(function () {
  5. $("#tooltip").hide('fast');
  6. },500);
  7. };
  8.  
  9. $("#tip").mouSEOver(function () {
  10. clearTimeout(timeout);
  11. $("#tooltip").stop().show('fast');
  12. }).mouSEOut(hide);
  13.  
  14. $("#tooltip").mouSEOver(function () {
  15. clearTimeout(timeout);
  16. }).mouSEOut(hide);

其中#tip是您要鼠标悬停的元素,以显示工具提示,#tooltip是实际的工具提示元素.

这是一个例子:http://jsfiddle.net/pvyhY/

如果你想将它包装在一个jQuery插件中:

  1. (function($) {
  2. $.fn.tooltip = function(tooltipEl) {
  3. var $tooltipEl = $(tooltipEl);
  4. return this.each(function() {
  5. var $this = $(this);
  6. var hide = function () {
  7. var timeout = setTimeout(function () {
  8. $tooltipEl.hide();
  9. },500);
  10.  
  11. $this.data("tooltip.timeout",timeout);
  12. };
  13.  
  14. /* Bind an event handler to 'hover' (mouSEOver/mouSEOut): */
  15. $this.hover(function () {
  16. clearTimeout($this.data("tooltip.timeout"));
  17. $tooltipEl.show();
  18. },hide);
  19.  
  20. /* If the user is hovering over the tooltip div,cancel the timeout: */
  21. $tooltipEl.hover(function () {
  22. clearTimeout($this.data("tooltip.timeout"));
  23. },hide);
  24. });
  25. };
  26. })(jQuery);

用法

  1. $(document).ready(function() {
  2. $("#tip").tooltip("#tooltip");
  3. });

添加更多功能,你最终会得到优秀的qTip plugin,我建议你也看看.

猜你在找的jQuery相关文章