简体   繁体   English

jQuery滑块不起作用。 下一个和上一个按钮分别在最后一个图像和第一个图像时应该消失

[英]JQuery Slider Not Working. Next and Previous Buttons should disappear when at the last and first image respectively

I have a code for JQuery Slider which is not responding. 我有一个JQuery Slider没有响应的代码。

The Slider has 4 images and 2 buttons: Next and Previous. 滑块具有4个图像和2个按钮:下一个和上一个。 When the user reaches the last image, the Next Button should not be seen and similarly when the user is on the first image, the previous button should not be seen. 当用户到达最后一个图像时,不应看到“下一个”按钮,类似地,当用户位于第一个图像上时,则不应看到上一个按钮。

But at all other times, the next and previous buttons should be seen. 但是在所有其他时间,都应该看到下一个和上一个按钮。

I have zipped the entire directory here: Click 我在此处压缩了整个目录: 单击

I dont expect someone to actually sit and debug my code, But even if I am told why my JS doesn't work, I would be helped enough. 我不希望有人真正坐下来调试我的代码,但是即使我被告知为什么我的JS无法正常工作,我也会得到足够的帮助。

I am a novice with Jquery and JS. 我是Jquery和JS的新手。 Any Help would be appreciated. 任何帮助,将不胜感激。

JS Code (After the Modifications suggested by Jon): JS代码(在乔恩建议的修改之后):

   $(document).ready(function(){
$('.slider img:first').addClass('active');                    // Here we are assigning a class "active" to the first image in the "slider" div.

var imagewidth = $('.visible-area').width();                  // Width of 1 image (should be equal to the width of "visible-area" box)
var totalimages = $('.slider img').size();                    // Total Number of images inside "slider" div.
var sliderwidth = imagewidth * totalimages;                   // Total width of "slider" div.
$('.slider').css({'width': sliderwidth});                     // Here we are assigning the width to the slider div (using the css method in jquery)

$('.next').click(function(){                                  // This following function will be executed on click of "next" button
$active = $('.slider img.active').next();                 // On click of next button, we are saving the image (next to "active" image) in a jQuery variable $active

$('.slider img').removeClass('active');                   // Remove class active from the images inside slider div.
$active.addClass('active');                               // Add the class active to the $active (next image).

var count = $active.attr('alt') -1;                       
var sliderposition = count * imagewidth;                  // Here we are calculating, how much "slider" div will slide on click of next button, and we are saving it in a variable "sliderposition".
$('.slider').animate({'left': -sliderposition}, 500);     // Here we are using the jQuery animate method to slide the "slider" div.

$active = $('.slider img.active').next();                 // On click of next button, we are saving the image (next to "active" image) in a jQuery variable $active

if ($active.length==0){                                 // If this is the last image inside the "slider" div, hide the next button
    $('.next').hide();
}});

$('.previous').click(function(){                              // This following function will be executed on click of "previous" button
    $active = $('.slider img.active').prev();                 // On click of previous button, we are saving the image (previous to "active" image) in a jQuery variable $active.
    if ($active.length==0){                                   // If this is the first image inside the "slider" div, hide the previous button
    //$(this).hide();
    }
    $('.slider img').removeClass('active');                   // Remove class active from the images inside slider div.
    $active.addClass('active');                               // Add the class active to the $active (next image).

    var count = $active.attr('alt') -1;                       
    var sliderposition = count * imagewidth;                  // Here we are calculating, how much "slider" div will slide on click of next button, and we are saving it in a variable "sliderposition".
    $('.slider').animate({'left': -sliderposition}, 500);     // Here we are using the jQuery animate method to slide the "slider" div.
});     

}); });

$('.next').click(function(){                                  // This following function will be executed on click of "next" button
    $active = $('.slider img.active').next();                 // On click of next button, we are saving the image (next to "active" image) in a jQuery variable $active
    if ($active.length==0){                                 // If this is the last image inside the "slider" div, hide the next button
    $(this).hide();
    }
    $('.slider img').removeClass('active');                   // Remove class active from the images inside slider div.
    $active.addClass('active');                               // Add the class active to the $active (next image).

    var count = $active.attr('alt') -1;                       
    var sliderposition = count * imagewidth;                  // Here we are calculating, how much "slider" div will slide on click of next button, and we are saving it in a variable "sliderposition".
    $('.slider').animate({'left': -sliderposition}, 500);     // Here we are using the jQuery animate method to slide the "slider" div.
}); 

