【问题标题】:Mouseover shop scroll effect鼠标悬停商店滚动效果
【发布时间】:2018-06-21 19:40:08
【问题描述】:
我想在我的图片上添加鼠标悬停“立即购买”效果,我使用了以下代码:
<!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 张图像。如下所示:
我可以做些什么来解决这个问题?之前有人告诉我,如果我将容器大小更改为刚好适合图像,它应该可以工作,但我该怎么做呢?
【问题讨论】:
标签:
html
scroll
containers
mouseover
【解决方案1】:
<!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 使用了错误的语法。样式=“宽度:300像素;高度:300像素;”如果它在你的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/
【解决方案2】:
一些简短的笔记:
U 不能在 css 中使用 style="width:300px;height:300px;"。在您的示例中,您的第一行应该是:
.container {
width:300px;
height:300px;
left:0;
Right:0;
}
您只能在 html 中使用样式属性,但这不是必需的。如果你这样做,它将绕过你的 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>