Shopify网站上产品滑块中的图像和视频

时间:2017-10-10 04:02:18

标签: javascript jquery shopify liquid

我需要一些帮助,在Shopify中创建一个产品滑块,允许您添加图像和视频。目前滑块工作正常,但我在向滑块添加产品视频时遇到问题。

根据我的阅读,您可以使用alt标记添加YouTube嵌入代码,该代码将覆盖图像并显示视频。我可以为第一张图片工作,但它有一个租赁来打破滑块的其余部分。我相信我可能需要更改默认滑块并使用另一种选择。

这是我到目前为止所拥有的

  $('#FeaturedImageZoom-product-template .product-featured-img').each (function() {

    var videoid = $(this).attr("alt");
    console.log(videoid);

    if ($('.product-featured-img').attr("alt") == videoid) {         
      $(this).replaceWith('<iframe width="500px" height="500px" src="'+videoid+'?autoplay=1&amp;showinfo=0&amp;controls=0&amp;modestbranding=1&amp;rel=0" frameborder="0" allowfullscreen></iframe>');
    } 

  });

以下是与之相关的流动代码:

<div class="grid product-single">
<div class="grid__item product-single__photos {{ product_image_width }}{% if section.settings.image_size == 'full' %} product-single__photos--full{% endif %}">
  {%- assign featured_img_src = current_variant.featured_image.src | default: product.featured_image.src -%}
  {%- assign featured_img_alt = current_variant.featured_image.alt | default: product.featured_image.alt -%}
  <div id="FeaturedImageZoom-{{ section.id }}" class="product-single__photo{% if product.images.size > 1 %} product-single__photo--has-thumbnails{% endif %}" {% if enable_zoom %}data-zoom="{{ featured_img_src | img_url: product_image_zoom_size, scale: product_image_scale }}"{% endif %}>
    <img src="{{ featured_img_src | img_url: product_image_size, scale: product_image_scale }}" alt="{{ featured_img_alt }}" id="FeaturedImage-{{ section.id }}" class="product-featured-img{% if enable_zoom %} js-zoom-enabled{% endif %}">
  </div>

  {% if product.images.size > 1 %}
    {% if product.images.size > 3 %}
      {%- assign enable_thumbnail_slides = true -%}
    {% endif %}

    <div class="thumbnails-wrapper{% if enable_thumbnail_slides == true %} thumbnails-slider--active{% endif %}">
      {% if enable_thumbnail_slides == true %}
        <button type="button" class="btn btn--link medium-up--hide thumbnails-slider__btn thumbnails-slider__prev thumbnails-slider__prev--{{ section.id }}">
          {% include 'icon-chevron-left' %}
          <span class="icon__fallback-text">{{ 'sections.slideshow.previous_slide' | t }}</span>
        </button>
      {% endif %}
      <ul class="grid grid--uniform product-single__thumbnails product-single__thumbnails-{{ section.id }}">
        {% for image in product.images %}
          <li class="grid__item {{ product_thumbnail_width }} product-single__thumbnails-item">
            <a
              href="{{ image.src | img_url: product_image_size, scale: product_image_scale }}"
                class="text-link product-single__thumbnail product-single__thumbnail--{{ section.id }}"
                {% if enable_zoom %}data-zoom="{{ image.src | img_url: product_image_zoom_size, scale: product_image_scale }}"{% endif %}>
                <img class="product-single__thumbnail-image" src="{{ image.src | img_url: product_thumb_size, scale: product_image_scale }}" alt="{{ image.alt | escape }}">
            </a>
          </li>
        {% endfor %}
      </ul>
      {% if enable_thumbnail_slides == true %}
        <button type="button" class="btn btn--link medium-up--hide thumbnails-slider__btn thumbnails-slider__next thumbnails-slider__next--{{ section.id }}">
          {% include 'icon-chevron-right' %}
          <span class="icon__fallback-text">{{ 'sections.slideshow.next_slide' | t }}</span>
        </button>
      {% endif %}
    </div>
  {% endif %}
</div>

我已经阅读了一些帖子,其中列出了与尝试添加产品视频的开发人员类似的问题,但我还没有找到解决方案。

如果有人可以在这里提供帮助,那将不胜感激。

由于

2 个答案:

答案 0 :(得分:2)

为什么在使用简单的液体时可以使用javascript?

