I have set up a gallery like this one by W3Schools but with jquery instead of javascript as shown here.
I have a problem with a modification, maybe someone can help: I added mousover titles using the images alt tag to the thumbnails using the following code:
//show alt as title on hover
var alt;
var $imgElem = $('.column img');
$imgElem.each(function(){
alt = $(this).attr('alt');
$(this).parent().append('<h2>' + alt + '</h2>');
});
which works fine with the css
.column {
position:relative;
}
.column h2 {
position:absolute;
top:0;left:0;
width:420px;
background:#EDE9D6;
font-size:38px;
line-height:90px;
padding:0 30px;
box-sizing:border-box;
opacity:0;
cursor:pointer;
}
.column:hover h2 {
opacity:1;
}
Now the problem is that the title, when visible, doesn't have the onclick function for changing the expanded images. Does anybody have an idea how to add that function to the h2 headlines? I tried the following, but it didn't work (image is always the same):
$(".column h2").click(function() {
console.clear();
var newclass = $(".column img").attr("src");
console.log(newclass);
var oldclass = $("#expandedImg").attr("id");
console.log(oldclass);
$(".stage").show();
$("#expandedImg").attr('src', newclass).hide().fadeIn("slow");
//set new source and hide element in order to be able to fade it in again
});
Here's the html:
<div class="stage">
<div id="imgtext"></div>
<span class="closebtn">×</span>
<img id="expandedImg" />
</div>
<div id="column1" class="column">
<img src="train.jpg" alt="train" title="train" />
</div>
<div id="column2" class="column">
<img src="bike.jpg" alt="bike" title="bike" />
</div>
<div id="column3" class="column">
<img src="cake.jpg" alt="cake" title="cake" />
</div>
Thanks for any help and ideas.