In your HTML code, you can call display('imgs/imgNew.jpg', idx) on page load, or respond to a button's click event: <input type="button" value="Display imgNew" onclick="display('imgs/imgNew.jpg', 2)" />
PS:
Maybe beforeSlideChange event handler is also useful if you want to trigger the display function at a specific point. See aforementioned demo 4 for details.
hi Milo,
your code worked only for one image slide, I was not able to click on the next button multiple time for multiple slides.
in the below example, I want each time I click on the >, it should slide to a new image("images/image-slider-x.jpg" for x = 1 to n). So I am trying to find out if it's possible to use your slider if I don't have a set of images defined when the page is loaded, and can images be dynamically added to the queue(sorry for lack of better word)
<!DOCTYPE html>
<html>
<head>
<title>Demo 4 - with Navigation Buttons</title>
<link href="themes/4/js-image-slider.css" rel="stylesheet" type="text/css" />
<script src="themes/4/js-image-slider.js" type="text/javascript"></script>
<link href="generic.css" rel="stylesheet" type="text/css" />
<script>
var mcSlides = [];
function getSlides() {
if (mcSlides.length == 0) {
var slider = document.getElementById("slider");
var children = slider.childNodes;
for (var i = 0; i < children.length; i++) {
//assume slider's immediate children are all <img>, no <a>
if (children[i].tagName == "IMG")
mcSlides.push(children[i]);
}
}
return mcSlides;
}
var i = 1;
function display(imgSrc, idx) {
mcSlides = getSlides();
mcSlides[idx].src = "images/image-slider-"+i+".jpg";
i++;
imageSlider.displaySlide(idx, true, true);
}
</script>
</head>
<body>
<div id="sliderFrame">
<!--There must at least 2 image slide within the slider-->
<div id="slider">
<img src="images/slider-1.jpg" />
<img src="images/slider-2.jpg" />
</div>
<!--Custom navigation buttons on both sides-->
<div class="group1-Wrapper">
<a onclick="imageSlider.previous()" class="group1-Prev"></a>
<a onclick="display('images/image-slider-2.jpg',1)" class="group1-Next"></a>
</div>
</div>
</body>
</html>
Hi, This did not work for me. I modified demo 4 as the following, when I clicked on >, nothing happened. I do see the innerHTML of slider div tag change to the image I want to display, but the image is not displayed.
<!DOCTYPE html>
<html>
<head>
<title>Demo 4 - with Navigation Buttons</title>
<link href="themes/4/js-image-slider.css" rel="stylesheet" type="text/css" />
<script src="themes/4/js-image-slider.js" type="text/javascript"></script>
<link href="generic.css" rel="stylesheet" type="text/css" />
<script>
var idx = 1;
function display(){
var slider = document.getElementById("slider");
var children = slider.childNodes;
var slides = [];
for (var i = 0; i < children.length; i++) {
slides.push(children[i]);
}
imgSrc = "images/image-slider-"+idx+".JPG";
//alert(imgSrc);
slides[0].src = imgSrc;
//alert(slider.innerHTML);
imageSlider.displaySlide(0, true, true);
idx++;
}
</script>
</head>
<body>
<div id="sliderFrame">
<div id="slider"><img src="images/slider-1.jpg"/></div>
<!--Custom navigation buttons on both sides-->
<div class="group1-Wrapper">
<a onclick="imageSlider.previous()" class="group1-Prev"></a>
<a onclick="display()" class="group1-Next"></a>
</div>
</div>
</body>
</html>
If your question is related to the topic of this post, you can post your question to this page by clicking the "Post a reply" button at left;
If your question is related to the Javascript Image Slider, click:
Ask new question: Javascript Image SliderOtherwise navigate to one of the following forum categories, and post your question there.
##