【问题标题】:How can I change featured image when I click on swatches?单击色板时如何更改特色图像?
【发布时间】:2020-12-16 08:37:43
【问题描述】:

任何人都可以帮助我为什么当我在收藏页面上选择变体时我的精选照片不会改变??

https://strokes-test.myshopify.com/collections/frontpage

Javascript

initColorSwatchGrid: function() { 
jQuery('.item-swatch li label').click(function(){ 
var newImage = jQuery(this).parent().find('.hidden img').attr('src');
jQuery(this).parents('.item-row').find('.featured-image').attr({ src: newImage }); 
return false;
});
}

HTML

<ul class="item-swatch color_swatch_Value">  
{% for option in product.options %}
  {% if option == 'Shades' %}
    {% assign index = forloop.index0 %}
    {% assign colorlist = '' %}
    {% assign color = '' %}
    {% for variant in product.variants %}
      {% capture color %}
        {{ variant.options[index] }}
      {% endcapture %}
      {% unless colorlist contains color %}  
      {% assign text = color | handleize %}
      <li>
       <label style="{% if text == 'white' %}border: 1px solid #cbcbcb; {% endif %}background-color: {{ color | split: ' ' | last | handle }}; background-image: url({{ text | append: '.png' | file_url }});"></label>
        {% if variant.image != null %}
        <div class="hidden">
          <img src="{{ variant.image.src | product_img_url: 'grande' }}" alt="{{ text }}"/>
        </div>
        {% endif %}
      </li>
        {% capture tempList %}
          {{ colorlist | append: color | append: ' ' }}
        {% endcapture %}
        {% assign colorlist = tempList %}
      {% endunless %}
    {% endfor %}
  {% endif %}
{% endfor %}
</ul>

注意我也在使用 jquery v1.8.1

【问题讨论】:

    标签: javascript html jquery shopify liquid


    【解决方案1】:

    需要更新JS代码才能正常工作,使用它

    initColorSwatchGrid: function() {
       jQuery('.item-swatch li label').click(function () {
          var newImage = jQuery(this).next().find('img').attr('src');
          jQuery(this).parents('.product-detail').prev().find('.featured-image').attr({ src: newImage });
          return false;
       });
    }
    

    【讨论】:

    • 谢谢@Onkar,我还打算添加一个“添加到购物车按钮”,有什么建议可以应用到我的样本上吗?
    • 需要更新 JS 代码并设置 HTML prnt.sc/w4j127
    • 使用jQuery(this).parents('ul.item-swatch').prev().val(jQuery(this).attr('value')); 将点击的样本项的值分配给在 swtach 点击函数中选择列表。也隐藏选择,因为样本和选择是相同的目的。
    猜你喜欢
    • 1970-01-01
    • 2021-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-14
    • 1970-01-01
    • 2016-03-09
    相关资源
    最近更新 更多