我正在用JavaScript構建一個基本滑塊。每次單擊按鈕時,div滑塊應增加其margin-left "-100px"
。每次點擊增加style.marginLeft?
我有這樣的代碼:
document.getElementById('core-next').onclick = function() {
document.getElementById('slides').style.marginLeft = "-100px";
}
而且它的方式,當我點擊#core-next
保證金被設置爲-100px工作。 但我想實現的是每次點擊一個按鈕時,邊距增加-100px
。
所以它看起來像:-100px, -200px, -300px...
這是可能的純JavaScript? jQuery有"+="
!我可以在Javascript中執行此操作,但不添加其他變量嗎?
我嘗試這樣做:
document.getElementById('core-next').onclick = function() {
document.getElementById('slides').style.marginLeft -= 200 + 'px';
}
但它不工作...
有沒有辦法在JavaScript來實現這一點,而不產生額外的變量,將舉行保證金價值?
謝謝!
參考[該解決方案(http://stackoverflow.com/questions/2781887/how-to-increase-decrease-current-margin-at-a-number-by-jquery)你可能會得到解決! –