Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hi everyone,

I'm beginer to Jquery, i have a requirement of creating a banner, that needs to be changed on every refresh. like i will have a collection of images in a application folder , i have to display them on home page on at a time, if i refresh the page then next image should display.

Using JQuery...
If javascript also no problem...
Posted
Comments
AnkitGoel.com 21-Jun-13 1:54am    
how do you plan to decide which image to show in the banner?
Rockstar_ 21-Jun-13 2:05am    
There are some images in the image folder named like im1, im2, im3 etc..
Nick Ginis 21-Jun-13 3:56am    
Did the solution below help?

Do they need to load in a certain order or can it be random?

If random can just do:


JavaScript
var imageNo = Math.floor((Math.random()*5)+1); /* Change the 5 to the amount of images you have */
$(".banner-img").attr("src", "bannerimage-" + imageNo + ".jpg"); /* Images must be named bannerimage-1.jpg, bannerimage-2.jpg, etc or any convention of your choosing*/


Then just add the class of "banner-img" to your img element where the banner will appear.

JavaScript
<img class="banner-img" />
 
Share this answer
 
v3
Assume your banners are stored in slider under image folder under root directory.

Call this following method on pageload

private void LoadImage()
{

string imagePath = "~/images/slider/";
string imageSource = SiteBaseUrl + "images/slider/";
string PhotoFilePath = Server.MapPath(imagePath);
string allimg = "";
string fileSlide = string.Empty;
string fileName = string.Empty;

DirectoryInfo di = new DirectoryInfo(PhotoFilePath);
FileInfo[] rgFiles = di.GetFiles("*.*");
foreach (FileInfo fi in rgFiles)
{
fileSlide = @"";
allimg += fileSlide;
}
allimg.Replace("'", "\"");
Literal1.Text = allimg + @"
";
}

use this following JQuery



$(document).ready(function () {

//Execute the slideShow
slideShow();

});

function slideShow() {

//Set the opacity of all images to 0
$('#gallery a').css({ opacity: 0.0 });

//Get the first image and display it (set it to full opacity)
$('#gallery a:first').css({ opacity: 1.0 });

//Set the caption background to semi-transparent
$('#gallery .caption').css({ opacity: 0.7 });

//Resize the width of the caption according to the image width
$('#gallery .caption').css({ width: $('#gallery a').find('img').css('width') });

//Get the caption of the first image from REL attribute and display it
$('#gallery .content').html($('#gallery a:first').find('img').attr('rel'))
.animate({ opacity: 0.7 }, 400);

//Call the gallery function to run the slideshow, 6000 = change to next image after 6 seconds
setInterval('gallery()', 6000);

}

function gallery() {

//if no IMGs have the show class, grab the first image
var current = ($('#gallery a.show') ? $('#gallery a.show') : $('#gallery a:first'));

//Get next image, if it reached the end of the slideshow, rotate it back to the first image
var next = ((current.next().length) ? ((current.next().hasClass('caption')) ? $('#gallery a:first') : current.next()) : $('#gallery a:first'));

//Get next image caption
var caption = next.find('img').attr('rel');

//Set the fade in effect for the next image, show class has higher z-index
next.css({ opacity: 0.0 })
.addClass('show')
.animate({ opacity: 1.0 }, 1000);

//Hide the current image
current.animate({ opacity: 0.0 }, 1000)
.removeClass('show');

//Set the opacity to 0 and height to 1px
$('#gallery .caption').animate({ opacity: 0.0 }, { queue: false, duration: 0 }).animate({ height: '1px' }, { queue: true, duration: 300 });

//Animate the caption, opacity to 0.7 and heigth to 100px, a slide up effect
$('#gallery .caption').animate({ opacity: 0.7 }, 100).animate({ height: '60px' }, 500);

//Display the content
$('#gallery .content_2').html(caption);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900