【发布时间】:2020-04-20 05:19:43
【问题描述】:
在产品系列页面和特色产品部分的产品卡上,我有每个产品的产品卡和一张图片,在图片下方,我有产品所具有的变体的颜色样本。
当图片悬停在上面时,它会显示第二张产品图片:{{ product.images[1] | img_url: '300x400', scale: 3, crop: 'center' }} 指的是产品图片数组中的第二张图片。
但是,当将鼠标悬停在色样上时,我希望特色图像在鼠标悬停时更改为变体图像。
产品卡片图片的代码:
{% for product in collection.products %}
<!-- this div contains both product-card snippet and color variant snippet -->
<div class="product-card w-50 mb3" data-product-id="{{ product.id }}">
<!-- product card snippet code -->
<a class="link-to-product" href="{{ product.url | within: collection }}">
{% if product.featured_image %}
<div class="card-image reveal mb3">
<img src="{{ product | img_url: '300x400', scale: 3, crop: 'center' }}" alt="{{ product.title }}">
<img class="hidden" src="{{ product.images[1] | img_url: '300x400', scale: 3, crop: 'center' }}" />
</div>
{% endif %}
<div class="card-info flex flex-column items-center tc">
<h5 class="mb2 f6 f5-ns">{{ product.title }}</h5>
<p class="price mb3">
{% if product.compare_at_price %}
<span class="strike">{{ product.compare_at_price | money_with_currency }}</span>
{% endif %}
{{ product.price | money_with_currency }}
</p>
</div>
</a>
<!-- end of color product card snippet -->
<!-- color variant hover snippet code -->
<div class="option-colors">
<div class="product-option-row flex justify-center flex-column flex-row-ns tc tl-ns mb4">
<div class="option-values">
{% for value in option.values %}
{% assign radio_id = 'option-' | append: option_name | append: '-' | append: value | handleize %}
{%- for variant in product.variants -%}
{%- if variant.title contains value -%}
{%- assign productColorOptionURL = product.url | append: "?variant=" | append: variant.id -%}
{%- assign variantImgSrc = variant.image.src | img_url: '300x400', scale: 3, crop: 'center' -%}
{%- break -%}
{%- endif -%}
{%- endfor -%}
<a href="{{ productColorOptionURL }}" data-variant-img="{{ variantImageSrc }}">
<label for="{{ radio_id }}" class="color-swatch-list">
{% if force_colors == true %}
{% include 'option-color' with color: value %}
{% else %}
{{ value }}
{% endif %}
</label>
</a>
{% endfor %}
</div>
</div>
</div>
<!-- end of color variant hover snippet -->
</div>
<!-- main div ends -->
jQuery 代码
let
productCardContainer = '[data-product-id]',
productVariantSwatch = '[data-variant-img]',
productMainImage = '.card-image img';
$(productCardContainer)
.find(productVariantSwatch).mouseover(function() {
$(this).(productMainImage).attr('src', $(this).data("variant-img"));
})
【问题讨论】:
标签: javascript jquery shopify liquid