首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >elemnt:悬停-文本和背景

elemnt:悬停-文本和背景
EN

Stack Overflow用户
提问于 2016-04-22 11:35:13
回答 2查看 25关注 0票数 0

我需要文本出现在图像上,一旦你悬停它,我也需要它影响不透明度。

看看这支笔http://codepen.io/anon/pen/NNBgbQ

代码语言:javascript
复制
 <div class="flex-menu">
 <div class="menu-container">
 <img class="menu-image" src="http://www.libbyroach.ca/wp-content/uploads/2014/11/Adams-Sandwich.jpg" alt="Sandwitch">
 <div class="menu-description">

 <h4>Sandwitch</h4>
 <p>Powder marshmallow marshmallow brownie carrot cake candy bonbon. Sweet sugar plum gummies caramels tart carrot cake tiramisu cheesecake. Cheesecake biscuit jelly beans. Jelly-o icing chocolate macaroon. </p>
 </div>
 </div>
 </div>

我想要的结果是:

悬停,图像改变其不透明度和文本出现在它的中间-任何文本,不需要当前标题和段落。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-04-22 11:40:39

使用'position:abolsute定位文本div以覆盖图像是开始。

.menu-container div获取position:relative来提供定位上下文。

然后将:hover触发器切换到包装器,同时触发两者。

代码语言:javascript
复制
.flex-menu,
.menu-container,
.menu-image {
  width: 500px;
}
.menu-container {
  position: relative;
}
.menu-description {
  display: none;
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
}
.flex-menu {
  background-color: #fd5c63;
}
.menu-image {
  transition: all 0.3s ease 0s;
  display: block;
}
.flex-menu:hover .menu-image {
  opacity: 0.2;
}
.flex-menu:hover .menu-description {
  display: block;
}
代码语言:javascript
复制
<div class="flex-menu">
  <div class="menu-container">
    <img class="menu-image" src="http://www.libbyroach.ca/wp-content/uploads/2014/11/Adams-Sandwich.jpg" alt="Sandwitch">
    <div class="menu-description">

      <h4>Sandwitch</h4>
      <p>Powder marshmallow marshmallow brownie carrot cake candy bonbon. Sweet sugar plum gummies caramels tart carrot cake tiramisu cheesecake. Cheesecake biscuit jelly beans. Jelly-o icing chocolate macaroon.</p>
    </div>
  </div>
</div>

票数 1
EN

Stack Overflow用户

发布于 2016-04-22 11:42:02

position: absolute;在图像上定位文本

我已经将:hover.menu-image迁移到了.menu-container,一些转换更改以使其看起来更好:

代码语言:javascript
复制
.flex-menu,
.menu-container,
.menu-image {
  width: 500px;
}
.flex-menu {
  background-color: #fd5c63;
}
.menu-image {
  opacity: 1;
  display: block;
  transition: opacity 300ms ease-in-out;
}
.menu-container:hover .menu-image {
  opacity: 0.2;
}
.menu-container {
  position: relative;
}
.menu-description {
  position: absolute;
  left: 0;
  top: 0;
  z-index: 8;
  transition: opacity 300ms ease-in-out;
  opacity: 0;
}
.menu-container:hover .menu-description {
  opacity: 1;
}
代码语言:javascript
复制
<div class="flex-menu">
  <div class="menu-container">
    <img class="menu-image" src="http://www.libbyroach.ca/wp-content/uploads/2014/11/Adams-Sandwich.jpg" alt="Sandwitch">
    <div class="menu-description">

      <h4>Sandwitch</h4>
      <p>Powder marshmallow marshmallow brownie carrot cake candy bonbon. Sweet sugar plum gummies caramels tart carrot cake tiramisu cheesecake. Cheesecake biscuit jelly beans. Jelly-o icing chocolate macaroon.</p>
    </div>
  </div>
</div>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36792787

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档