【问题标题】:Tooltip add second style工具提示添加第二种样式
【发布时间】:2013-05-03 11:19:57
【问题描述】:

我的页面上有 2 个工具提示,我目前正在使用下面的代码,但我想以某种方式添加第二个 'rel=',这样我就不必复制所有 jQuery,计划是添加一个 div使用代码中显示的工具提示 id 和一个额外的 tooltip2 类,以便可以使用不同的样式。我已经在这个问题上摆弄了很多,只是无法弄清楚如何在那里获得一个单独的课程。

主要目的是使弹出窗口的工具提示 2 为黑色并带有白色文本。

我也有一个 jsfiddle,我一直在研究它:http://jsfiddle.net/XJZ9v/

jQuery:

  $( document ).ready( function()
  {
     var targets = $( '[rel~=tooltip]' ),
         target  = false,
         tooltip = false,
         title   = false;

     targets.bind( 'mouseenter', function()
     {
         target  = $( this );
         tip     = target.attr( 'title' );
         tooltip = $( '<div id="tooltip"></div>' );

         if( !tip || tip == '' )
             return false;

         target.removeAttr( 'title' );
         tooltip.css( 'opacity', 0 )
                .html( tip )
                .appendTo( 'body' );

         var init_tooltip = function()
         {
             if( $( window ).width() < tooltip.outerWidth() * 1.5 )
            tooltip.css( 'max-width', $( window ).width() / 2 );
             else
                 tooltip.css( 'max-width', 340 );

             var pos_left = target.offset().left + ( target.outerWidth() / 2 ) - ( tooltip.outerWidth() / 2 ),
                 pos_top  = target.offset().top - tooltip.outerHeight() - 20;

             if( pos_left < 0 )
             {
                 pos_left = target.offset().left + target.outerWidth() / 2 - 20;
                 tooltip.addClass( 'left' );
             }
             else
                 tooltip.removeClass( 'left' );

             if( pos_left + tooltip.outerWidth() > $( window ).width() )
             {
                 pos_left = target.offset().left - tooltip.outerWidth() + target.outerWidth() / 2 + 20;
                 tooltip.addClass( 'right' );
             }
             else
                 tooltip.removeClass( 'right' );

             if( pos_top < 0 )
             {
                 var pos_top  = target.offset().top + target.outerHeight();
                 tooltip.addClass( 'top' );
             }
             else
                 tooltip.removeClass( 'top' );

             tooltip.css( { left: pos_left, top: pos_top } )
                    .animate( { top: '+=10', opacity: 1 }, 50 );
         };

         init_tooltip();
         $( window ).resize( init_tooltip );

         var remove_tooltip = function()
         {
             tooltip.animate( { top: '-=10', opacity: 0 }, 50, function()
             {
                 $( this ).remove();
             });

             target.attr( 'title', tip );
         };

         target.bind( 'mouseleave', remove_tooltip );
         tooltip.bind( 'click', remove_tooltip );
     });
 });

CSS:

 .infoToolTip{
background-color: #D7DF23;
display: inline-block;
height: 18px;
width: 18px;
color: #000;
line-height: 28px;
font-size: 28px;
padding: 2px 5px 8px 5px;
text-align: center;
position: relative;
font-family: 'IM Fell English', serif;
font-style: italic;
margin: -5px 0 -5px 5px;
font-weight: normal;
cursor: default;
-webkit-border-radius: 14px;
-moz-border-radius: 14px;
border-radius: 14px;
-webkit-box-shadow: 1px 1px 1px 0px rgba(0, 0, 0, 0.4);
box-shadow: 1px 1px 1px 0px rgba(0, 0, 0, 0.4);
z-index: 1;
text-decoration: none;
 }


 .bondTen
 {
     text-align: center;
     color: #FFFFFF;
     background: #000000;

 }

 .bondTen:after /* triangle decoration */
 {
    border-top: 10px solid #000000;
}

 #tooltip
 {
    text-align: center;
    color: #000000;
     background: #D7DF23;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
-webkit-box-shadow: 1px 1px 1px 0px rgba(0, 0, 0, 0.4);
box-shadow: 1px 1px 1px 0px rgba(0, 0, 0, 0.4);
     position: absolute;
     z-index: 100;
     padding: 15px;
 }

#tooltip:after /* triangle decoration */
{
    width: 0;
    height: 0;
    border-left: 10px solid transparent;
    border-right: 10px solid transparent;
    border-top: 10px solid #D7DF23;
    content: '';
    position: absolute;
    left: 50%;
    bottom: -10px;
    margin-left: -10px;
  }

    #tooltip.top:after
    {
        border-top-color: transparent;
        border-bottom: 10px solid #D7DF23;
        top: -20px;
        bottom: auto;
    }

    #tooltip.left:after
    {
        left: 10px;
        margin: 0;
    }

    #tooltip.right:after
    {
        right: 10px;
        left: auto;
        margin: 0;
    }

    .bondTenTooltip{
        float: none !important;
        font-family: 'ChevinProDemiBold';
        font-size: 22px;
        padding: 0 5px 10px;
        -webkit-box-shadow: 1px 1px 1px 0px rgba(0, 0, 0, 0.2);
        box-shadow: 1px 1px 1px 0px rgba(0, 0, 0, 0.2);
        background-color: #FFF;
        display: inline-block;
        height: 18px;
        width: 18px;
        color: #666;
        line-height: 28px;
        text-align: center;
        position: relative;
        font-style: normal;
        margin: -5px 0 -5px 25px;
        font-weight: normal;
        cursor: default;
        -webkit-border-radius: 14px;
        -moz-border-radius: 14px;
        border-radius: 14px;
        z-index: 1;
    }

HTML:

  <abbr title="Tooltip 1" class="bondTenTooltip" rel="tooltip">1</abbr>

  <abbr title="Tooltip 2" class="bondTenTooltip" rel="tooltip">2</abbr>

【问题讨论】:

    标签: jquery html css styles tooltip


    【解决方案1】:

    您只需要再添加一个针对Tooltip 2 的类:

     <abbr title="Tooltip 2" class="bondTenTooltip bondTen" rel="tooltip">2</abbr>
    

    并将.bondTen 类声明移到.bondTenTooltip 之后

    Updated fiddle

    另外,如果你想改变tooltip本身,看看here

    编辑已修复,可在 chrome 中使用。 Fiddle

    【讨论】:

    • 弹出 div 我需要在黑色上显示白色,这使得它比这更棘手,并且
      是在悬停时添加的
    • @huddds 你看过第二个链接了吗?
    • 是的,弹出窗口似乎在那个 JS 中根本不起作用,至少在我的 crome 浏览器上不起作用,没有尝试过其他任何方法
    • 我只在 Firefox 中测试过。我会修复并发布结果
    • 谢谢,这就是我想要的东西
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签