【问题标题】:How to create a css hover effect for show button如何为显示按钮创建 CSS 悬停效果
【发布时间】:2021-04-22 08:28:26
【问题描述】:

我是编程初学者,我正在尝试创建具有悬停效果的卡片。将鼠标悬停在卡片上时,我想为卡片添加不透明度并显示 4 个按钮,一个在另一个之上。你能帮帮我吗?

下面是我的代码。

.cardnew {
  height          : 256px;
  text-align      : center;
  width           : 246px;
  background      : #ffffff;
  margin          : 20px;
  display         : inline-block;
  box-shadow      : 1px 1px 22px rgba(157, 184, 209, 0.19);
  border-radius   : 1px;
  cursor          : pointer;
  transition      : 0.15s linear;
  }
.cardnew > img {
  height          : 150px;
  width           : 100%;
  border-radius   : 10px 10px 0 0;
  padding         : 35px 0 5px 0;
  }
.cardnew > h1 {
  font-size       : 22px;
  color           : #5c5c5c;
  padding         : 15px 0 5px 0;
  font-weight     : 100;
  }
.cardnew > p {
  font-size       : 13px;
  color           : #cdcdcd;
  padding         : 0 40px;
  line-height     : 20px;
  }
.cardnew > a {
  padding         : 13px;
  font-weight     : normal;
  width           : 260px;
  color           : #2687f1;
  border          : 2px solid #2687f1;
  border-radius   : 7px;
  margin          : auto;
  margin-top      : 35px;
  display         : block;
  transition      : 0.2s linear;
  }
.cardnew:hover {
  transform       : scale(1.015);
  transition      : 0.15s linear;
  box-shadow      : 1px 1px 22px rgba(157, 184, 209, 0.5);
  }
.cardnew:hover > a {
  padding         : 13px;
  width           : 260px;
  color           : #fff;
  background      : #2687f1;
  border-radius   : 7px;
  margin          : auto;
  margin-top      : 35px;
  transition      : 0.2s linear;
  }
.row-card {
  justify-content : space-around;
  flex-wrap       : wrap;
  margin-right    : -12.5px;
  margin-left     : -12.5px;
  }
<div class="row-card">
    <div class="cardnew">
      <img src="https://enginehosting-html.fruitfulcode.com/wp-content/themes/enginehosting-html/assets/images/server.svg" style="padding: 60px 0 5px 0;height: 125px;" />
      <h1>
        name 
      </h1>
      <p>firstname</p>
      <div class="button"><a href="#"> BUTTON </a>
      <div class="button"><a href="#"> BUTTON </a>
      <div class="button"><a href="#"> BUTTON </a>
      <div class="button"><a href="#"> BUTTON </a>
    </div>
</div>

我尝试了一些在 MDN 和 youtube 上找到的教程,但无法实现。任何帮助都会有很大帮助。 你能帮帮我吗?

喜欢这支笔:https://codepen.io/philcheng/pen/YWyYwG

【问题讨论】:

  • 如果您想更改 opactiy,只需在悬停选择器中添加不透明度,例如 .cardnew:hover {opacity: 0.5;},并删除 transition 声明,因为您已经在 .cardnew 选择器中进行了此操作
  • @iulo 你好,你好吗?我可以放置的不透明度,但是如何仅隐藏和显示悬停卡上的按钮? 4 个按钮一个在另一个上方,以卡片为中心?
  • 您可以使用 JS,addEventListener("mouseover", ...),而不是当鼠标指针将在卡片内时,您可以更改按钮的显示。 w3schools.com/jsref/event_onmouseover.asp
  • 不清楚你想做什么。我敢肯定,您的部分问题是您在选择器中使用了child combinator (&gt;):.cardnew &gt; a。如果链接是 .cardnew 的直接子级,则该选择器将匹配,但在您的 HTML 中,它们是 .button 元素的子级。在我看来,如果没有子组合器,您的所有代码都可以工作。事实上,我在this pen 所做的只是删除它们,看起来它可能更接近你想要的。
  • @Vince 这个,我想让按钮隐藏起来。并在卡片的悬停中以卡片的中心显示卡片内的按钮。我设法在卡片上设置了不透明度,但没有调整悬停时卡片中心的按钮。

标签: html css flexbox css-transitions


【解决方案1】:

未应用按钮样式的原因是您拥有的所有&gt;s。如果你删除它们,它似乎工作得更好。此外,您还有很多底部未关闭的divs。有什么原因吗?

对于消失,您可以使用opacity 属性使它们消失并在悬停时重新出现。

.cardnew {
  height: 256px;
  text-align: center;
  width: 246px;
  background: #ffffff;
  margin: 20px;
  display: inline-block;
  box-shadow: 1px 1px 22px rgba(157, 184, 209, 0.19);
  border-radius: 1px;
  cursor: pointer;
  transition: 0.15s linear;
}

.cardnew img {
  height: 150px;
  width: 100%;
  border-radius: 10px 10px 0 0;
  padding: 35px 0 5px 0;
}

.cardnew h1 {
  font-size: 22px;
  color: #5c5c5c;
  padding: 15px 0 5px 0;
  font-weight: 100;
}

.cardnew p {
  font-size: 13px;
  color: #cdcdcd;
  padding: 0 40px;
  line-height: 20px;
}

