如何使用3张图片列进行悬停?

时间:2018-05-27 17:44:06

标签: html css multiple-columns mouseover

我是编码的初学者。鼠标悬停时我想在图像上显示文字(不使用JS)。但是,我已经为HTML和CSS制作了3个图像列,并且不知道如何在我的代码上添加悬停效果。我试图应用这么多不同的方法来制作但失败了。我认为我的代码搞砸了,肯定是错的。请让我知道如何解决这个问题..!

我想对我的3张图片产生这种效果(但在一列中) https://www.w3schools.com/howto/howto_css_image_overlay.asp

这是我的HTML代码:

<div class="row">
  <div class="column">
    <img src="https://i.imgur.com/LBFnJzd.gif" style="width:100%" class="hover">
  </div>

  <div class="column">
    <img src="https://i.imgur.com/eOrPIjL.png" style="width:100%" class="hover">
  </div>

  <div class="column">
    <img src="https://i.imgur.com/HWi1rPW.png" style="width:100%">
  </div>
</div>

这是我的CSS代码:

header {
  text-align: center;
  animation: myanimation 15s infinite;
}

@keyframes myanimation {
  0% {
    background-color: #20B2AA;
  }
  25% {
    background-color: #FF8C00;
  }
  50% {
    background-color: #6495ED;
  }
  75% {
    background-color: #F08080;
  }
  100% {
    background-color: #20B2AA;
  }
}

a {
  color: white;
}

a:hover,
a:visited,
a:link,
a:active {
  text-decoration: none;
}

h1 {
  margin-top: 10;
  color: #231900;
  font-family: 'Sunflower', sans-serif;
  font-size: 80px;
  font-weight: 800;
  letter-spacing: -2px;
  line-height: 84px;
  margin-bottom: 70px;
  text-transform: uppercase;
}

h2 {
  color: #cccccc;
  font-family: 'Sunflower', sans-serif;
  font-size: 25px;
  font-weight: 300;
  line-height: 32px;
  margin: 0px 0px 50px;
  text-align: center;
}

ul {
  list-style-type: none;
  margin: 0;
  padding: 5px;
  overflow: hidden;
  background-color: rgba(0, 0, 0, 0.2);
  position: -webkit-sticky;
  /* Safari */
  position: sticky;
  top: 0;
}

li {
  float: center;
  display: inline;
  padding: 0px 30px 0px 10px;
  color: #333333;
  font-family: 'Sunflower', sans-serif;
  font-size: 16px;
  font-weight: 300;
  line-height: 20px;
  text-align: center;
  position: -webkit-sticky;
  /* Safari */
  position: sticky;
  top: 0;
}


.responsive {
  width: 100%;
  max-width: 400px;
  height: auto;
}

* {
  box-sizing: border-box;
}

.row {
  display: -ms-flexbox;
  /* IE10 */
  display: flex;
  -ms-flex-wrap: wrap;
  /* IE10 */
  flex-wrap: wrap;
  padding: 0 30px;
}

/* Create four equal columns that sits next to each other */

.column {
  -ms-flex: 25%;
  /* IE10 */
  flex: 25%;
  max-width: 33%;
  padding: 0 4px;
}

.column img {
  margin-top: 8px;
  vertical-align: middle;
}

p {
  color: #231900;
  font-family: 'Crimson Text';
  font-size: 34px;
  font-weight: 500;
  line-height: 48px;
  margin-bottom: 48px;
  max-width: 650px;
}

/* Responsive layout - makes a two column-layout instead of four columns */

@media screen and (max-width: 800px) {
  .column {
    -ms-flex: 50%;
    flex: 50%;
    max-width: 50%;
  }
}

/* Responsive layout - makes the two columns stack on top of each other instead of next to each other */

@media only screen and (max-width: 700px) {
  .responsive {
    width: 49.99999%;
    margin: 6px 0;
  }
}

@media screen and (max-width: 600px) {
  .column {
    -ms-flex: 100%;
    flex: 100%;
    max-width: 100%;
  }
}

@media only screen and (max-width: 500px) {

  h1 {
    font-size: 30px;
    padding: 10px;
    margin-bottom: 0px;
  }
  h2 {
    font-size: 15px;
  }
  li {
    padding: 3px;
  }

  .responsive {
    width: 100%;
  }
}

2 个答案:

答案 0 :(得分:0)

HTML

<div class="row">
  <div class="column">
    <img src="https://i.imgur.com/LBFnJzd.gif" style="width:100%" class="image">
    <div class="overlay">
    <div class="text">Hello World</div>
  </div>
  </div>

  <div class="column">
    <img src="https://i.imgur.com/eOrPIjL.png" style="width:100%" class="image">
    <div class="overlay">
    <div class="text">Hello World</div>
  </div>
  </div>

  <div class="column">
    <img src="https://i.imgur.com/HWi1rPW.png" class="image" style="width:100%">
    <div class="overlay">
    <div class="text">Hello World</div>
  </div>
  </div>
</div>

额外的CSS

.column {
  position: relative;
}

.image {
  display: block;
  width: 100%;
  height: auto;
}
.overlay {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  height: 100%;
  width: 100%;
  opacity: 0;
  transition: .5s ease;
  background-color: #008CBA;
}

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

.text {
  color: white;
  font-size: 20px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
  text-align: center;
}

检查小提琴here

第二效果

HTML

<div class="row">
  <div class="column">
    <img src="https://i.imgur.com/LBFnJzd.gif" style="width:100%" class="image">
    <div class="middle">
    <div class="text">John Doe</div>
  </div>
  </div>

  <div class="column">
    <img src="https://i.imgur.com/eOrPIjL.png" style="width:100%" class="image">
    <div class="middle">
      <div class="text">John Doe</div>
    </div>
  </div>

  <div class="column">
    <img src="https://i.imgur.com/HWi1rPW.png" class="image" style="width:100%">
    <div class="middle">
      <div class="text">John Doe</div>
    </div>
  </div>
</div>

额外的CSS

.column {
    position: relative;
}

.image {
  opacity: 1;
  display: block;
  width: 100%;
  height: auto;
  transition: .5s ease;
  backface-visibility: hidden;
}

.middle {
  transition: .5s ease;
  opacity: 0;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
  text-align: center;
}

.column:hover .image {
  opacity: 0.3;
}

.column:hover .middle {
  opacity: 1;
}

.text {
  background-color: #4CAF50;
  color: white;
  font-size: 16px;
  padding: 16px 32px;
}

JS小提琴here

答案 1 :(得分:0)

.column {
width: 70%;
height: 100%;
position:relative;
overflow: hidden;
}
.column > img {
width: 100%;
float: left;
height: 300px;
}

.onHover {
width: 100%;
height: 300px;
background: rgba(0,0,0,0);
color: rgba(255,255,255,0);
position: absolute;
top:0; left:0;
transition: all .5s ease;
}

.onHover:hover {
background: rgba(0,0,0,.6);
color: rgba(255,255,255,1);
transition: all .5s ease;
}
<div class="column">
    <img src="https://i.imgur.com/HWi1rPW.png" style="width:100%">
    <div class="onHover">Some text</div>
</div>

你可以这样做。