开发者

How to change Images by using jquery by using .load()

开发者 https://www.devze.com 2023-01-22 17:52 出处:网络
I have a list of images. When the page load each time I want to show the next i开发者_如何学Cmage in the series, and when it reloads again I want to change next image, etc.

I have a list of images. When the page load each time I want to show the next i开发者_如何学Cmage in the series, and when it reloads again I want to change next image, etc.

Please suggest me a solution using jQuery.

Thanks


You should consider using server side technology to do this. However, this can be accomplished easily enough with jQuery, or even library-less Javascript.

Using cookies, we can store the current image count, which can then be used to access an array of image URL which is used to change the image on page load. Using the cookie script from QuirksMode, we have a script that looks like this:

var images = ['img1.png', 'img2.png', 'img3.png', 'img4.png' ...],
    imageCount = parseInt(readCookie('image'), 10);

if(isNaN(imageCount)) imageCount = 0;

$('#thumbnail').attr('src', images[imageCount % images.length]);
createCookie('image', imageCount + 1, 30);

If you're using vanilla Javascript, replace the 6th line with this:

document.getElementById('thumbnail').src = images[imageCount % images.length];

See a demo of this script here: http://www.jsfiddle.net/yijiang/2cqqt/


I suggest you use the plugin cycle. It's as easy like that:

$('.slideshow').cycle({
        fx: 'fade'
    });

http://jquery.malsup.com/cycle/

Or if you want a carrousel: jcarousel

http://sorgalla.com/jcarousel/

0

精彩评论

暂无评论...
验证码 换一张
取 消