Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.5k views
in Technique[技术] by (71.8m points)

html - Picture slider on Mobile

I am working on a Shopify store, and I want to display multiple product images depending on what options are selected. This is the code I am using, which works perfectly on desktop, but messed up the way pictures are displayed on slider in mobile:

{% assign wanted_alts = product.selected_or_first_available_variant.options %}
            
            <ul class="product-single__thumbnails product-single__thumbnails-{{ section.id }}" data-slider-container>
              {% if enable_thumbnail_slides == true %}
                <div class="product-single__thumbnails-slider-track" data-slider-track>
              {% endif %}

              {% for media in product.media %}
                {% if wanted_alts contains media.alt or media.alt == "All" %}
                  <li class="product-single__thumbnails-item product-single__thumbnails-item--{{ section.settings.media_size }} {% if enable_thumbnail_slides == true %} product-single__thumbnails-item-slide{% endif %} js"{% if enable_thumbnail_slides == true %} data-slider-slide-index="{{ forloop.index0 }}" data-slider-item{% endif %}>
                    <a href="{{ media.preview_image | img_url: product_image_zoom_size, scale: product_image_scale }}"
                      class="text-link product-single__thumbnail product-single__thumbnail--{{ section.id }}"
                      data-thumbnail-id="{{ section.id }}-{{ media.id }}"
                      {% if enable_thumbnail_slides == true %} data-slider-item-link{% endif %}
                      {% if enable_image_zoom %}data-zoom="{{ media.preview_image | img_url: product_image_zoom_size, scale: product_image_scale }}"{% endif %}>

                        {%- capture thumbnailAlt -%}
                          {%- if media.media_type == 'video' or media.media_type == 'external_video' -%}
                            {{ 'sections.featured_product.video_thumbnail_alt' | t: imageAlt: media.alt | escape }}
                          {%- elsif media.media_type == 'model' -%}
                            {{ 'sections.featured_product.model_thumbnail_alt' | t: imageAlt: media.alt | escape }}
                          {%- else -%}
                            {{ 'sections.featured_product.gallery_thumbnail_alt' | t: imageAlt: media.alt | escape }}
                          {%- endif -%}
                        {%- endcapture -%}

                        <img class="product-single__thumbnail-image" src="{{ media.preview_image | img_url: '110x110', scale: 2 }}" alt="{{ thumbnailAlt }}">
                        {%- if media.media_type == 'video' or media.media_type =='external_video' -%}
                          <div class="product-single__thumbnail-badge">
                            {% include 'icon-video-badge-full-color' %}
                          </div>
                        {%- endif -%}
                        {%- if media.media_type == 'model' -%}
                          <div class="product-single__thumbnail-badge">
                            {% include 'icon-3d-badge-full-color' %}
                          </div>
                        {%- endif -%}
                    </a>
                  </li>
                {% endif %}
              {% endfor %}

              {% if enable_thumbnail_slides == true %}
                </div>
              {% endif %}
            </ul>

The only things I changed are: Included {% assign wanted_alts = product.selected_or_first_available_variant.options %} before the images list to find out what options I want to display.

Wrap each <li> item in a if statement that tests if I want to show that image or not (based on the alt_text):

{% for media in product.media %}
    {% if wanted_alts contains media.alt or media.alt == "All" %} //Line I added
        <li>
        ...
        </li>
    {% endif %}//Line I added
{% endfor %}

This is my website.

Now my thumbnails are messed up on mobile, but they look exactly like I wanted on desktop.

Can someone help me to figure this out?

This is how it looks on mobile now:

Messed up

This is how it looked before I changed the code (and the way I want it to be):

Good pictures


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...