You have another problem beyond that not working, it looks like. 看起来,除了问题之外,您还有另一个问题。

Right now, this code checks the next item. 现在,此代码检查下一项。 If no next item is found, you hide the button. 如果找不到下一项,则隐藏按钮。

After that, you then remove the class from all the images, and add the active class to the next item. 之后,您可以从所有图像中删除该类别,然后将活动类别添加到下一个项目。

Your code only hides the button if you are already on the last item and click the next button. 如果您已经在最后一项上,然后单击下一步,则您的代码仅隐藏该按钮。 It then will hide all the images, but because no next item was found, it has nothing to set as active. 然后它将隐藏所有图像,但是由于未找到下一个项目,因此没有任何内容设置为活动状态。

Changing the order of operations to check for anything after the active item is set may help you. 设置活动项目后,更改操作顺序以检查所有内容可能会有所帮助。

$('.next').click(function(){                                  // This following function will be executed on click of "next" button
    $active = $('.slider img.active').next();                 // On click of next button, we are saving the image (next to "active" image) in a jQuery variable $active

    $('.slider img').removeClass('active');                   // Remove class active from the images inside slider div.
    $active.addClass('active');                               // Add the class active to the $active (next image).

    var count = $active.attr('alt') -1;                       
    var sliderposition = count * imagewidth;                  // Here we are calculating, how much "slider" div will slide on click of next button, and we are saving it in a variable "sliderposition".
    $('.slider').animate({'left': -sliderposition}, 500);     // Here we are using the jQuery animate method to slide the "slider" div.

    $active = $('.slider img.active').next();                 // On click of next button, we are saving the image (next to "active" image) in a jQuery variable $active

    if ($active.length==0){                                 // If this is the last image inside the "slider" div, hide the next button
        $('.next').hide();
    }
});

This way, you set the new active item, move the slider position, and then check to see if there is any more items after the newly selected one. 这样,您可以设置新的活动项目,移动滑块位置,然后检查在新选择的项目之后是否还有其他项目。 If there isn't, hide the button. 如果没有,请隐藏按钮。

The problem is with first line of code in prev() and next() function ie 问题在于prev()和next()函数中的第一行代码,即

$active = $('.slider img.active').next();
$active = $('.slider img.active').prev();

Above code is actually trying to find next/previous element under anchor tag which is not there. 上面的代码实际上是尝试在锚标签下找到下一个/上一个元素,但该元素不存在。 So work around this is as below: 因此,解决方法如下:

$active = $('.slider img.active').parent().next().children();
$active = $('.slider img.active').parent().prev().children();

By this way you will come one level up to image tag and would be able to find next/previous anchor tag. 通过这种方式,您可以上一层图像标签,并且能够找到下一个/上一个锚标签。 Then you can find the childern of that anchor tag, which is your desired image. 然后,您可以找到该锚标签的子标签,这就是您想要的图像。

Hope this will help!!! 希望这会有所帮助!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TinySlider:在第一个/最后一个索引时使上一个/下一个按钮消失? - TinySlider: Make prev/next buttons disappear when at first/last index? 分别到达最后一张和第一张幻灯片后,如何使“下一个”按钮和“上一个”按钮不起作用? - How to make Next button and Previous button non-working after I reach last and first slide respectively? 当第一个或最后一个项目处于活动状态时隐藏下一个和上一个按钮 - Hiding Next and Previous buttons when First or Last items are active jQuery滑块在上一个或最后一个时隐藏上一个和下一个 - Jquery slider hide prev and next when first or last 当画廊只有一个图像时,jQuery隐藏下一个和上一个按钮不起作用 - jQuery to hide next & previous buttons not working when the gallery has only one image 使上一个/下一个按钮消失 - Getting previous/next buttons to disappear 使jQuery图像滑块在最后一张图像处停止并添加下一个和上一个按钮 - Making a jQuery image slider stop at last image and adding next and prev buttons 带模式上一个/下一个按钮的图像滑块不起作用? - Image slider with modal prev/next buttons not working? jQuery分页的下一个和上一个按钮不起作用 - jQuery pagination next and previous buttons are not working Swiper JS Slider 下一个/上一个按钮无法正常工作 - Swiper JS Slider next/previous buttons aren't working perfectly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM