无法使用“上一个”和“下一个”箭头对数组进行切片
Not able to slice the array using `prev` and `next` arrow
我有一个数组值,需要切片并显示在网页中。 我有prev
和next
箭头,根据数组导航,我正在对数组值进行切片。 当我使用箭头时next
这是有效的。 但不适用于prev
数组。
从逻辑上讲,我在这里犯了一些错误。 同样,当next
或prev
超过数组长度或小于数组长度时,我正在应用opacity
并禁用该按钮。
有人在这里纠正我的错误吗?
这是我的代码:
var num = 0;
var myArray = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"];
var batch = 5;
var value = 0;
var showArray = function (num) {
var required = myArray.slice(value, (batch+value));
console.log(required, num);
value += 5;
}
showArray(0);
function add(amount) {
num = (num + myArray.length - batch + amount) % myArray.length + batch;
showArray(num);
}
$('#next-arrow').click(function(e){
if(value >= myArray.length) {
$(e.target).css({ opacity:0.5 });
return
}
$(e.target).css({ opacity:1});
add(batch)
});
$('#prev-arrow').click(function(e){
if(value <= myArray.length) {
$(e.target).css({ opacity:0.5 });
return
}
$(e.target).css({ opacity:1});
add(-batch)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<a href="#" id="prev-arrow">Previous</a>
<a href="#" id="next-arrow">Next</a>
</div>
请尝试这个
$(document).ready(function(){
var num = 0;
var myArray = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"];
var batch = 5;
var nextIndex = 0;
var prevIndex = 0;
var showArray = function (boolShowPrev) {
var required;
if(boolShowPrev){
console.log("==",nextIndex, nextIndex+batch);
required = myArray.slice(nextIndex, nextIndex+batch);
console.log(required)
nextIndex += 5;
prevIndex -= 5;
}else{
if(nextIndex >= myArray.length){
prevIndex=batch- (nextIndex-myArray.length);
required = myArray.slice(-prevIndex);
console.log(required);
nextIndex -= 5;
}else{
console.log("prev",-batch-prevIndex,-prevIndex)
required = myArray.slice(-batch-prevIndex,-prevIndex);
console.log(required);
prevIndex +=batch;
nextIndex -= 5;
}
}
}
showArray(true);
$('#next-arrow').click(function(e){
if(nextIndex >= myArray.length) {
$(e.target).css({ opacity:0.5 });
return
}
$(e.target).css({ opacity:1});
showArray(true)
});
$('#prev-arrow').click(function(e){
if(prevIndex >= myArray.length) {
$(e.target).css({ opacity:0.5 });
return
}
$(e.target).css({ opacity:1});
showArray(false)
});
})
请参考这个 普伦克
相关文章:
- 当滑块只有一个图像时,如何禁用下一个/上一个按钮
- jQuery UI 选项卡 - 将参数设置为下一个/上一个按钮单击
- 下一个/上一个锚链接,如何在用户滚动时更新下一个/上一个链接
- Jquery UI选项卡&选择方框-下一个/上一个按钮
- 引导分页下一个上一个不起作用
- 使用“下一个”/“上一个”按钮切换图像
- 角度 - ng 网格通过分组按语法选择下一个/上一个项目
- 下一个/上一个 jQuery 控件类跳转了太多的列表项
- 显示带有缩略图和下一个/上一个按钮的图像
- 花式框下一个/上一个不显示 iframe
- j查询滑动选项卡:控制选项 - 下一个/上一个
- 如何将左/右箭头(下一个/上一个)功能添加到我的 jQuery 图像和文本滑块
- j查询带有下一个/上一个的 UI 选项卡
- 如何通过幻灯片查看活动箭头(下一个/上一个)
- 尝试运行下一个上一个按钮以播放列表媒体播放器代码中的视频,出现错误
- 修复了标题导航和滚动到()下一个/上一个元素
- 将下一个/上一个输入集中在达到最大长度或退格键上
- 数据表:禁用第一个下一个上一个最后一个,并在处理时显示/搜索记录
- 如何从简介.js工具提示中动态添加或删除下一个/上一个按钮
- Javascript幻灯片与播放暂停和下一个上一个按钮