mchaplin897
07-15-2012, 03:23 AM
Hi there,
I'm trying to use javascript to create a very basic photo portfolio. No fancy light boxes or effects, just thumbnails and a "next" and "prev" button to navigate them. Right now I'm just working with four images until I figure this out. I want it to just do a basic rotation of the images, where the first page loads one image and then clicking "next" swaps it out for the next image and so on and so forth. Clicking "prev" would load the previous image.
I stole the below code from a friend of mine who did something similar but it isn't working for me and I can figure out why. The first image will appear properly, but then clicking "next" displays nothing, until the fourth click where it reloads the original image. I wan't it to load the next image every time I click "next". I think somehow the other images are being prevented from displaying but I'm not sure how. Any ideas would be very much appreciated, I've been staring at this for days now and can't figure it out.
Here is my code
In the header
-----------------
<script>
var currentId ="img1";
$(document).ready(function(){
$(document).ready(function(){
$("a#prev_link").click(function(e){
var previousId = $("#" + currentId).attr("prev");
$("#" + currentId).hide();
$("#" + previousId).show();
currentId = previousId;
e.preventDefault();
});
$("a#next_link").click(function(e){
var nextId=$("#" + currentId).attr("next");
$("#" + currentId).hide();
$("#" + nextId).show();
currentId = nextId;
e.preventDefault();
});
});
});
</script>
In the body
-----------------
<div id="photo_body">
<ul id="image_navigation">
<li class="image_navigation"><a id="prev_link" href="#">prev</a></li>
<li class="image_navigation"><a id="main" href="main"> index </a></li>
<li class="image_navigation"><a id="next_link" href="#">next</a></li>
</ul>
<div id="img1" next="img2" prev="img4" style="display: block"><img class="portfolio" src="images/bradlees.jpg">
<div id="img2" next="img3" prev="img1" style="display: none"><img class="portfolio" src="images/blue_white_building.jpg"/></div>
<div id="img3" next="img4" prev="img2" style="display: none"><img class="portfolio" src="images/perkins.jpg"/></div>
<div id="img4" next="img1" prev="img3" style="display: none"><img class="portfolio" src="images/pizza_hut.jpg"/></div>
</div>
I'm trying to use javascript to create a very basic photo portfolio. No fancy light boxes or effects, just thumbnails and a "next" and "prev" button to navigate them. Right now I'm just working with four images until I figure this out. I want it to just do a basic rotation of the images, where the first page loads one image and then clicking "next" swaps it out for the next image and so on and so forth. Clicking "prev" would load the previous image.
I stole the below code from a friend of mine who did something similar but it isn't working for me and I can figure out why. The first image will appear properly, but then clicking "next" displays nothing, until the fourth click where it reloads the original image. I wan't it to load the next image every time I click "next". I think somehow the other images are being prevented from displaying but I'm not sure how. Any ideas would be very much appreciated, I've been staring at this for days now and can't figure it out.
Here is my code
In the header
-----------------
<script>
var currentId ="img1";
$(document).ready(function(){
$(document).ready(function(){
$("a#prev_link").click(function(e){
var previousId = $("#" + currentId).attr("prev");
$("#" + currentId).hide();
$("#" + previousId).show();
currentId = previousId;
e.preventDefault();
});
$("a#next_link").click(function(e){
var nextId=$("#" + currentId).attr("next");
$("#" + currentId).hide();
$("#" + nextId).show();
currentId = nextId;
e.preventDefault();
});
});
});
</script>
In the body
-----------------
<div id="photo_body">
<ul id="image_navigation">
<li class="image_navigation"><a id="prev_link" href="#">prev</a></li>
<li class="image_navigation"><a id="main" href="main"> index </a></li>
<li class="image_navigation"><a id="next_link" href="#">next</a></li>
</ul>
<div id="img1" next="img2" prev="img4" style="display: block"><img class="portfolio" src="images/bradlees.jpg">
<div id="img2" next="img3" prev="img1" style="display: none"><img class="portfolio" src="images/blue_white_building.jpg"/></div>
<div id="img3" next="img4" prev="img2" style="display: none"><img class="portfolio" src="images/perkins.jpg"/></div>
<div id="img4" next="img1" prev="img3" style="display: none"><img class="portfolio" src="images/pizza_hut.jpg"/></div>
</div>