如何使用jquery移动到下一个图像?

时间:2013-07-09 09:48:01

标签: jquery

我在移动到下一张图片时遇到问题。就像我们移动到FB中的下一张图片一样。 我尝试但无法实现。 rightimage是右箭头的类

$('.rightimage').on("click", function (event) {

    var sectionId = $(this).parents('ul').attr('id'); // i get the current image
    var outputId = sectionId.replace('ul', 'img'); // i get the current image id
    var NextId = outputId + 2; //increment and assign it to another var

    if ($('NextId').length) { // see if the image with id exists    

        // here i have to put the image if it exists else i have to check or the next image
    }
    if ($('NextId+1' > rightImgcounter)) {

        // disable the button
    }
}); 

'rightImgcounter'是全局javascript变量,它为我们的右图像提供序列号。这些影像是动态生成的。  不知道如何移动到屏幕上的右下一个图像。如果右下方没有图像,则禁用该按钮。

1 个答案:

答案 0 :(得分:0)

看看这是否有帮助:

// Using the jQuery version 1.7+
$('.rightimage').on("click", function (event) {

    var sectionId = $(this).parents('ul').attr('id');
    var outputId = sectionId.replace('ul', 'img');

    //var NextId = outputId + 2;    
    var NextId = outputId.replace(/(\d+)+/g, function (_, number) {
        return parseInt(number) + 2;
    });

    // Check the NextId in the console (for debugging purpose) 
    console.log(NextId);

    // see if the image with id exists 
    if ($('#' + NextId).length) {
        // here i have to put the image if it exists 

    }

    var counter = parseInt(/(\d+)/.exec(NextId)[0], 10) + 1;
    if (counter > rightImgcounter) {

        // disable the button
        $(this).prop('disabled', true);
    }
});