
Originally Posted by
markbrown4
Hi jazzo, welcome to Sitepoint
Your changeImage function get's
all of the big images, changes their src attributes then toggles the classes.
This is probably an easier way to achieve what you're wanting.
Hi thank you : - )
Thanks for posting your code, but I am still at the beginning of my jquery career, so I might need a little explanation if you don't mind, and also I have attempted another way, which I am not sure whether it is correct or not. ANyway, one thing at a time, let's start by looking at your code.
Here
Code:
$('#thumbs').on('click', 'a', function(event)
you select the thumbs div and the On method attaches a click event to thumbs a (which is the thumbnail image) and run a function.
Code:
$('#image').attr('src', this.href);
here we grab the default image and we change the src property ('this' refers to #thumbs here and then we attach .href therefore 'this.href' refers to #thumbs with an attribute of href, which is the big image.)
Code:
$('#caption').html($(this).data('caption'));
here we grab the caption id and I am not sure what happens there...sorry
The return false is to stop the link from being followed by the browser
Just after I posted this last night, I have attempted a different solution. I think the idea is good, but unfortunately it doesn't work...
the html is similar although I am calling the function from within the html in onclick="changeImage()":
Code:
<div>
<p>This paragraph is the default</p>
<p class="hide_paragraph">This paragraph comes in with image 1</p>
<p class="hide_paragraph">This paragraph comes in with image 2</p>
<p class="hide_paragraph">This paragraph comes in with image 3</p>
</div>
<div class = "big_image" id="big_image">
<img src="placeholder.jpg" alt="" id="placeholder_image" >
</div>
<div class="thumb_box">
<ul class="thumb_images">
<li><a href="#"><img src="test1_thumb.jpg" alt="" onclick="changeImage()">Thumb1</a></li>
<li><a href="#"><img src="test2_thumb.jpg" alt="" onclick="changeImage()">Thumb2</a></li>
<li><a href="#"><img src="test3_thumb.jpg" alt="" onclick="changeImage()">Thumb3</a></li>
</ul>
</div>
JAVASCRIPT
Code:
var pic1 = new Image();
pic1.src = "test1.jpg";
var pic2 = new Image();
pic2.src = "test2.jpg";
var pic3 = new Image();
pic3.src = "test3.jpg";
function changeImage(){
var theImage = document.getElementById("placeholder_image");
theImage.src = pic1.src;
}
while I was coding this last night, I found it somewhat easier than coding it in javascript, although the code is longer. I basically preloaded the images and then the function gets the default image and change its src. Thing is with the code above I can only change the default image, nothing else. If I want to change the other images then I can't, and I am not sure how to ptimize the code to achieve that. I think knowing both ways (jquery and javascript) will help me to understand a bit more the technicalities, that's why I posted also the javascript code, I can do it in another post if you think it is better
Bookmarks