简体   繁体   English

Shopify Liquid 文件中的部分加载事件 (Shopify)

[英]Shopify Section load event in Liquid file (Shopify)

I am Shopify developer.我是 Shopify 开发者。 I designed a slideshow using Flickity library in Shopify. My slider working fine on URL when load but it's not work on Shopify Theme Editor, till I am not saved the design.我在 Shopify 中使用 Flickity 库设计了一个幻灯片。我的 slider 在加载时在 URL 上工作正常,但它在 Shopify 主题编辑器上不起作用,直到我没有保存设计。 after save design is working fine.保存后设计工作正常。

I am using this script in section file it's not working but when I paste this code in theme.liquid file it's working fine on both sides.我在节文件中使用这个脚本它不起作用但是当我将这个代码粘贴到 theme.liquid 文件中时它在两边都工作正常。

I don't want like this image我不想喜欢这个图片

在此处输入图像描述


<script>
  {% if request.design_mode %}
        console.log("123");
        document.addEventListener("shopify:section:load", function() {
        console.log("abc");
        var elem = document.querySelector('.carousel');
            var flkty = new Flickity( elem, {
                autoPlay: "{{ section.settings.autoplay_slide }}", 
                pageDots: true, 
                contain: true, 
                wrapAround: false, 
                imagesLoaded: true, 
                accessibility: false
            });
        });
      {% endif %}
</script>

I want, that when i select section from theme editor it's working smoothly.我想,当我 select 来自主题编辑器的部分时,它工作顺利。

I want like this image我想要这个图片

在此处输入图像描述

Instead of using shopify:section:load which fires only inside the theme editor (that's why you can't get it working outside of theme editor), you should include this <script> tag in the end of your section's code and change it a bit:不要使用仅在主题编辑器内部触发shopify:section:load (这就是为什么您不能在主题编辑器之外使用它的原因),您应该在您的部分代码末尾包含此<script>标记并将其更改为少量:

<script>
  {% if request.design_mode %}
        console.log("123");
        let flkty; // for later use
        document.addEventListener("DOMContentLoaded", function() { // run this script after window is ready and scripts are loaded
            console.log("abc");
            var elem = document.querySelector('.carousel');
            if (elem) { // check if element was found before using it
                 flkty = new Flickity( elem, {
                    autoPlay: "{{ section.settings.autoplay_slide }}", 
                    pageDots: true, 
                    contain: true, 
                    wrapAround: false, 
                    imagesLoaded: true, 
                    accessibility: false
                 });
             }
        });
      {% endif %}
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM