javascript - Photogallery previous button not functioning right -
i'm playing around learning javascript , trying make simple photo gallery. next button have works it's suppose advancing photo 1 @ time, when hit previous goes way first image instead of 1 image , can't quite figure out why. help.
it's in jsbin here: http://jsbin.com/unijet/2/edit
my code is:
html:
<div id="navcontainer"> <span id="prev"><a href="" onclick="return prev_img();">prev</a></span> <span id="pg"></span> <span id="next"><a href="" onclick="return next_img();">next</a></span> </div> <div id="imgcontainer"><img src="" id="img"/></div> js:
var prev = document.getelementbyid("prev"); var next = document.getelementbyid("next"); var imge = document.getelementbyid("img"); var page = document.getelementbyid("pg"); var imag = [ "http://images.nationalgeographic.com/wpf/media-live/photos/000/667/cache/red-fox-manitoba_66703_990x742.jpg", "http://images.nationalgeographic.com/wpf/media-live/photos/000/667/cache/martial-arts-india_66701_990x742.jpg?01ad=3ha8vz6-_sylzhh6qoaapo6cxs3ylip8x8qywogguawbdsuq9py9ljw&01ri=20fa22155e4e381&01na=na", "http://images.nationalgeographic.com/wpf/media-live/photos/000/666/cache/egret-reflection-florida_66697_990x742.jpg", "http://images.nationalgeographic.com/wpf/media-live/photos/000/667/cache/tornado-storm-saskatchewan_66707_990x742.jpg", "http://images.nationalgeographic.com/wpf/media-live/photos/000/667/cache/penguin-south-georgia-island_66702_990x742.jpg", "http://images.nationalgeographic.com/wpf/media-live/photos/000/667/cache/sunset-south-africa_66706_990x742.jpg", "http://images.nationalgeographic.com/wpf/media-live/photos/000/667/cache/man-motorbike-india_66710_990x742.jpg" ]; var = 0; imge.setattribute("src",imag[i]); prev.innerhtml = "prev"; page.innerhtml = i; function next_img() { i++; page.innerhtml = i; imge.setattribute("src",imag[i]); if (i == imag.length-1) { next.innerhtml = "next"; prev.innerhtml = "<a href='' onclick='return prev_img();'>prev</a>"; } else { next.innerhtml = "<a href='' onclick='return next_img();'>next</a>"; prev.innerhtml = "<a href='' onclick='return prev_img();'>prev</a>"; } return false; } function prev_img() { i--; page.innerhtml = i; image.setattribute("src",imag[i]); if (i === 0) { prev.innerhtml = "prev"; next.innerhtml = "<a href='' onclick='return next_img();'>next</a>"; } else { prev.innerhtml = "<a href='' onclick='return prev_img();'>prev</a>"; next.innerhtml = "<a href='' onclick='return next_img();'>next</a>"; } return false; }
in prev_img() function change
image.setattribute("src",imag[i]); to
imge.setattribute("src",imag[i]);
Comments
Post a Comment