我想在 javascript 中开始我的 onclick() 函数,style.marginLeft=0
就像从'0px' 到'-100%'到'-200%'开始,然后以'-200%'结尾,当它变成 '-200% '如果您再次单击它,它将带您回到 .marginLeft='0'
Jsfiddle 上的代码
var x = document.getElementsByClassName('box')[0];
var u = -100;
function addMargin() {
var current = x.style.marginLeft = u + '%';
if (x.style.marginLeft == -200) {
x.style.marginLeft = '0';
} else {}
}
#container { display: inline-block; position: relative; width: 100%; height: 150px; white-space:
nowrap; overflow: hidden; }
.box { width: 100%; height: 150px;
display: inline-block; position: relative; }
.box:nth-child(1) { background: lightblue; }
.box:nth-child(2) { background: red; }
.box:nth-child(3) { background: green; }
<div id='container'>
<div class='box'>1</div>
<div class='box'>2</div>
<div class='box'>3</div>
</div>
<input type="button" value="Next>" onclick="addMargin()" />