簡體   English   中英

在Shopify或網站上安裝JS代碼

[英]Installing JS code on Shopify or Website

提前致謝。

我創建了此JS函數,該函數可在測試網站上使用,但不適用於shopify。

如何將其稱為產品頁面? 可能那里有一些錯誤

謝謝

 var output, started, duration, desired; // Constants duration = 5000; desired = '50'; // Initial setup output = $('#output'); started = new Date().getTime(); // Animate! animationTimer = setInterval(function() { // If the value is what we want, stop animating // or if the duration has been exceeded, stop animating if (output.text().trim() === desired || new Date().getTime() - started > duration) { console.log('animating'); // Generate a random string to use for the next animation step output.text('' + Math.floor(Math.random() *(50 - 25) + 25) ); } else { console.log('animating'); // Generate a random string to use for the next animation step output.text('' + Math.floor(Math.random() * (50 - 25) + 25) ); } }, 4000); 
 #output { margin: 20px; padding: 20px; background: gray; border-radius: 10px; font-size: 80px; width: 80px; color: white; } 
 <div id="output"></div> 

由於我單擊了產品頁面右上角的箭頭,然后將其復制並粘貼到注釋中,因此注釋中的空間不足,因此可以正常工作。

如果仍然無法正常工作,我唯一想到的另一件事可能是您的主題中沒有包含jquery,因為您的代碼使用了jquery。 為了驗證你的主題已經擁有jQuery的去theme.liquid 按Ctrl + F“的jQuery”

如果找到它,我不確定您遇到什么問題; 如果沒有將其添加到您的主題中,然后再次進行測試。

<div id="output"></div>
<script>// <![CDATA[
var output, started, duration, desired;

// Constants
duration = 5000;
desired = '50';

// Initial setup
output = $('#output');
started = new Date().getTime();

// Animate!
animationTimer = setInterval(function() {
    // If the value is what we want, stop animating
    // or if the duration has been exceeded, stop animating
    if (output.text().trim() === desired || new Date().getTime() - started > duration) {
        console.log('animating');
        // Generate a random string to use for the next animation step
        output.text('' + Math.floor(Math.random() *(50 - 25) + 25) 

        );

    } else {
        console.log('animating');
        // Generate a random string to use for the next animation step
        output.text('' + Math.floor(Math.random() * (50 - 25) + 25)

        );
    }
}, 4000);
// ]]></script>

好解決了

我應該這樣安裝Jquery

{{'//ajax.googleapis.com/ajax/libs/jquery/2.2.3/jquery.min.js'| script_tag}}

而不是這樣

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM