jQuery/Ajax : image slider

前端 未结 1 1047
终归单人心
终归单人心 2021-01-17 06:35

There is a row which can accommodate only three images at once but i have several images in server and all i want to fetch. so in that case i need image slider.

Let

相关标签:
1条回答
  • 2021-01-17 07:28

    You can put your image names in an array and then loop through them. I used three div's to hold the images but you could use anything.

    $(function () {
        var images = ["img_1", "img_2", "img_3", "img_4", "img_5", "img_6", "img_7", "img_8", "img_9", "img_10"],
            curIndex = 0,
            gotoImage = function (index) {
                $('#images div').each(function (i) {
                   var image = curIndex + i;
                    if (image >= images.length) {
                        image = image - images.length;
                    }
                   $(this).html(images[image]);
                   // Use something like this to show images instead of text
                   // $(this).html('<img src="' + images[image] + '.jpg" />');
                });            
            };
    
        $('.prev').click(function (e) {
            curIndex--;
            if (curIndex === -1) {
                curIndex = images.length - 1;
            }
            gotoImage(curIndex);
        });
    
        $('.next').click(function (e) {
            curIndex++;
            if (curIndex === images.length) {
                curIndex = 0;
            }
            gotoImage(curIndex);
        });
    });
    

    Here is an example: http://jsfiddle.net/rustyjeans/c3sJW/

    There's no need to use ajax to load the images, unless you don't know what the list of images is going to be. If you're pulling the list of image names from a databse you could use ajax to get that data out and put it in an array, then you could assign that to the array images.

    0 讨论(0)
提交回复
热议问题