【问题标题】:Make title attribute appear instantly on hover使标题属性在悬停时立即出现
【发布时间】:2014-10-08 14:38:14
【问题描述】:

是否有可能使锚标记的标题属性在鼠标悬停时立即出现,而不仅仅是几秒钟后。 JavaScript/jQuery 和 CSS 中的任何解决方案都很好。

【问题讨论】:

  • 没有原生的title属性,没有;使用 CSS 你可以模拟它(对于大多数非空元素)。
  • 延迟是由浏览器控制的,不是你可以通过编程改变的。
  • 这很接近jqueryui.com/tooltip
  • @DavidThomas 您可以使用 javascript 来获取 title 属性的内容,然后将其放入一些类似工具提示的元素中,不是吗?
  • @Woodrow:当然。

标签: javascript jquery html css


【解决方案1】:

title 属性的处理是依赖于浏览器的,并且没有定义任何 API 来控制它,更不用说在规范中指定了。这包括延迟、显示的持续时间、使用的字体、字体大小等。

还有其他技术可以使用代替title 属性。其中一些已在其他答案中提到。请注意,简单的“CSS 工具提示”可以在纯 CSS 中简单灵活地实现。使用这些技术时,要显示的数据通常title 属性中,因为它的处理依赖于浏览器,所以会有你的特殊工具提示显示的风险 title 的浏览器实现。尽管可以防止后者,但启用脚本后,使用对任何内容都没有默认影响的属性会更简单,例如 data-title=...data-tooltip=...

【讨论】:

    【解决方案2】:

    一种方法:

    // textFrom : String, the attribute from which the text
    //            should come,
    // delta :    String or Number, the distance from the cursor at
    //            which the tooltip should appear
    function instantTooltips(textFrom, delta) {
      // if delta exists, and can be parsed to a number, we use it,
      // otherwise we use the default of 5:
      delta = parseFloat(delta) ? parseFloat(delta) : 5;
    
      // function to handle repositioning of the created tooltip:
      function reposition(e) {
        // get the tooltip element:
        var tooltip = this.nextSibling;
        // setting the position according to the position of the
        // pointer:
        tooltip.style.top = (e.pageY + delta) + 'px';
        tooltip.style.left = (e.pageX + delta) + 'px';
      }
    
      // get all elements that have an attribute from which we
      // want to get the tooltip text from:
      var toTitle = document.querySelectorAll('[' + textFrom + ']'),
        //create a span element:
        span = document.createElement('span'),
        // find if we should use textContent or innerText (IE):
        textProp = 'textContent' in document ? 'textContent' : 'innerText',
        // caching variables for use in the upcoming forEach:
        parent, spanClone;
      // adding a class-name (for CSS styling):
      span.classList.add('createdTooltip');
      // iterating over each of the elements with a title attribute:
      [].forEach.call(toTitle, function(elem) {
        // reference to the element's parentNode:
        parent = elem.parentNode;
        // cloning the span, to avoid creating multiple elements:
        spanClone = span.cloneNode();
        // setting the text of the cloned span to the text
        // of the attribute from which the text should be taken:
        spanClone[textProp] = elem.getAttribute(textFrom);
    
        // inserting the created/cloned span into the
        // document, after the element:
        parent.insertBefore(spanClone, elem.nextSibling);
    
        // binding the reposition function to the mousemove
        // event:
        elem.addEventListener('mousemove', reposition);
    
        // we're setting textFrom attribute to an empty string
        // so that the CSS will still apply, but which
        // shouldl still not be shown by the browser:
        elem.setAttribute(textFrom, '');
      });
    }
    
    // calling the function:
    instantTooltips('title', 15);
    [title] {
      display: block;
      margin: 0 0 1em 0;
    }
    
    /*
      hiding, and styling, the elements we'll be creating
    */
    [title] + span.createdTooltip {
      display: none;
      border: 2px solid #f90;
      background-color: rgba(255, 255, 255, 0.8);
      padding: 0.2em 0.5em;
      border-radius: 0.75em;
    }
    
    /*
      showing the created elements on hovering the element we want to
      show tooltips for
    */
    [title]:hover + span.createdTooltip {
      display: block;
      position: absolute;
    }
    <span title="This is the span's title">A span element</span>
    <img src="http://placekitten.com/g/250/250" title="A kitten." />
    <input title="This is an input element's title." value="This input has a title" />

    参考资料:

    【讨论】:

    • 我在我的 react 应用程序中使用了它,它就像一个魅力。一段精彩的代码大卫。 ?
    【解决方案3】:

    您不能使用默认工具提示来做到这一点,但您可以使用 jQuery 插件作为工具提示或引导程序。最好的方法是创建自定义工具提示。

    http://tech.pro/tutorial/930/jquery-custom-tooltips

    这里有一些你可以使用的参考资料

    简单提示:http://craigsworks.com/projects/simpletip/

    引导程序:http://getbootstrap.com/javascript/#tooltips

    【讨论】:

      【解决方案4】:

      Bootstraps ToolTip 插件在这方面做得很好,响应速度更快。

      只需要默认的引导文件即可运行。

      可以更改 CSS 以满足您的要求。

      更多信息和示例可以在这里显示:

      http://getbootstrap.com/javascript/#tooltips

      【讨论】:

        【解决方案5】:

        您可以在mouseover 上隐藏tite 并附加一个跨度。然后删除跨度并恢复mouseout上的标题

        $('a').hover(function(e){
            title = $(this).attr('title');
            $(this).append('<span>Im Super Fast!!!</span>')
            $(this).removeAttr('title');
        },
        function(e){
            $('span', this).remove();
            $(this).attr('title',title);
        });
        

        查看示例 - http://jsfiddle.net/1z3catx3/1/

        注意:您当然需要设置 span 的样式

        【讨论】:

          【解决方案6】:

          用 box-shadow 和边框数组托盘这个:

          $('a').hover(function(e){
          
              title = $(this).attr('alt');
              $(this).append('<span>'+title+'</span>')
          
          
          },
          function(e){
              $('span', this).remove();
          
          
          });
          

          http://jsfiddle.net/1z3catx3/112/

          【讨论】:

            【解决方案7】:
            <!DOCTYPE html>
            <html>
            
            <head>
              <style>
                
                [data-title]:hover:after {
                  opacity: 1;
                  transition: all 0.1s ease 0.5s;
                  visibility: visible;
                }
                
                [data-title]:after {
                  content: attr(data-title);
                  position: absolute;
                  bottom: -1.6em;
                  z-index: 99999;
                  visibility: hidden;
                  white-space: nowrap;
                  background-color: lightgray;
                  color: #111;
                  font-size: 90%;
                  font-weight: lighter;
                  padding: 1px 5px 2px 5px;
                  box-shadow: 1px 1px 3px #222222;
                  opacity: 0;
                  border: 1px solid #111111;
                  border-radius: 5px 5px 5px 5px;
                }
                
                [data-title] {
                  position: relative;
                }
              </style>
            </head>
            <body>
            
            <p data-title="also you can put it here for toltip other element ">my element </p>
            
            </body>
            

            【讨论】:

            • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
            猜你喜欢
            • 1970-01-01
            • 2020-11-27
            • 2014-09-22
            • 1970-01-01
            • 1970-01-01
            • 2014-06-02
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多