悬停工具提示显示图像

时间:2015-09-25 08:48:10

标签: jquery html css tooltip mousehover

我试图在悬停时在工具提示中显示图像。 我找到了这个解决方案,这很棒,我需要这样的东西:

Codepen

CSS

@import "compass";

* {
  box-sizing: border-box;
}

.tooltip {
  position: relative;
  border-bottom: 1px dotted #ccc;
  cursor: help;

  &:before, &:after {
    @include single-transition($duration: .6s);
    visibility: hidden;
  }

  &:before {
    @include opacity(0);
    position: absolute;
    bottom: 0;
    left: 50%;
    margin-left: -6px;
    content: '\00a0';
    height: 0;
    width: 0;
    border-bottom: 6px solid #363636;
    border-left: 6px solid transparent;
    border-right: 6px solid transparent;
  }

  &:after {
    @include opacity(0);
    @include border-radius(4px);
    position: absolute;
    content: attr(title);
    top: 100%;
    left: -12px;
    width: 100%;
    min-width: 120px;
    margin: 0;
    padding: 8px 10px;
    background: #363636;
    color: #fff;
    font-size: 1.2rem;
    font-weight: normal;
    text-align: center;
  }

  &:hover {

    &:after, &:before {
      @include opacity(1);
      visibility: visible;
    }

    &:before {
      bottom: -6px;
    }

    &:after {
      margin-top: 6px;
    }
  }
}

// Demo

html {
  font-size: 62.25%;
}

body {
  font-size: 1.6rem;
}

h1 {
  font-size: 2.4rem;
}

.wrapper {
  max-width: 32em;
  padding: 12px;
  margin: 0 auto;
  font-family: sans-serif;
  line-height: 1.4;
}

HTML

<div class="wrapper">
  <h1 class="tooltip" title="Tooltip">Pure CSS Tooltip</h1>
  <p><span class="tooltip" title="I'm a Tooltip">Lorem ipsum dolor sit amet</span> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Numquam molestiae, quis accusamus fugiat officiis amet ullam, inventore vero! Officia iusto totam quis amet consequatur numquam nesciunt alias deleniti eos quas?<span class="tooltip" title="Another Tooltip">
  Lorem ipsum</span> dolor sit amet, consectetur adipisicing elit. Beatae ut nihil labore at reiciendis placeat modi, Lorem ipsum dolor sit ameteum quia delectus similique accusamus sed soluta sunt aliquid repudiandae assumenda a illo molestiae.
    <span class="tooltip" title="One more Tooltip">Lorem ipsum</span> dolor sit amet, consectetur adipisicing elit. Deserunt incidunt et earum repellendus, maxime neque voluptate molestias quod at illo maiores tempora suscipit, deleniti ipsum nesciunt, animi tempore, esse cum?
  </p>
</div>

但问题是title属性只能是 text ,我需要显示图像。这可以改变,如果是这样,怎么样?还是有另一种解决方案吗?

提前谢谢你。

PS:很抱歉,这听起来像是一个愚蠢的问题,因为它是我在 Stackoverflow 的第一个问题。

3 个答案:

答案 0 :(得分:0)

你可以这样做:

的CSS:

* {
  box-sizing: border-box;
}

.tooltip {
  position: relative;
  border-bottom: 1px dotted #ccc;
  cursor: help;
}

:before, :after {
  visibility: hidden;
}

:before {
  position: absolute;
  bottom: 0;
  left: 50%;
  margin-left: -6px;
  content: '\00a0';
  height: 0;
  width: 0;
  border-bottom: 6px solid #363636;
  border-left: 6px solid transparent;
  border-right: 6px solid transparent;
}

:after {
  position: absolute;
  content: attr(title);
  top: 100%;
  left: -12px;
  width: 100%;
  min-width: 120px;
  margin: 0;
  padding: 8px 10px;
  background: #363636;
  color: #fff;
  font-size: 1.2rem;
  font-weight: normal;
  text-align: center;
}

:hover:after, :hover:before {
  visibility: visible;
}

:hover:before {
  bottom: -6px;
}

:hover:after {
  margin-top: 6px;
}

html {
  font-size: 62.25%;
}

body {
  font-size: 1.6rem;
}

h1 {
  font-size: 2.4rem;
}

.wrapper {
  max-width: 32em;
  padding: 12px;
  margin: 0 auto;
  font-family: sans-serif;
  line-height: 1.4;
}

DEMO

答案 1 :(得分:0)

欢迎使用Stackoverflow!不幸的是,没有机会将图像放到title属性中。你应该使用像jquery.ui这样的js库。请参阅此https://github.com/helhum/upload_example/blob/master/Configuration/TCA/tx_uploadexample_domain_model_example.php#L128

答案 2 :(得分:0)

如果您更专注于自定义工具提示,那么您应该选择

jQuery-powertip

由于您正在寻找图像工具提示,请按照此示例进行操作。

这是我的JSFiddle

<强> HTML

<div id="image_tooltip">
   <div>1914 translation by H. Rackham</div>
</div>

<强> CSS

#image_tooltip div {
   background-color: #EEE;
   border: 1px solid #CCC;
   border-radius: 3px;
   text-align: center;
   width: 400px;
   padding: 40px;
}

jQuery (您必须包含jQuery Lib以及powertip jQuery&amp; CSS:

/*image_tooltip examples*/
$('#image_tooltip div').data('powertip', $([
    '<img src="https://lh3.googleusercontent.com/-5khUd9Qwc-o/AAAAAAAAAAI/AAAAAAAAAZ0/IUp786Zkfgc/photo.jpg?sz=48" />'].join('\n')));
$('#element div').powerTip({
    followMouse: true
 });

按照说明,您将获得如下工具提示:

enter image description here

您可以轻松自定义其位置,工具提示行为及其外观。

希望这会对你有所帮助。