使用css

时间:2016-05-15 22:49:29

标签: html css

就像标题所说的那样,当鼠标悬停在图像的某个部分上时,我想在图像上放置一个文本框。

当鼠标悬停在图像的任何部分而不是特定部分时,我能够做到这一点。

这是我的HTML:

<h1>World Map</h1>
<div class="map">
  <img src="assets/strangemap.png" usemap="#testing">
  <map name="testing">
    <area shape="rect" coords="100,100,200,200" class="overlay">
  </map>
  <span class="text-content"><span>Strange World</span></span>
</div>

这是我的css:

.map {
  display: inline-block;
  position: relative;
  border-top: 3px solid $gray-lighter;
  border-left: 3px solid $gray-lighter;
  border-bottom: 3px solid $gray-light;
  border-right: 3px solid $gray-light;
}

.overlay {
  cursor: default;
}

span.text-content {
  background: rgba(0,0,0,0.5);
  color: white;
  display: table;
  height: 100px;
  left: 0;
  position: absolute;
  top: 0;
  width: 100px;
  opacity: 0;
  -webkit-transition: opacity 500ms;
  -moz-transition: opacity 500ms;
  -o-transition: opacity 500ms;
  transition: opacity 500ms;
}

span.text-content span {
  display: table-cell;
  text-align: center;
  vertical-align: middle;
}

.overlay:hover span.text-content {
  opacity: 1;
}

谢谢!

1 个答案:

答案 0 :(得分:1)

你试过这条CSS线吗?

.map map:hover + span { opacity: 1 }

这是一个有效的例子:

&#13;
&#13;
.map {
  display: inline-block;
  position: relative;
  border-top: 3px solid $gray-lighter;
  border-left: 3px solid $gray-lighter;
  border-bottom: 3px solid $gray-light;
  border-right: 3px solid $gray-light;
}
.overlay {
  cursor: default;
}
span.text-content {
  background: rgba(0, 0, 0, 0.5);
  color: white;
  display: table;
  height: 100px;
  left: 0;
  position: absolute;
  top: 0;
  width: 100px;
  opacity: 0;
  -webkit-transition: opacity 500ms;
  -moz-transition: opacity 500ms;
  -o-transition: opacity 500ms;
  transition: opacity 500ms;
}
span.text-content span {
  display: table-cell;
  text-align: center;
  vertical-align: middle;
}

img {
  width: 300px;
  height: 300px;
}
.map map:hover + span { opacity: 1 }
&#13;
<h1>World Map</h1>

<div class="map">
  <img src="assets/strangemap.png" usemap="#testing">
  <map name="testing">
    <area shape="rect" coords="100,100,200,200" class="overlay">
  </map>
  <span class="text-content"><span>Strange World</span></span>
</div>
&#13;
&#13;
&#13;