gpt4 book ai didi

html - 在产品图片上添加悬停功能

转载 作者:行者123 更新时间:2023-11-28 01:30:41 25 4
gpt4 key购买 nike

我正在尝试添加一项功能,当光标悬停在产品系列 View 中的产品图片上时显示替代产品图片。

每当我使用下面的代码查看页面时,产品图像就会移动到容器的底部,并且还会覆盖它们下方的标题/价格。

我要如何修正才能使图片保持原来的对齐方式并且只有悬停效果起作用?

谢谢!

这是我目前在 product-card-grid.liquid 中的内容 --

<div id="{{ wrapper_id }}" class="grid-view-item__image-wrapper js">
<div style="padding-top:{% unless product.featured_image == blank %}{{ 1 | divided_by: product.featured_image.aspect_ratio | times: 100}}%{% else %}100%{% endunless %};">
<div class="reveal">
<img id="{{ img_id }}"
class="grid-view-item__image lazyload"
src="{{ product.featured_image | img_url: '300x300' }}"
data-src="{{ img_url }}"
data-widths="[180, 360, 540, 720, 900, 1080, 1296, 1512, 1728, 2048]"
data-aspectratio="{{ product.featured_image.aspect_ratio }}"
data-sizes="auto"
alt="">
<img id="{{img_id}}"
class="hidden"
src="{{ product.images.last }}"
data-src="{{ img_url }} "
data-widths="[180, 360, 540, 720, 900, 1080, 1296, 1512, 1728, 2048]"
data-aspectratio="{{ product.featured_image.aspect_ratio }}"
data-sizes="auto"
alt="{{ product.images.last.alt | escape }}">
</div>
</div>
</div>

“显示”的 css 如下:

  /* ===============================================
// Reveal module
// =============================================== */

.reveal .hidden { display: block !important; visibility: visible !important;}
.product:hover .reveal img { opacity: 1; }
.reveal { position: relative; }
.reveal .hidden {
position: absolute;
z-index: -1;
top: 0;
width: 100%;
height: 100%;
opacity: 0;
-webkit-transition: opacity 0.3s ease-in-out;
-moz-transition: opacity 0.3s ease-in-out;
-o-transition: opacity 0.3s ease-in-out;
transition: opacity 0.3s ease-in-out;
}
.reveal:hover .hidden {
z-index: 100000;
opacity: 1;
}
.reveal .caption {
position: absolute;
top: 0;
display: table;
width: 100%;
height: 100%;
background-color: white; /* fallback for IE8 */
background-color: rgba(255, 255, 255, 0.7);
font: 13px/1.6 sans-serif;
text-transform: uppercase;
color: #333;
letter-spacing: 1px;
text-align: center;
text-rendering: optimizeLegibility;
}
.reveal .hidden .caption .centered {
display: table-cell;
vertical-align: middle;
}

@media (min-width: 480px) and (max-width: 979px) {
.reveal .caption {
font-size: 11px;
}
}

最佳答案

您的 CSS 中发生了很多事情,但如果我正确理解了情况,您希望获得类似于 this Codepen 中的效果,对吧?

在您的 CSS 中,绝对将两个图像放在一个 div 中(使用 top: 0pxleft: 0px)并更改堆栈中顶部图像的不透明度将鼠标悬停在父项上。

HTML:

<div class='reveal'>
<img src='image_src' class='bottom'>
<img src='image_src' class='top'>
</div>

CSS:

.top, .bottom {
position: absolute;
opacity: 1;
top: 0px;
left: 0px;
}

.frame:hover .top {
opacity: 0;
}

编辑:根据要求使用网格实现。

HTML:

<div class='grid'>
<div class='reveal'>
<img src='image_src' class='bottom'>
<img src='image_src' class='top'>
</div>
<div class='reveal'>
<img src='image_src' class='bottom'>
<img src='image_src' class='top'>
</div>
</div>

CSS:

.grid {
display: flex;
flex-wrap: wrap; /*makes the grid "responsive"*/
}

.reveal {
display: flex;
flex: 1 1 200px; /*last value must be picture width*/
height:200px; /* picture height*/
margin: 20px; /*aesthetic only*/
position: relative;
}

.top, .bottom {
position: absolute;
opacity: 1;
top: 0px;
left: 50%; /*centers pictures inside flex children (.reveal divs)*/
transform: translate(-50%, 0); /*see above*/
}

.reveal:hover .top {
opacity: 0;
}

Demo on Codepen

上面的示例使用 flexbox 创建一个简单的网格,该网格将与我原始帖子中的(稍微修改过的).reveal div 一起使用。如果你不熟悉 flexbox 语法,这里有一个很棒的 free video course来自 Wes Bos,我强烈推荐。

附言请注意,如果图像具有设置的像素宽度,网格将仅在“开箱即用”的情况下工作,尽管您可以通过一点媒体查询魔法轻松支持不同的图像尺寸。

关于html - 在产品图片上添加悬停功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50996566/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com