jQuery carousel -当到达列表上的最后一个项目时,禁用下一个/上一个链接
jQuery carousel - disable next/previous link when last item on the list is reached
我对jQuery非常陌生,我所做的只是遵循这里所做的:
carousel使用jQuery
但是我只是想问,如果我想滑动到最后一个项目,然后禁用链接,表明最后一个项目已经到达,所以我们只需要停止滑动?请帮忙,我真的不需要无限滑动。
这是我的代码
$(function(){
var carousel = $('.carousel ul');
var carouselChild = carousel.find('li');
var clickCount = 0;
var canClick = true;
itemWidth = carousel.find('li:first').width()+1; //Including margin
//Set Carousel width so it won't wrap
carousel.width(itemWidth*carouselChild.length);
//Place the child elements to their original locations.
refreshChildPosition();
//Set the event handlers for buttons.
$('.btnNext').click(function(){
if(canClick){
canClick = false;
clickCount++;
//Animate the slider to left as item width
carousel.stop(false, true).animate({
left : '-='+itemWidth
},300, function(){
//Find the first item and append it as the last item.
lastItem = carousel.find('li:first');
lastItem.remove().appendTo(carousel);
lastItem.css('left', ((carouselChild.length-1)*(itemWidth))+(clickCount*itemWidth));
canClick = true;
});
}
});
$('.btnPrevious').click(function(){
if(canClick){
canClick = false;
clickCount--;
//Find the first item and append it as the last item.
lastItem = carousel.find('li:last');
lastItem.remove().prependTo(carousel);
lastItem.css('left', itemWidth*clickCount);
//Animate the slider to right as item width
carousel.finish(true).animate({
left: '+='+itemWidth
},300, function(){
canClick = true;
});
}
});
function refreshChildPosition(){
carouselChild.each(function(){
$(this).css('left', itemWidth*carouselChild.index($(this)));
});
}
});
<标题> JSFiddle演示 标题>您只需检查滑动窗口的第一个和最后一个li
。
这将为您工作:
$(function(){
var carousel = $('.carousel ul');
var carouselChild = carousel.find('li');
var clickCount = 0;
var canClick = true;
itemWidth = carousel.find('li:first').width()+1; //Including margin
//Set Carousel width so it won't wrap
carousel.width(itemWidth*carouselChild.length);
//Place the child elements to their original locations.
refreshChildPosition();
//Set the event handlers for buttons.
$('.btnNext').click(function(e){
if($(".carousel").find("li:eq(6)").text()!=14) {
if(canClick) {
canClick = false;
clickCount++;
//Animate the slider to left as item width
carousel.stop(false, true).animate({
left : '-='+itemWidth
},300, function(){
//Find the first item and append it as the last item.
lastItem = carousel.find('li:first');
lastItem.remove().appendTo(carousel);
lastItem.css('left', ((carouselChild.length-1)*(itemWidth))+(clickCount*itemWidth));
canClick = true;
});
}
}
});
$('.btnPrevious').click(function(){
if($(".carousel").find("li:eq(0)").text()!=1) {
if(canClick){
canClick = false;
clickCount--;
//Find the first item and append it as the last item.
lastItem = carousel.find('li:last');
lastItem.remove().prependTo(carousel);
lastItem.css('left', itemWidth*clickCount);
//Animate the slider to right as item width
carousel.finish(true).animate({
left: '+='+itemWidth
},300, function(){
canClick = true;
});
}
}
});
function refreshChildPosition(){
carouselChild.each(function(){
$(this).css('left', itemWidth*carouselChild.index($(this)));
});
}
});
<<h1> JSFiddle演示/strong> 相关文章:
- 在javascript中的xml中创建一个链接
- 下一个/上一个锚链接,如何在用户滚动时更新下一个/上一个链接
- 如何在javascript中将下一个和上一个链接添加到当前滑块
- 使悬停保持悬停状态,直到下一个链接
- 水平滚动页面上的“上一个”和“下一个”链接
- 如何使用vanilla Javascript识别无序和/或嵌套列表序列中的上一个/下一个链接
- 如何获得下一个/上一个链接以停留在CarouFredSel幻灯片的两侧
- JavaScript图像库中的下一个和上一个链接不起作用
- 使用jQuery创建一个链接,用于选择下拉选项
- Jquery,添加上一个/下一个链接对幻灯片图像的效果
- 如何使用 jquery 获取下一个/上一个链接,无论它在 DOM 中的位置如何
- 在最好的情况下,我如何检查一个链接属于*.domain.com
- 在angularjs中,点击一个链接后给下一页传递一个ID
- 如何浏览作为链接列表的url数组以及上一个/下一个链接
- 滚动到每12个li元素与下一个/上一个链接的问题
- 如何使一个链接记住它的价值作为一个href之前转移到下一页中使用
- jQuery carousel -当到达列表上的最后一个项目时,禁用下一个/上一个链接
- 使用jquery,当我点击一个链接时,我怎么能引用它上面的选择下拉菜单呢?
- 高图表:我想在向下钻取数据数组中创建一个链接
- MediaWiki如何使用扩展添加下一个和上一个链接:PageAfterAndBefore