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

时间:2018-06-22 23:12:51

标签: html css shopify liquid liquid-layout

我试图添加一项功能,以将光标悬停在集合视图中的产品图片上方时显示替代产品图片。

每当我使用以下代码查看页面时,产品图像都已移至容器底部,并且还覆盖了其下方的标题/价格。

我该如何纠正以使图片保持原始对齐并仅使悬停效果有效?

谢谢!

这是我目前在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; 
  }
}

2 个答案:

答案 0 :(得分:0)

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语法,那么我强烈推荐Wes Bos提供一个很棒的free video course

P.S。请注意,只有在图像具有设置的像素宽度的情况下,网格才可以“即开即用”地工作,尽管您可以通过一点媒体查询来轻松支持不同尺寸的图像。

答案 1 :(得分:0)

使其可以与以下

一起使用

在CSS中,我使用了@media(hover:hover){},这样用户不必双击移动设备上的图像即可转到产品页面。

CSS:

/* ===============================================
// Reveal module
// =============================================== */
@media (hover:hover) {
.has-secondary.grid-view-item__link img.secondary{
 display:none;
}
}

@media (hover:hover) {
.has-secondary.grid-view-item__link:hover img.secondary{
 display:block;
}
}

@media (hover:hover) {
.has-secondary.grid-view-item__link:hover img.grid-view-item__image{
 display:none;
}
}

@media screen and (min-width:767px){
.has-secondary.grid-view-item__link img.secondary{
 display:none;
}

.has-secondary.grid-view-item__link:hover img.secondary{
 display:block;
}

.has-secondary.grid-view-item__link:hover img.grid-view-item__image{
 display:none;
}
}

@media screen and (max-width:767px){
.has-secondary.grid-view-item__link img.secondary{
 display:none;
}
}

将product-card-grid.liquid HTML / Liquid更改为以下内容:

{% unless grid_image_width %}
  {%- assign grid_image_width = '600x600' -%}
{% endunless %}
<div class="grid-view-item{% unless product.available %} product-price--sold-out grid-view-item--sold-out{% endunless %}">
  <a class="grid-view-item__link {% if product.images.size > 1 %} has-secondary{% endif %}" href="{{ product.url | within: collection }}">
    <img class="grid-view-item__image" src="{{ product.featured_image.src | img_url: grid_image_width }}" alt="{{ product.featured_image.alt }}">
    {% if product.images.size > 1 %}
     <img class="secondary" src="{{ product.images.last | img_url: grid_image_width }}" alt="{{ product.images.last.alt | escape }}">
    {% endif %}
  </a>
</div>

<div class="h4 grid-view-item__title">
     <a href="{{ product.url | within: collection }}"> {{ product.title }} </a>
</div>   
{% if section.settings.show_vendor %}
      <div class="grid-view-item__vendor">{{ product.vendor }}</div>
    {% endif %}
 <div class="grid-view-item__meta">
   <a href="{{ product.url | within: collection }}"> {% include 'product-price', variant: product %} </a>
    </div>