如果alt属性包含youtube或其他内容,您只需添加{% if image.alt contains 'youtube' %} <iframe width="500px" height="500px" src="{{image.alt}}?autoplay=1&amp;showinfo=0&amp;controls=0&amp;modestbranding=1&amp;rel=0" frameborder="0" allowfullscreen></iframe> {% else %} <a href="{{ image.src | img_url: product_image_size, scale: product_image_scale }}" class="text-link product-single__thumbnail product-single__thumbnail--{{ section.id }}" {% if enable_zoom %}data-zoom="{{ image.src | img_url: product_image_zoom_size, scale: product_image_scale }}"{% endif %}> <img class="product-single__thumbnail-image" src="{{ image.src | img_url: product_thumb_size, scale: product_image_scale }}" alt="{{ image.alt | escape }}"> </a> {% endif %} 属性的检查。

例如:

<asp:button>

答案 1 :(得分:0)

我只是想根据上面的答案发布我到达的解决方案&#34; drip&#34;。我希望这可能有助于其他人走上正轨。

  <!-- Product Slider w/ Video -->
  <div id="product-flexslider" class="flexslider product_gallery product-{{ product.id }}-gallery {% if product-images == blank %}product_slider{% endif %} {% if settings.product_thumbs == false %}animated fadeInUp{% endif %}">
    <ul class="slides">
      {% for image in product.images %}
      <li data-thumb="{{ image | product_img_url: '1024x1024' }}" data-title="{% if image.alt contains 'youtube' or image.alt contains 'vimeo' %}{{ product.title }}{% else %}{{ image.alt | escape }}{% endif %}">
        {% if image.alt contains 'youtube' or image.alt contains 'vimeo' %}
        {% assign src = image.alt | split: 'src="' %}
        {% assign src = src[1] | split: '"' | first %}

        {% if src contains '?' %}
        {% assign src = src | append: '&amp;autoplay=1' %}
        {% else %}
        {% assign src = src | append: '?autoplay=1' %}
        {% endif %}

        <div class="video-container {% if image.alt contains 'vimeo' %}vimeo{% else %}youtube{% endif %}">
          <div>
            <a href="{{ src }}" title="{{ product.title | escape }}">
              {{ image.alt }}
            </a>
          </div>
        </div>
        {% else %}
        <a href="{{ image | product_img_url: 'master' }}" title="{{ image.alt | escape }}">
          <img  src="{{ image | product_img_url: '1024x1024' }}" alt="{{ image.alt | escape }}"/>
        </a>
        {% endif %}
      </li>
      {% endfor %}
    </ul>
  </div>

  <!-- Carousel -->

  <div id="product-carousel" class="flexslider product_gallery product-{{ product.id }}-gallery {% if product-images == blank %}product_slider{% endif %} {% if settings.product_thumbs == false %}animated fadeInUp{% endif %}">
    <ul class="slides">
      {% for image in product.images %}
      <li data-thumb="{{ image | product_img_url: '1024x1024' }}" data-title="{% if image.alt contains 'youtube' or image.alt contains 'vimeo' %}{{ product.title }}{% else %}{{ image.alt | escape }}{% endif %}">

        <a href="{{ image | product_img_url: 'master' }}" title="{{ image.alt | escape }}">
          <img src="{{ image | product_img_url: '1024x1024' }}" alt="{{ image.alt | escape }}" />
        </a>
      </li>
      {% endfor %}
    </ul>
  </div>
  <!-- End Slider -->

<!-- FlexSlider -->

{{ 'jquery.flexslider-min.js' | asset_url | script_tag }}
{{ 'flexslider.css' | asset_url | stylesheet_tag }}


<script type="text/javascript" charset="utf-8">
(function($) {
  $(window).load(function(){
   $('#product-carousel').flexslider({
    animation: "slide",
    controlNav: false,
    itemWidth: 41,
    itemMargin: 5,
    asNavFor: '#product-flexslider'
  });
  $('#product-flexslider').flexslider({
    animation: "slide",
    controlNav: false,
    sync: "#product-carousel"
  });
});
})(jQuery);

</script>

更新代码后,您可以将嵌入代码放在图像的ALT标记中,同时将图像用作缩略图占位符。

FlexSlider属性可以位于此处: https://github.com/woocommerce/FlexSlider/wiki/FlexSlider-Properties

此处的帖子也帮助解决了另一个问题: https://ecommerce.shopify.com/c/ecommerce-discussion/t/issue-with-variable-image-444159