.cardnew a {
  padding: 13px;
  font-weight: normal;
  width: 260px;
  color: #2687f1;
  border: 2px solid #2687f1;
  border-radius: 7px;
  margin: auto;
  margin-top: 35px;
  display: block;
  opacity: 0;
  transition: 0.2s linear;
}

.cardnew:hover {
  transform: scale(1.015);
  transition: 0.15s linear;
  box-shadow: 1px 1px 22px rgba(157, 184, 209, 0.5);
}

.cardnew:hover a {
  padding: 13px;
  width: 260px;
  color: #fff;
  background: #2687f1;
  border-radius: 7px;
  margin: auto;
  margin-top: 35px;
  opacity: 1;
  transition: 0.2s linear;
}

.row-card {
  justify-content: space-around;
  flex-wrap: wrap;
  margin-right: -12.5px;
  margin-left: -12.5px;
}
<div class="row-card">
  <div class="cardnew">
    <img src="https://enginehosting-html.fruitfulcode.com/wp-content/themes/enginehosting-html/assets/images/server.svg" style="padding: 60px 0 5px 0;height: 125px;" />
    <h1>
      name
    </h1>
    <p>firstname</p>
    <div class="button"><a href="#"> BUTTON </a>
      <div class="button"><a href="#"> BUTTON </a>
        <div class="button"><a href="#"> BUTTON </a>
          <div class="button"><a href="#"> BUTTON </a>
          </div>
        </div>

编辑: 我弄乱了你的代码,我想我已经创建了类似你的图片的东西。

(您可能需要弄乱这些值以使其完美。)

.overlay {
  background: rgba(0, 0, 0, 0.5);
  height: 256px;
  width: 246px;
  position: absolute;
  opacity: 0;
  transition: 0.2s linear;
}

.cardnew a {
  position: absolute;
  padding: 13px;
  font-weight: normal;
  width: 160px;
  color: #2687f1;
  border: 2px solid #2687f1;
  border-radius: 7px;
  margin: auto;
  margin-top: 35px;
  display: block;
  opacity: 0;
  transition: 0.2s linear;
}

.cardnew:hover a {
  transform: scale(1);
  padding: 13px;
  color: #fff;
  background: #2687f1;
  border-radius: 7px;
  margin: auto;
  margin-top: 35px;
  opacity: 1;
  transition: 0.2s linear;
}

.btn1 {
  top: 10px;
  left: 42.5px;
}

.btn2 {
  top: 65px;
  left: 42.5px;
}

.btn3 {
  top: 120px;
  left: 42.5px;
}

.btn4 {
  top: 175px;
  left: 42.5px;
}

.cardnew {
  height: 256px;
  text-align: center;
  width: 246px;
  background: #ffffff;
  margin: 20px;
  display: inline-block;
  box-shadow: 1px 1px 22px rgba(157, 184, 209, 0.19);
  border-radius: 1px;
  cursor: pointer;
  transition: 0.15s linear;
}

.cardnew img {
  height: 150px;
  width: 100%;
  border-radius: 10px 10px 0 0;
  padding: 35px 0 5px 0;
}

.cardnew h1 {
  font-size: 22px;
  color: #5c5c5c;
  padding: 15px 0 5px 0;
  font-weight: 100;
}

.cardnew p {
  font-size: 13px;
  color: #cdcdcd;
  padding: 0 40px;
  line-height: 20px;
}

..cardnew:hover {
  transform: scale(1.015);
  transition: 0.15s linear;
  box-shadow: 1px 1px 22px rgba(157, 184, 209, 0.5);
}

.cardnew:hover .overlay {
  opacity: 1;
}

.row-card {
  justify-content: space-around;
  flex-wrap: wrap;
  margin-right: -12.5px;
  margin-left: -12.5px;
}
<div class="row-card">
  <div class="cardnew">
    <div class='overlay'>
    </div>
    <img src="https://enginehosting-html.fruitfulcode.com/wp-content/themes/enginehosting-html/assets/images/server.svg" style="padding: 60px 0 5px 0;height: 125px;" />
    <h1>
      name
    </h1>
    <p>firstname</p>
    <div class="button"><a href="#" class="btn1"> BUTTON </a></div>
    <div class="button"><a href="#" class="btn2"> BUTTON </a></div>
    <div class="button"><a href="#" class="btn3"> BUTTON </a></div>
    <div class="button"><a href="#" class="btn4"> BUTTON </a></div>
  </div>
</div>

这里是代码笔:click here

【讨论】:

  • 你好,我试过这个,我的风格全坏了,就像上面的代码一样。我正在尝试与笔 codepen.io/philcheng/pen/YWyYwG 完全一样,我想将按钮留在卡片内,以卡片悬停为中心。
  • 您想增加悬停时卡片的高度吗?还是只是移动卡片中已有的内容?
  • 我希望按钮在悬停时位于卡片内的中心,并添加不透明度,以便卡片的内容消失,并仅显示卡片内的按钮。我试过:.card: hover { background: #ffffff;不透明度:0.1; }
  • 我用我认为你想要的东西编辑了我的答案。
  • 哇,这正是您所需要的。我试图在按钮之间留一个间距,但我不明白,你能告诉我为什么吗?我想在每个按钮上至少留出 5px 的空间,我的 CSS 不接受。
猜你喜欢
  • 2014-07-05
  • 1970-01-01
  • 2012-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多