2015-01-21 26 views
0

Demo無法停止進度條上某一點

使用jQuery插件progressbar.js我不能夠在任何特定點停止進度條。不幸的是,插件api沒有解釋如何做到這一點,但我試圖通過聲明一個正在存儲進度值的目標變量來完成。

target = bar.value(); 

然後我試圖通過應用如果這樣

if ((target * 100).toFixed(0) == 60) { 
    circle.stop(); 
} 

條件,但這不是停在60進度條停止前進!我試圖用此基礎上circle.stop();功能(如果你停止按鈕點擊停止陸侃和警報條(價值目標),但如果stattement不工作!

var target; 
var circle = new ProgressBar.Circle('#container', { 
    color: '#FCB03C', 
    strokeWidth: 3, 
    trailWidth: 1, 
    duration: 1500, 
    bar: 60, 
    text: { 
     value: '0' 
    }, 
    step: function (state, bar) { 
     bar.setText((bar.value() * 100).toFixed(0)); 
     target = bar.value(); 
    } 
}); 

circle.animate(1, function() { 
    circle.animate(); 
}) 


if ((target * 100).toFixed(0) == 60) { 
    circle.stop(); 
} 


$('#stop').on('click', function() { 
    circle.stop(); 
    alert((target * 100).toFixed(0)); 
}); 

能否請你讓我知道我做錯了什麼,我怎麼能解決這個問題?謝謝

回答

1

你的演示不工作的原因是你只檢查目標一次,當它只是宣佈,沒有任何價值(undefined * 100).toFixed(0) == "NaN"。由於javascript isn' t默認情況下,你必須每次檢查條件target更新。step函數是完美的,因爲它被稱爲每次值變動ES。要修正此錯誤,移動:

if ((target * 100).toFixed(0) == 60) { 
    circle.stop(); 
} 

step功能:

step: function (state, bar) { 
    bar.setText((bar.value() * 100).toFixed(0)); 
    target = bar.value(); 
    if ((target * 100).toFixed(0) == 60) { 
     circle.stop(); 
    } 
} 

Fixed demo

+0

感謝Maremp,我已經試過這個,但是得到的錯誤必須是我的代碼中的錯字或其他內容!謝謝 – Suffii 2015-01-21 00:40:49

+0

很高興能幫到你。如果您對細節感興趣,我已經更新了答案以添加更多描述。 – 2015-01-21 00:42:06

+0

當然,非常感謝,順便說一句,我必須等10分鐘才能接受您的答案 – Suffii 2015-01-21 00:42:58