我被分配了一些工作,我必须在包含不同内容的某些块(div)上实现一种老虎机类型的滚动。我想要做的是:每次单击按钮/链接时,我希望一列 4 个块(div)向上移动等于每个块高度的距离。我已经能够为基本功能提出以下建议(感谢 SO 上的出色答案!):
HTML
<div class="slot" id="slot1">
<div class="block">1</div>
<div class="block">2</div>
<div class="block">3</div>
<div class="block last">4</div>
</div>
<div class="slot" id="slot2">
<div class="block">5</div>
<div class="block">6</div>
<div class="block">7</div>
<div class="block last">8</div>
</div>
<div class="slot" id="slot3">
<div class="block">9</div>
<div class="block">10</div>
<div class="block">11</div>
<div class="block">12</div>
</div>
<div class="clear"></div>
<p><a href="#" id="nudge1">nudge1</a></p>
<p><a href="#" id="nudge2">nudge2</a></p>
<p><a href="#" id="nudge3">nudge3</a></p>
CSS
.block {
height:58px;
width:100px;
color: #ccc;
border: 1px solid #666;
position:absolute;
}
.slot {
height: 176px;
width:100px;
border: solid 1px black;
position: relative;
overflow: hidden;
float:left;
}
.clear {
clear:both;
}
p {
display:inline;}
}
JS
$(document).ready(function() {
$(".block").last().addClass("last");
$(".slot").each(function() {
var i = 0;
$(this).find(".block").each(function() {
var $this = $(this);
$this.css("top", i);
i += $this.height();
});
});
$('#nudge1').on('click',function() {
var countScrolls = $('.slot .block').length;
for (var i=0; i < countScrolls; i++) {
var top = parseInt($('#slot1 .block:nth-child(' + i + ')').css("top"));
if (top < -58) {
var $lastEle = $('#slot1 .block:nth-child(' + i + ')').closest('.slot').find(".last");
$lastEle.removeClass("last");
$('#slot1 .block:nth-child(' + i + ')').addClass("last");
var top = (parseInt($lastEle.css("top")) + $lastEle.height());
$('#slot1 .block:nth-child(' + i + ')').css("top", top);
}
$('#slot1 .block:nth-child(' + i + ')').clearQueue().stop().animate({
top: (parseInt(top) - 58)
}, 100, 'linear');
}
});
$('#nudge2').on('click',function() {
var countScrolls = $('.slot .block').length;
for (var i=0; i < countScrolls; i++) {
var top = parseInt($('#slot2 .block:nth-child(' + i + ')').css("top"));
if (top < -58) {
var $lastEle = $('#slot2 .block:nth-child(' + i + ')').closest('.slot').find(".last");
$lastEle.removeClass("last");
$('#slot2 .block:nth-child(' + i + ')').addClass("last");
var top = (parseInt($lastEle.css("top")) + $lastEle.height());
$('#slot2 .block:nth-child(' + i + ')').css("top", top);
}
$('#slot2 .block:nth-child(' + i + ')').clearQueue().stop().animate({
top: (parseInt(top) - 58)
}, 100, 'linear');
}
});
$('#nudge3').on('click',function() {
var countScrolls = $('.slot .block').length;
for (var i=0; i < countScrolls; i++) {
var top = parseInt($('#slot3 .block:nth-child(' + i + ')').css("top"));
if (top < -58) {
var $lastEle = $('#slot2 .block:nth-child(' + i + ')').closest('.slot').find(".last");
$lastEle.removeClass("last");
$('#slot3 .block:nth-child(' + i + ')').addClass("last");
var top = (parseInt($lastEle.css("top")) + $lastEle.height());
$('#slot3 .block:nth-child(' + i + ')').css("top", top);
}
$('#slot3 .block:nth-child(' + i + ')').clearQueue().stop().animate({
top: (parseInt(top) - 58)
}, 100, 'linear');
}
});
});
这是一个小提琴供更好的参考。单击“轻推”会使块动画向上移动一个高度,该高度等于每个块的高度。我的问题是,现在这一次只适用于两个块,我不知道如何显示更多(特别是 3 个)。任何人都可以建议我在这里做到这一点吗?