当用户点击变体颜色或选项时,自动切换显示该变体的图片。而当用户将鼠标悬停在某个变体上时,展示不同的图片。通过这种方式,用户可以更直观地查看不同颜色或款式的产品。
实现原理:
-
变体图片预加载:所有变体图片隐藏加载,通过
data-variant-id关联变体 -
颜色去重逻辑:使用
option值提取颜色属性,通过color_mapping配置自定义色值 -
悬停交互机制:通过计算当前图片索引获取"下一张"图片URL存储为
data-hover-img
1.通过{% assign product = block.settings.product %}获取产品数据
2.通过遍历变体,获取当前变体图,并且通过{% assign next_index = current_index | plus: 1 %}获取当前变体的下一张图片路径并且在 图片中增加 data-hover-img="{{ next_image_url }}"属性路径
{% for variant in product.variants %}
{% assign variant_image = variant.featured_image %}
{% assign next_image_url = "" %}
{% for image in product.images %}
{% if image.src == variant_image.src %}
{% assign current_index = forloop.index0 %}
{% assign next_index = current_index | plus: 1 %}
{% if next_index == image_count %}
{% assign next_index = 0 %}
{% endif %}
{% assign next_image = product.images[next_index] %}
{% assign next_image_url = next_image | image_url: width: '1000x' %}
{% endif %}
{% endfor %}
<img class="main-product-image variant-image default-img"
src="{{ variant_image | image_url: width:'1000x' }}"
alt="{{ variant_image.alt | escape }}"
loading="lazy"
width="{{ variant_image.width }}"
height="{{ variant_image.height }}"
data-variant-id="{{ variant.id }}"
data-hover-img="{{ next_image_url }}"
style="display: none;">
{% endfor %}
2.使用 jQuery 为每个变体的颜色按钮绑定点击事件,当用户点击某个颜色时,页面上的产品图片会更新为该颜色的变体图片。点击颜色按钮 → 通过data-variant-id匹配对应主图
$slide.find('.product-variant-item').click(function() {
var variantId = $(this).data('variant-id');
var currentVariantImage = $slide.find('.variant-image.default-img[data-variant-id="' + variantId + '"]');
var nextImageSrc = currentVariantImage.data('hover-img');
$slide.find('.hover-img').attr('src', nextImageSrc);
$slide.find('.variant-image.default-img').hide();
currentVariantImage.show();
});
3.对于每个变体,我们需要为它找到对应的悬停图。data-hover-img 存储了当前变体的下一张图片
{% assign hover_next = product.images[next_index] | image_url: width: '1000x' %}
<img class="main-product-image variant-image hover-img"
src="{{ hover_next }}"
alt="{{ variant_image.alt }}"
loading="lazy"
width="1000"
height="1000">
4.产品的颜色选项是通过后台的自定义设置提供的,有些颜色名称不能识别,通过映射字符串,将颜色选项转化为实际的颜色值 Blue Gray:#4F697B;Light Gray:#B3B3B3; 这种格式。当有不同的尺寸或者不同属性,但是颜色值相同的就会生成不同的变体,造成重复渲染图片, 过滤重复的颜色。
{% unless displayed_colors contains mapped_color %}
<div class="product-variant-item"
style="background-color:{{ mapped_color }};"
data-variant-id="{{ variant.id }}" title="{{ variant_color }}" >{{ variant_color }}</div>
{% assign displayed_colors = displayed_colors | append: mapped_color | append: "," %}
{% endunless %}
实现的效果示例
完整的代码示例
<style>
.{{ section.id }} {
margin-top: {{ section.settings.margin_top }}px;
margin-bottom: {{ section.settings.margin_bottom }}px;
position: relative;
}
.product-variant{
display: flex;
gap: 20px;
}
.main-product-image{
width: 300px;
}
.main-product-images{
display: grid;
grid-template-columns: repeat(4,1fr);
}
.product-variant-image{
position: relative;
}
.hover-img{
opacity: 0;
transition: all 0.3s ease-in-out;
position: absolute;
top: 0;
left: 0;
display: block !important;
}
.product-variant-image:hover .hover-img{
opacity: 1;
}
.test-product-variant-inner-mySwiper .swiper-slide{
width: auto;
}
.product-variant-item{
display: flex;
align-items: center;
justify-content: center;
overflow: hidden;
margin:0 auto;
width: 40px;
height: 40px;
font-size: 12px;
text-align: center;
}
</style>
<section class="test-product-variant {{ section.id }}">
<div class="container">
<div class="test-product-variant-inner">
<div class="swiper test-product-variant-inner-mySwiper">
<div class="swiper-wrapper">
{% for block in section.blocks %}
{% assign product = block.settings.product %}
<div class="swiper-slide">
<div class="product-title">{{ product.title }}</div>
<div class="product-variant-image">
{% assign image_count = product.images.size %}
{% for variant in product.variants %}
{% assign variant_image = variant.featured_image %}
{% assign next_image_url = "" %}
{% for image in product.images %}
{% if image.src == variant_image.src %}
{% assign current_index = forloop.index0 %}
{% assign next_index = current_index | plus: 1 %}
{% if next_index == image_count %}
{% assign next_index = 0 %}
{% endif %}
{% assign next_image = product.images[next_index] %}
{% assign next_image_url = next_image | image_url: width: '1000x' %}
{% endif %}
{% endfor %}
<img class="main-product-image variant-image default-img"
src="{{ variant_image | image_url: width:'1000x' }}"
alt="{{ variant_image.alt | escape }}"
loading="lazy"
width="{{ variant_image.width }}"
height="{{ variant_image.height }}"
data-variant-id="{{ variant.id }}"
data-hover-img="{{ next_image_url }}"
style="display: none;">
{% endfor %}
<!-- 初始悬停图(默认显示第一变体对应的下一张) -->
{% assign first_variant = product.variants[0] %}
{% assign variant_image = first_variant.featured_image %}
{% assign hover_next = "" %}
{% for image in product.images %}
{% if image.src == variant_image.src %}
{% assign current_index = forloop.index0 %}
{% assign next_index = current_index | plus: 1 %}
{% if next_index == product.images.size %}
{% assign next_index = 0 %}
{% endif %}
{% assign hover_next = product.images[next_index] | image_url: width: '1000x' %}
{% endif %}
{% endfor %}
<img class="main-product-image variant-image hover-img"
src="{{ hover_next }}"
alt="{{ variant_image.alt }}"
loading="lazy"
width="1000"
height="1000">
</div>
<div class="product-variant">
{% assign displayed_colors = "" %}
<!-- 将后台设置的 color_mapping 进行分割成数组 -->
{% assign color_mapping_string = section.settings.color_mapping %}
{% assign color_mapping = color_mapping_string | split: ";" %} <!-- 分割为每个映射项 -->
{% for variant in product.variants %}
{% assign variant_color = variant.option1 %}
{% assign mapped_color = variant_color %} <!-- 默认使用原始颜色 -->
<!-- 遍历 color_mapping,找到匹配的颜色 -->
{% for mapping in color_mapping %}
{% assign parts = mapping | split: ":" %}
{% if parts[0] == variant_color %}
{% assign mapped_color = parts[1] | strip %}
{% endif %}
{% endfor %}
<!-- 只有未显示过的颜色才会显示 -->
{% unless displayed_colors contains mapped_color %}
<div class="product-variant-item"
style="background-color:{{ mapped_color }};"
data-variant-id="{{ variant.id }}" title="{{ variant_color }}" >{{ variant_color }}</div>
<!-- 将显示的颜色添加到 displayed_colors 中 -->
{% assign displayed_colors = displayed_colors | append: mapped_color | append: "," %}
{% endunless %}
{% endfor %}
</div>
</div>
{% endfor %}
</div>
</div>
</div>
</div>
</section>
<script>
$(document).ready(function() {
// 初始化Swiper
var swiper = new Swiper(".test-product-variant-inner-mySwiper", {
slidesPerView: 'auto',
spaceBetween: 30,
});
// 为每个轮播项设置独立的变体切换逻辑
$('.swiper-slide').each(function() {
var $slide = $(this);
// 默认显示第一个变体图
$slide.find('.variant-image.default-img').first().show();
// 为当前轮播项内的变体项绑定点击事件
$slide.find('.product-variant-item').click(function() {
var variantId = $(this).data('variant-id');
// 只在当前轮播项内查找元素
var currentVariantImage = $slide.find('.variant-image.default-img[data-variant-id="' + variantId + '"]');
var currentImageSrc = currentVariantImage.attr('src');
var nextImageSrc = currentVariantImage.data('hover-img');
console.log('当前变体图:', currentImageSrc);
console.log('下一张图(hover):', nextImageSrc);
// 更新当前轮播项的悬停图
$slide.find('.hover-img').attr('src', nextImageSrc);
// 切换显示当前轮播项的变体图
$slide.find('.variant-image.default-img').hide();
currentVariantImage.show();
});
});
});
</script>
{% schema %}
{
"name": "test product variant",
"presets": [
{
"name": "test product variant"
}
],
"settings": [
{
"type": "number",
"id": "margin_top",
"label": "margin top",
"default": 0
},
{
"type": "number",
"id": "margin_bottom",
"label": "margin bottom",
"default": 0
},
{
"type": "number",
"id": "mobile_margin_top",
"label": "mobile margin top",
"default": 0
},
{
"type": "number",
"id": "mobile_margin_bottom",
"label": "mobile margin bottom",
"default": 0
},
{
"type":"textarea",
"id": "color_mapping",
"label": "color mapping"
},
],
"blocks": [
{
"type": "swiper",
"name": "swiper",
"limit": 4 ,
"settings": [
{
"type":"text",
"id": "Featured_Collections",
"label": "Featured Collections",
"default": "Featured Collections"
},
{
"type":"textarea",
"id": "title",
"label": "title"
},
{
"type":"textarea",
"id": "text",
"label": "text"
},
{
"type": "product",
"id": "product",
"label": "Product"
},
{
"type":"text",
"id": "btn_text",
"label": "btn_text"
},
{
"type": "image_picker",
"id": "img_1",
"label": "Image 1"
},
{
"type": "image_picker",
"id": "mobile_img_1",
"label": "mobile Image 1"
},
{
"type": "image_picker",
"id": "img",
"label": "Image"
},
{
"type": "image_picker",
"id": "mobile_img",
"label": "mobile Image"
}
]
}
]
}
{% endschema %}
7327




被折叠的 条评论
为什么被折叠?



