简体   繁体   English

灯箱图片滑块无法正常点击,滑块无法正确循环播放

[英]Lightbox image slider is not coming click wise and slider is not looping properly

In my code i have three images and when each image is clicked a modal box will open and that box will have a function to show all images by clicking right arrow(next), left arrow(prev). 在我的代码中,我有三个图像,当单击每个图像时,将打开一个模式框,该框将具有通过单击右箭头(下一个),左箭头(上一个)来显示所有图像的功能。

I've done all basic things but I am facing two problems: 我已经完成了所有基本工作,但是面临两个问题:

  1. When the next button is clicked at the time of last image it giving blank, the same is happening with prev button at first image time. 当最后一个图像时单击下一个按钮时,它会显示为空白,而在上一个图像时,上一个按钮也会发生同样的情况。
  2. when at the page I click on any image it opens the modal with the first image every time, how to enable that when modal will open with particular image which I've clicked. 当我在页面上单击任何图像时,每次都会用第一张图像打开模态,如何在模态打开时使用我单击的特定图像来启用模态。

This is my JavaScript code: 这是我的JavaScript代码:

//next prev images
//active image setting
$('.image_post li:first').addClass('activepostimg');
//code for next image  
$('.buttons_next').on('click', function() {
    $('.image_post li.activepostimg').index() + parseInt(1) !== $('.image_post li').length ; 
    $('.activepostimg').removeClass('activepostimg').next('.image_post li').addClass('activepostimg');  
});
//code for previous
$('.buttons_prev').on('click', function() {
    $('.image_post li.activepostimg').index() + parseInt(1) !== $('.image_post li').length ; 
    $('.activepostimg').removeClass('activepostimg').prev('.image_post li').addClass('activepostimg');
});
//Image counter
var totalItems = $('.image_post li').length;
var currentIndex = $('.activepostimg').index() + 1;
$('.slide_image_counter a').html(''+currentIndex+'/'+totalItems+'');
//for next

Code on Jsfiddle Jsfiddle上的代码

Replace all your js code and remove class="activepostimg" from your html. 替换所有的js代码,并从HTML中删除class="activepostimg" Here is the DEMO 这是演示

Make sure that your images are in the same order when they are zoomed and not. 确保图像缩放时的顺序相同,而不要缩放。

var totalItems = $('.image_post li').length, currentIndex = 1;
$('.slide_image_counter a').html('' + currentIndex + '/' + totalItems + '');

//code for opening image
$('.grid img').on('click', function() {
    currentIndex = $('.grid img').index(this) + 1;
    $('.slide_image_counter a').html('' + currentIndex + '/' + totalItems + '');
    $(".image_post li").removeClass("activepostimg");
    $('.image_post li').eq(currentIndex - 1).addClass('activepostimg')
});

//code for next image  
$('.buttons_next').on('click', function() {
    if($('.image_post li.activepostimg').index() < ($('.image_post li').length - 1)){
        currentIndex++;
        $('.slide_image_counter a').html('' + currentIndex + '/' + totalItems + '');
        $('.activepostimg').removeClass('activepostimg').next('.image_post li').addClass('activepostimg');  
    } else {
        currentIndex = 1;
        $('.slide_image_counter a').html('' + currentIndex + '/' + totalItems + '');
        $(".image_post li").removeClass("activepostimg");
        $('.image_post li').eq(currentIndex - 1).addClass('activepostimg')
    }
});

//code for previous
$('.buttons_prev').on('click', function() {
    if ($('.image_post li.activepostimg').index() > 0) {
            currentIndex--;
        $('.slide_image_counter a').html('' + currentIndex + '/' + totalItems + '');
        $('.activepostimg').removeClass('activepostimg').prev('.image_post li').addClass('activepostimg');
    } else {
        currentIndex = $('.image_post li').length;
        $('.slide_image_counter a').html('' + currentIndex + '/' + totalItems + '');
        $(".image_post li").removeClass("activepostimg");
        $('.image_post li').eq(currentIndex - 1).addClass('activepostimg')
    }
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM