2017-08-16 62 views
1

我有一個圓形的進度欄,並且想要在點擊div時更新它(以及它的跨度值)。點擊應該將輸入的值添加到var。我已經創建了這個小預覽。我的問題是var在點擊時似乎沒有改變。誰能幫忙?謝謝!jQuery:單擊時更改var

var calories = 1000; 
 
var total = 2500; 
 
var percentage = calories/total; 
 
var pluscal = $('#quantity').val(); 
 
var span = $('#today-progress span').html(calories + '/' + total); 
 

 

 
/* setInterval(() => { 
 
    calories = 0 
 
}, 1000 * 60 * 60 * 24); 
 
var today = new Date().getTime(); 
 
localStorage.setItem("today", today); */ 
 

 

 
$('#confirm-svg').click(function() { 
 
    calories = calories + pluscal; 
 
    $('#today-progress').circleProgress(); 
 
}); 
 

 

 
$('#today-progress').circleProgress({ 
 
    value: percentage, 
 
    size: 300.0, 
 
    startAngle: -1.57, 
 
    thickness: 'auto', 
 
    fill: { 
 
    gradient: ['#CE6363', '#B23939'], 
 
    }, 
 
    emptyFill: '#D6A0A0', 
 
    animation: { 
 
    duration: 3000, 
 
    easing: 'circleProgressEasing' 
 
    }, 
 
    animationStartValue: 0.0, 
 
    reverse: false, 
 
    lineCap: 'round', 
 
});
body { 
 
    font-family: Helvetica, sans-serif; 
 
} 
 

 
#today-progress { 
 
    z-index: 0; 
 
    position: relative; 
 
    top: 10px; 
 
    text-align: center; 
 
} 
 

 
#today-progress span { 
 
    position: absolute; 
 
    top: 50%; 
 
    left: 50%; 
 
    transform: translate(-50%, -50%); 
 
    text-align: center; 
 
    font-size: 45px; 
 
} 
 

 
svg { 
 
    width: 30px; 
 
    height: 30px; 
 
    margin: 5px 10px; 
 
    float: right; 
 
    fill: #000; 
 
} 
 

 
svg:active { 
 
    fill: red; 
 
} 
 

 
form { 
 
    margin-top: 20px; 
 
    margin-left: 20px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://rawgit.com/kottenator/jquery-circle-progress/1.2.2/dist/circle-progress.js"></script> 
 
<div id="today-progress"> 
 
    <span></span> 
 
</div> 
 

 
<form> 
 
    Quantity (between 1 and 1000): 
 
    <input type="number" id="quantity" min="1" max="1000"> 
 
</form> 
 

 
<div id="confirm-svg"> 
 
    <svg id="confirm-svg" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Capa_1" x="0px" y="0px" width="512px" height="512px" viewBox="0 0 305.002 305.002" style="enable-background:new 0 0 305.002 305.002;" 
 
    xml:space="preserve"> 
 
    <g> 
 
     <g> 
 
     <path d="M152.502,0.001C68.412,0.001,0,68.412,0,152.501s68.412,152.5,152.502,152.5c84.089,0,152.5-68.411,152.5-152.5 S236.591,0.001,152.502,0.001z M152.502,280.001C82.197,280.001,25,222.806,25,152.501c0-70.304,57.197-127.5,127.502-127.5 c70.304,0,127.5,57.196,127.5,127.5C280.002,222.806,222.806,280.001,152.502,280.001z" 
 
     /> 
 
     <path d="M218.473,93.97l-90.546,90.547l-41.398-41.398c-4.882-4.881-12.796-4.881-17.678,0c-4.881,4.882-4.881,12.796,0,17.678 l50.237,50.237c2.441,2.44,5.64,3.661,8.839,3.661c3.199,0,6.398-1.221,8.839-3.661l99.385-99.385 c4.881-4.882,4.881-12.796,0-17.678C231.269,89.089,223.354,89.089,218.473,93.97z" 
 
     /> 
 
     </g> 
 
    </g> 
 
    <g> 
 
    </g> 
 
    <g> 
 
    </g> 
 
    <g> 
 
    </g> 
 
    <g> 
 
    </g> 
 
    <g> 
 
    </g> 
 
    <g> 
 
    </g> 
 
    <g> 
 
    </g> 
 
    <g> 
 
    </g> 
 
    <g> 
 
    </g> 
 
    <g> 
 
    </g> 
 
    <g> 
 
    </g> 
 
    <g> 
 
    </g> 
 
    <g> 
 
    </g> 
 
    <g> 
 
    </g> 
 
    <g> 
 
    </g> 
 
    </svg> 
 
</div>

(的jsfiddle here) 另外,如果有可能,我想重置該變種(卡路里),以每24小時0,你可以在這個在看我可憐的嘗試在js代碼發表評論。如果你能幫助我與,這將是真棒太:)

回答

1

您必須更新每次單擊提交複選標記按鈕時在頂部聲明的變量。

此外,你應該parseInt從輸入框中的值,你應該打電話circleProgress()與所有必要的設置來重繪圓的東西。你現在的方式,沒有設置。

類似下面,讓您開始:See JSFiddle here

$('#confirm-svg').click(function() { 
    // Every time you click, you should parse the integer from the input, 
    // then add that to the sum of calories. 
    calories = calories + parseInt($('#quantity').val(), 10); 
    percentage = calories/total; 

    // Then you have to update the calories mark with the new value of calories 
    $('#today-progress span').html(calories + '/' + total); 
    // Then you have to redraw the circle stuff 
    drawCircle() 
}); 

function drawCircle() { 
    $('#today-progress').circleProgress({ 
    value: percentage, 
    size: 300.0, 
    startAngle: -1.57, 
    thickness: 'auto', 
    fill: { 
     gradient: ['#CE6363', '#B23939'], 
    }, 
    emptyFill: '#D6A0A0', 
    animation: { 
     duration: 3000, 
     easing: 'circleProgressEasing' 
    }, 
    animationStartValue: 0.0, 
    reverse: false, 
    lineCap: 'round', 
    }); 
} 

// Draw the initial circle 
drawCircle(); 
+0

非常感謝!這工作,我完全忘記了parseInt的必要性..我在函數drawCircle中添加了跨度變化更新,所以只有這個函數可以調用,但你解決了它,謝謝:) –

+0

對不起,我想接受你的答案,但有仍然重置問題沒有解決,如果我接受它,沒有人會去看它,如果這對你有好處,我會接受它 –

+0

@MaëlleJumel你可以設置一個超時時間讓它在24小時後重置,但是誰是將在24小時等待您的網站只是爲了重置?如果你正在談論永久存儲,那是完全不同的,並且需要一個後端數據庫來維護你的數據。前端只關注視圖,應該從其他地方接收數據。它只是以一種友好的格式呈現數據。 – nbkhope

1

在你擺弄我加...

setInterval(() => { 
    calories = 0; 
    $('#today-progress').circleProgress(); 
}, 5000); 

然後每5秒用動畫重置它。無視5秒vs 24小時,這是你在追求什麼?

+0

嗨,謝謝你的回答!這是我正在尋找的東西,但變種不是回到0?你也知道,如果用你的代碼,刷新時值會保持不變,這意味着間隔不會再次開始? –

+0

@MaëlleJumel我想你錯過了Web開發的一些基本概念。前端沒有任何東西是永久的,如果刷新頁面,它將回到其初始狀態。是的,你可以嘗試使用本地存儲,但這隻適用於應用程序的本地用戶。要擁有「真正的永久存儲」,您需要了解後端和數據庫的概念。你在前端看到的只不過是從其他地方接收到的友好的數據的再現。總之,前端總是首先從服務器獲取一些數據,然後以人性化的格式顯示它。 – nbkhope

+0

是的,我想使它成爲「真正的永久存儲」,我需要了解這一點。對不起,我確實不是一個專家,我只通過創建網站草稿(HTML/CSS/JS),但從來沒有一個實際的網站與域和實際數據瞭解到主要的東西@nbkhope –

1

要回答你的復位卡路里變量的問題, Assumning你希望每次上午10點重置, 嘗試:

var now = new Date(); 
    var millisTill10 = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 10, 0, 0, 0) - now; 
    if (millisTill10 < 0) { 
     millisTill10 += 86400000; // it's after 10am, try 10am tomorrow. 10 is my number 
    } 
    setTimeout(function(){var calories = 0;}), 

設我知道它是如何工作的(不要貶低投票,只是)有趣的編碼;-)。

+0

嘿,謝謝!雖然這與其他答案一樣正確,但只有在不重新加載頁面的情況下才有效。 –

+0

是的。你的意圖是什麼意圖你的應用程序工作? –

+1

您可以創建一個簡單的PHP腳本來照顧時間。這將照顧刷新頁面。 Le'me知道你是否需要幫助。 –