在表格中创建超级简单的CSS提示工具,为什么它没有显示,我该如何使其生效?

9
我一直在为客户的网页尝试实现许多不同的工具提示,他坚持要求我们在订单页面上悬停在产品名称上时显示产品图片。我决定使用超级简单的CSS工具提示,它非常容易实现并且正好符合我们的要求。它可以在动态页面上工作,而我尝试过的其他工具提示则不能。

我在这里做了一个示例:CSS tooltip in table example。<-- 更新以去除错误

HTML:

<table class="mytable" id="cart">

  <tr id="titles">
    <th id="varekodetext">Varekode</th>
    <th id="produkttext">Produkt</th>
    <th id="pristext">Pris</th>
    <th id="emptee">&nbsp;</th>
    <th id="antalltext">Antall</th>
    <th id="pristotaltext">Pris Total</th>
    <th id="sletttext">Slett</th>
  </tr>
  <tbody>
    <tr class="even first" id="topborder" height="40px">
      <td class="cart2Varekode"> <span>39261-02 </span>
      </td>
      <td class="cart2Produkt"> <a href="/Plantronics-CS361N.11" target="_blank" class="tooltip" title="Plantronics CS361N"> 
                Plantronics CS361N
                <span> 
                    <img src="http://www.euroworker.no/public/upload/productimage/220-353-2.jpg" alt="Plantronics CS361N" /> 

                </span> 
                </a>
        <p>
        </p>
      </td>
      <td class="cart2Price">
        <span class="actualPrice">2390.-</span>
      </td>
      <td class="cart2Salg">
        <span class="orderlistPrice"></span>
      </td>
      <td class="antallbox">
        <span class="cartQuant"><input name="item_1363" class="DG-spinner" id="text" type="text" value="1"/> <span class="cartUpdate"><button type="submit" class="submit" id="rfrsh_btn" name="saveFields" title="Oppdater" value="">&nbsp;</button></span>
        </span>
      </td>
      <td class="cart2Total">
        <span class="basePrice">2390.-</span><span class="actualPrice">2390.-</span>
      </td>
      <td class="delete">
        <a class="slett" href="../..//order/delete/1363?return=" title="Slett">Remove</a>
      </td>
    </tr>
    <tr class="odd last" id="topborder" height="40px">
      <td class="cart2Varekode"> <span>7050-20</span>
      </td>
      <td class="cart2Produkt"> <a href="/Target-7050CC-Duo-UNC.7" target="_blank" class="tooltip" title="Target 7050CC Duo UNC">Target 7050CC Duo UNC<span>
      <img src="upload/productimage/7-250-2.jpg?1251022192" alt="Target 7050CC Duo UNC" />               
                </span> 
                </a>

        <p>
          <div class="productOptions" style="color:#b2b2b2;">

            <div class="nonEditableOption">
              Skal tilkobles: Alcatel IP Touch
            </div>

            <div class="productOptionsMenu">
              <a style="color:#6c8aa2;" href="/order/options/1377" ajax="/order/optionForm/1377">Endre valg</a>
            </div>
          </div>

        </p>

      </td>
      <td class="cart2Price"><span class="actualPrice">899.-</span>
      </td>
      <td class="cart2Salg">
        <span class="salg" title="Rabatt">&nbsp;</span>

        <span class="orderlistPrice" title="Opprinnelige prisen"> 
                    (1599.-)
                </span>
      </td>
      <td class="antallbox">
        <span class="cartQuant"><input name="item_1377" class="DG-spinner" id="text" type="text" value="1"/> 

                    <span class="cartUpdate"><button type="submit" class="submit" id="rfrsh_btn" name="saveFields" title="Oppdater" value="">&nbsp;</button></span>
        </span>
      </td>


      <td class="cart2Total">

        <span class="basePrice">899.-</span><span class="actualPrice">899.-</span>

      </td>
      <td class="delete">

        <a class="slett" href="../..//order/delete/1377?return=" title="Slett">Remove</a>

      </td>
    </tr>
    <tr>
      <td colspan="6" class="cols-six"></td>
    </tr>
    <tr id="topborder-tr">
      <td class="subTotalCaption2">&nbsp;</td>
      <td colspan="4" class="subTotalCaption2">Mva (25%):</td>
      <td class="amount taxAmount2">822.-</td>
      <td>&nbsp;</td>



    </tr>
    <tr>
      <td class="subTotalCaption2">&nbsp;</td>
      <td colspan="4" class="subTotalCaption2">Totalt:</td>
      <td class="subTotal2">4111.-</td>
      <td>&nbsp;</td>

    </tr>
    <tr>
      <td colspan="5"></td>
      <td class="cartQuant"></td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td colspan="7">
        <a href="/" class="continueShopping"><span>&nbsp;</span></a>
        <div class="checkoutButtons">
          <a href="/checkout" class="proceedToCheckout" onclick="return Order.submitCartForm(this);"><span>&nbsp;</span></a>

        </div>
      </td>
    </tr>
    <script type="text/javascript">
      new Order.OptionLoader($('cart'));

    </script>

  </tbody>

</table>

CSS:

.cart2Produkt a:hover {
  background: #ffffff;
  text-decoration: none;
  z-index: 999;
}

/*BG color is a must for IE6*/

.tooltip {
  text-align: left;
}

.cart2Produkt a.tooltip span {
  z-index: 999;
  display: block;
  position: absolute;
  left: -999px;
  opacity: 0;
  padding: 2px 3px;
  margin-left: 8px;
  width: 130px;
  -webkit-transition: opacity;
  -webkit-transition-timing-function: ease-in;
  -webkit-transition-duration: 500ms;
}

.cart2Produkt a.tooltip:hover span {
  z-index: 999;
  display: block;
  position: absolute;
  right: 50%;
  opacity: 1;
  background: #ffffff;
  border: 1px solid #cccccc;
  color: #6c6c6c;
  top: -35px;
  left: -15px;
  z-index: 10;
  border-radius: 5px;
  -moz-border-radius: 5px;
  -webkit-border-radius: 5px;
  text-align: center;
  vertical-align: middle;
  padding: 1px;
  -webkit-transition: opacity;
  -webkit-transition-timing-function: ease-out;
  -webkit-transition-duration: 500ms;
}


/**************IGNORE BELOW*************/

#topborder {
  /*border-top: 1px #ccc solid;*/
  padding-bottom: 10px;
}

#topborder-tr {
  border-top: 1px #ccc solid;
}

.mytable {
  width: 100%;
  margin-bottom: 20px;
}

.mytable,
.mytable th,
.mytable tr,
.mytable td {
  border: 0;
  letter-spacing: 1px;
}

.mytableborders,
.mytableborders th,
.mytableborders tr,
.mytableborders td {
  border: 1px #ccc solid;
  /*width:670px;*/
}

.mytable td,
.mytable td span {
  padding-bottom: 0;
  padding-top: 5px;
  /*border-top: 1px #ccc solid;*/
  vertical-align: middle;
  font-size: 12px;
}

.mytable th {
  vertical-align: bottom;
  height: 30px;
  padding-bottom: 5px;
}

#titles {
  margin: 0px auto 0px auto;
  width: 100%;
  padding-bottom: 50px;
  z-index: 1;
  border: 1px #ccc solid;
  border-left: none;
  border-right: none;
}

.cart2Produkt a {
  color: #0a5692;
  text-decoration: none;
  line-height: 15px;
  clear: both;
  z-index: 999;
}

productOptionsMenu {
  clear: both;
}

#pristotaltext p,
#varekodetext p,
#sletttext p,
#pristext p,
#antalltext p,
#produkttext p {
  font: 12px/11px "Helvetica";
  margin: 0px;
  color: #828273;
  text-align: left;
}

#thinline,
#thinlinecopy {
  background: url(../../upload/thinline.png) no-repeat;
  visibility: visible;
  position: absolute;
  left: 0px;
  z-index: 4;
  width: 747px;
  height: 1px;
}

#thinlinefakt {
  background: url(../../upload/thinline.png) no-repeat;
  visibility: visible;
  position: absolute;
  top: 100px;
  left: 0px;
  z-index: 4;
  width: 747px;
  height: 1px;
}

#thinlinefakt2 {
  background: url(../../upload/thinline.png) no-repeat;
  background-position: bottom;
  visibility: visible;
  position: absolute;
  top: 205px;
  left: 0px;
  z-index: 4;
  width: 747px;
  height: 28px;
  margin: 5px;
  margin-left: 10px;
  margin-top: 0px;
  padding: 5px;
  color: #000000;
  font-size: 22px;
  font-family: "Helvetica";
}

.Fadresse {
  display: block;
  margin: 5px;
  padding: 0;
  color: #000000;
  font-size: 22px;
  font-family: "Helvetica";
}

#thinlinecopylever {
  background: url(../../upload/thinline.png) no-repeat;
  background-repeat: repeat-x;
  position: relative;
  left: 0px;
  top: -3px;
  width: 100%;
  height: 1px;
  padding: 0px;
}

#varekodetext {
  width: 65px;
  padding-left: 5px;
  text-align: center;
}

#produkttext {
  width: 150px;
}


/*#sgproductview  {

margin: 0px auto 0px auto;
width: 595px;
height:1%;
z-index:1;
}*/

#thinlinecopy {
  top: 1px;
}

#antalltext {
  width: 25px;
  text-align: center;
}

#pristotaltext {
  width: 10%;
  text-align: right;
}

#sletttext {
  width: 10%;
  text-align: center;
}

#thinline {
  top: 19px;
  position: absolute;
}

#pristext {
  width: 24px;
  text-align: center;
}

#emptee {
  background-color: none;
  width: 40px;
}


/*****************************************CONTENT*/

.cart2Produkt p,
.cart2Varekode p,
.cart2Salg p {
  font: 13px/11px "Helvetica";
  margin: 0px;
  letter-spacing: 1px;
  text-align: left;
}

.cart2Produkt p a {
  color: #000000;
}

.cart2Total p {
  font: 13px/11px "Helvetica";
  margin: 0px;
  letter-spacing: 1px;
  text-align: left;
  color: #000000;
}

.cart2Varekode {
  visibility: visible;
  z-index: 2;
  text-align: center;
  vertical-align: top;
  padding-top: 3px;
  font-weight: bold;
}

.cart2Produkt {
  visibility: visible;
  text-decoration: none;
  text-align: left;
  position: relative;
  width: 28%;
  font-weight: bold;
  padding-bottom: 2px;
}

.cart2Produkt a {
  white-space: nowrap;
}

.cart2Price {
  visibility: visible;
  z-index: 2;
  text-align: right;
  width: 6%;
  position: relative;
  font-weight: bold;
}

.cart2Price p {
  font: 13px/11px "Helvetica";
  margin: 0px;
  letter-spacing: 1px;
  text-align: right;
  width: 80%;
}

.cart2Total p {
  font-weight: bold;
  text-align: right;
}

.cart2Salg p {
  color: #B2B2B2;
  width: 20%;
}

.cart2Total {
  visibility: visible;
  z-index: 2;
  text-align: right;
  font-weight: bold;
}

.antallbox {
  white-space: nowrap;
  text-align: center;
}

.antallbox input {
  text-align: right;
  outline: none;
  width: 30px;
}

.antallbox input:focus {
  text-align: right;
  outline: none;
  border: 1px #000 solid;
  background-color: #F0F7FD;
}

.cartQuant {
  width: 30px;
  /*    white-space: nowrap;*/
  text-align: left;
  margin-top: 5px;
}

.cart2Salg {
  visibility: visible;
  z-index: 2;
  padding: 0;
  margin: 0;
}

.orderlistPrice {
  text-decoration: line-through;
  font-weight: bold;
  color: #b2b2b2;
  font-size: 12px;
}

.salg {
  background: url(../../upload/salg.png) no-repeat;
  visibility: visible;
  z-index: 2;
  width: 28px;
  height: 24px;
  display: inline-block;
  position: relative;
}

.delete {
  visibility: visible;
  z-index: 2;
  height: 21px;
  background: none;
  text-align: center;
}

.slett {
  width: 24px;
  height: 19px;
  background-image: url(../../upload/delete_box_sprite.png);
  background-position: 0 0;
  text-decoration: none;
  display: inline-block;
}

.slett:hover {
  width: 24px;
  height: 19px;
  background-image: url(../../upload/delete_box_sprite.png);
  background-position: -24px 0;
  text-decoration: none;
  display: inline-block;
}

.productOptions {
  background-color: #fff;
  postion: absolute;
  visibility: visible;
  display: block;
  top: 3px;
  padding-left: 5px;
  padding-bottom: 10px;
}

.nonEditableOption {
  background-color: #fff;
  float: left;
  postion: absolute;
  margin-right: 10px;
  visibility: visible;
  display: block;
}

这个页面以前是使用div显示的,现在我已经将其改为表格,因为它是表格数据且更易于处理。当它使用div时工作正常,但现在它在表格中,悬停时不会显示span。

我的问题是:

  1. 为什么它不起作用?

  2. 我该如何使其工作?

  3. 如果不行,有没有其他超级容易实现的工具提示可以在动态页面上正常工作?

以下是DIV工具提示的参考:DIV显示工具提示

HTML:

<div id="JSwrap">
    <div id="cart2Produkt">
        <p><a href="/Target-7050-Softphone-USB-Duo.220" target="_blank" class="tooltip" title="Target 7050 Softphone USB Duo ">Target 7050 Softphone USB Duo
    <span><img src="http://www.euroworker.no/public/upload/productimage/220-353-2.jpg?1251413379" alt="Target 7050 Softphone USB Duo " />
<br /> 
  </span> 
</a>
        </p>
    </div>
</div>

CSS:

#JSwrap {
  /*for jsfiddle only*/
  position: absolute;
  left: 100px;
  top: 50px;
}

#cart2Produkt {
  border: 1px solid #ccc;
  width: 500px;
  text-align: left;
  padding: 10px;
}

#cart2Produkt a.tooltip span {
  z-index: 999;
  display: block;
  position: absolute;
  left: -999px;
  opacity: 0;
  padding: 2px 3px;
  margin-left: 8px;
  width: 130px;
  -webkit-transition: opacity;
  -webkit-transition-timing-function: ease-in;
  -webkit-transition-duration: 500ms;
}

#cart2Produkt a.tooltip:hover span {
  z-index: 999;
  display: block;
  position: absolute;
  right: 50%;
  opacity: 1;
  background: #ffffff;
  border: 1px solid #cccccc;
  color: #6c6c6c;
  top: -35px;
  left: -15px;
  z-index: 10;
  border-radius: 5px;
  -moz-border-radius: 5px;
  -webkit-border-radius: 5px;
  text-align: center;
  vertical-align: middle;
  padding: 1px;
  -webkit-transition: opacity;
  -webkit-transition-timing-function: ease-out;
  -webkit-transition-duration: 500ms;
}

#cart2Produkt img {
  z-index: 999;
}

编辑:刚注意到它在IE8中只能半工作。

更新:由于Daniel指出我的CSS错误,现在它可以在所有浏览器中正常工作了,但有没有办法使其显示在表格单元格之外?


如果你将 span 显示为块级元素,让它始终可见,那么图片会显示吗?(我没能成功) - Tom
它已经设置为阻止,我也尝试了 inline-block,但这并没有起作用。 - Kyle
4个回答

