I looked up this guide on youtube on how to create a simple slideshow using javascript and it worked flawlessly. However I wanted each image to link to a specific href for the user to navigate.
<div id="slider_news">
<a href="news1.html" id="slider_link"><imglink><img src="images/img1.jpg" id="image"></imglink></a>
<div class="left_hold"><img onClick="img(-1)" class="left" src="images/arrow_left.png"></div>
<div class="right_hold"><img onClick="img(1)" class="right" src="images/arrow_right.png"></div
</div>
This is my HTML, I created a link around the image using the "a" tag and gave it an ID(="slider_link")
var imageCount = 1;
var total = 5;
function img(x) {
var image = document.getElementById('image');
imageCount = imageCount + x;
if(imageCount > total){imageCount = 1;}
if(imageCount < 1){imageCount = total;}
image.src = "images/img"+ imageCount +".jpg";
var sliderlink = document.getElementById("slider_link");
if(imageCount = 2){sliderlink.href = "news2.html";} //img2.jpg link
else if(imageCount = 3){sliderlink.href = "news3.html";} //img3.jpg link
else if(imageCount = 4){sliderlink.href = "news4.html";} //img4.jpg link
else if(imageCount = 5){sliderlink.href = "news5.html";} //img5.jpg link
}
window.setInterval(function imgA() {
var image = document.getElementById('image');
imageCount = imageCount + 1;
if(imageCount > total){imageCount = 1;}
if(imageCount < 1){imageCount = total;}
image.src = "images/img"+ imageCount +".jpg";
var sliderlink = document.getElementById("slider_link");
if(imageCount = 2){sliderlink.href = "news2.html";} //img2.jpg link
else if(imageCount = 3){sliderlink.href = "news3.html";} //img3.jpg link
else if(imageCount = 4){sliderlink.href = "news4.html";} //img4.jpg link
else if(imageCount = 5){sliderlink.href = "news5.html";} //img5.jpg link
},5000);
This is my javascript code, I make a variable out of the a tag using it's ID, then just add 4 else if statements and add the correct page for the correct image number.I basicly added everything starting from "var sliderlink" to the previously working slider.
The problem I'm having is that the html only changes once through the loop and stays on that page forever (news2.html). And once I click on the image and get directed to the page the whole slide starts acting wierd, not following the image order etc.
I'm sorry if the code seems a bit clunky and unorganized but hopefully you can understand what I'm doing wrong.