鼠标悬停商店滚动效果

时间:2018-06-21 19:40:09

标签: html scroll containers mouseover

我想在图像上添加鼠标悬停的“立即购买”效果,我使用了以下代码:

<!DOCTYPE html>
<html>
<style>
.container {
    style= "width:300px;height:300px;"
    left: 0;
    Right: 0;
}

.image {
  opacity: 1;
  display: block;
  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;
}

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

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

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

<div class="container">
  <img src="img_avatar.png" alt="Avatar" class="image" >
  <div class="middle">
    <div class="text">Shop Now</div>
  </div>
</div>
  
</html>

但是当我在我的网站上运行它时,滚动效果同时适用于所有3张图像。如下所示: image

我该怎么做才能解决此问题?以前有人告诉我,如果我将容器的大小更改为仅适合图像的大小,它应该可以工作,但是我该怎么做?

2 个答案:

答案 0 :(得分:0)

<!DOCTYPE html>
<html>
<style>
.container {
    width:300px; /*edited here*/
    height:300px;
    /*this syntax is for html tags ONLY: style= "width:300px;height:300px;"*/

    left: 0;
    Right: 0;
}

.image {
  opacity: 1;
  display: block;
  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;
}

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

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

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

<div class="container">
  <img src="img_avatar.png" alt="Avatar" class="image" >
  <div class="middle">
    <div class="text">Shop Now</div>
  </div>
</div>
  
</html>

您对CSS使用了错误的语法。 style =“ width:300px; height:300px;”如果它在您的html中,将是正确的:

<div class = "container" style= "width:300px;height:300px;"></div>

但是在CSS中,样式已经在标签中隐含了,因此在CSS中,您需要做的只是:

.container{
    width:300px;
    height:300px;
    /*and so on*/
}

注意:为避免将来出现问题,请了解chrome的检查工具。它将帮助您更好地了解页面布局,元素大小以及其他内容。 https://developers.google.com/web/tools/chrome-devtools/inspect-styles/

答案 1 :(得分:0)

一些简短的笔记:

U无法在CSS中使用 style =“ width:300px; height:300px;” 。在您的示例中,第一行应为:

.container {
    width:300px;
    height:300px;
    left:0;
    Right:0;
}

您只能在html中使用样式属性,但这不是nessesairy。如果这样做,它将绕过CSS:

<div class="container" style="width:300px;height:300px;">

此外,您实际上不必同时调用宽度和高度,因为当图像具有宽度和高度之一时,它将自动缩放。

话虽如此,我相信这段代码可以解决您的问题:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {box-sizing: border-box;}

.container {
  position: relative;
  width: 50%;
  width: 200px;
}

.image {
  display: block;
  width: 100%;
  height: auto;
}

.overlay {
  position: absolute; 
  bottom: 0; 
  background: rgb(0, 0, 0);
  background: green; /* Black see-through */
  color: #f1f1f1; 
  width: 100%;
  transition: .5s ease;
  opacity:0;
  color: white;
  font-size: 20px;
  padding: 20px;
  text-align: center;
}

.container:hover .overlay {
  opacity: 1;
}
</style>
</head>
<body>

<h2>Image Overlay Title</h2>
<p>Hover over the image to see the effect.</p>

<div class="container">
  <img src="https://www.w3schools.com/howto/img_avatar.png" alt="Avatar" class="image">
  <div class="overlay">Shop now</div>
</div>

<div class="container">
  <img src="https://www.w3schools.com/howto/img_avatar2.png" alt="Avatar" class="image">
  <div class="overlay">Shop now</div>
</div>

</body>
</html>