20
在这里找到一个更易读的可行解决方案:链接

.cart { width: 100%; }

.hasTooltip span {
    display: none;
    color: #000;
    text-decoration: none;
    padding: 3px;
}

.hasTooltip:hover span {
    display: block;
    position: absolute;
    background-color: #FFF;
    border: 1px solid #CCC;
    margin: 2px 10px;
}
<table class="cart"> 
    <tr> 
        <th id="pos">Pos</th> 
        <th id="name">Product</th> 
        <th id="price">Price</th> 
    </tr> 
    <tbody> 
        <tr>
            <td>1</td>
            <td>
                <a href="productdetails.htm" class="hasTooltip">Flatscreen 
                    <span>New visual experience!</span>
                </a>
            </td>
            <td>19.99</td>
        </tr>
        <tr>
            <td>2</td>
            <td>
                <a href="productdetails.htm" class="hasTooltip">Headset 
                    <span>Inject music directly into your ears!</span>
                </a>
            </td>
            <td>19.99</td>
        </tr>
    </tbody> 
</table>


非常有帮助!比我的版本简单多了 :) +1 - Kyle
我将其添加到我的网站上,与之前的版本相同,只是在表格单元格的范围内显示span。有没有一种方法可以覆盖它?在这里可以正常工作:http://jsfiddle.net/Mcv7P/,但在我的实际网站上却不能。 - Kyle
1
你的CSS包含许多z-index定义。清理它们,或将z-index: 99999;添加到类.hasTooltip span { ... }中以解决此问题。你问题的另一个原因可能是不同的display: ...值相互覆盖。将我示例中用于工具提示的两个CSS类放在CSS定义的底部,或者为这些属性添加!important - stefanglase
好的,搞定了,将cart2Produkt的位置改为absolute,hasTooltip改为relative。不过这又产生了另一个问题... - Kyle
只有在您没有可滚动页面的情况下才能使用此方法。我有一个包含数据的长表格。我在某些列上使用工具提示。随着页面向下滚动,工具提示的位置计算变得越来越混乱。在某个时候,它会超出视口范围。 - A.T.

2
您似乎遇到了CSS问题。只需使用工作的JSFiddle演示中的样式即可解决问题。您将span设置为display:none,然而这个提示框是通过使用left:-999px从屏幕上隐藏图像来工作的。
请查看: http://jsfiddle.net/WNzhJ/

谢谢Damiel,我确实尝试使样式保持一致。 - Kyle
不错的例子,但有点复杂。 - zero_cool

1

我已经检查了你的代码,正如你可以通过jsfiddle语法高亮看到,你的代码中有很多错误:例如,你的a标签内开始的span标签却在a标签外结束。CSS应该像这样工作。还有一些其他的错误。我认为如果你纠正了它们,它就会工作。(只要通过w3c检查,那么一切都应该没问题)

编辑:我错过了一个东西,你的链接应该是position: relative;因为你的提示框是position: absolute,absolute始终相对于最后一个relative父元素。

同时删除原始span中的opacity: 0。


更改位置,仍然无效。在IE8中仍然只能半工作,它会显示隐藏的span,但仅限于表格单元格的范围内。 - Kyle
看,它可以工作:http://jsfiddle.net/dbZTK/6/。在您原始的span中有一个opacity: 0(它在IE中工作,因为IE不支持CSS opacity,它使用自己的垃圾;P)。 - meo

1
我曾尝试同样的事情,试图让CSS提示框出现在表格单元格外部。在我的情况下,表格单元格仅为1px宽,因此必须显示在外部。我发现通过使:hover spanposition: absolute;,然后添加margin: -37px 0px 0px -50px;,可以用来移动工具提示。但是!如果我尝试调整上/左/下/右,那么绝对设置会将其绝对定位在屏幕上,而不是相对于单元格。希望这能对某些人有所帮助。

1
谢谢,我搞不清楚怎么在 position: absolute 的情况下移动它! - user1810292

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接