Используйте template string literals
, чтобы передать значение i
методу :eq
.
$(document).ready(function() {
var i = 0;
$(document).keydown(function(e) {
if (e.which == 39) {
$(`.thing span:eq(${i})`).animate({
opacity: "0.5",
left: "+=50"
}, 100);
}
i++;
});
});
.thing {
height: 130px;
width: 100px;
border: 1px black solid;
display: block;
left: 0px;
position: relative;
}
span {
display: block;
background: #f00;
margin-bottom: 2px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="thing">
<span>0</span>
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
</div>