Reputation: 21
I am sorry, this a newbie question. I want to display random pictures taken from a directory of my server and I want to display a text associated to each image (txt files in the same directory. Example: 1.jpg -> 1.txt; 2.jpg -> 2.txt...). I want that the text appear above the image, in a frame placed on the top of the image, only upon mouse hover on the image.
The javascript code, css and html codes can be found here:
http://jsfiddle.net/Totoleheros/ES22a/
html code:
<img id="fullSize" onload="fixImage(this)" />
<div id="HOLDER">
<div id="theCaption"></div>
</div>
<img id="showImage" alt="random image" />
CSS code:
/* The first style is to hide the text*/
#theCaption {
position: absolute;
width: 200px;
height: 331px;
filter:alpha(opacity=0);
-moz-opacity:0;
-khtml-opacity: 0;
opacity: 0;
z-index: 3;
}
/*The send style is to show the text on hover*/
#theCaption:hover, #theCaption:active {
position: absolute;
width: 200px;
height: 40px;
background-color: #eeeeee;
color: #000000;
overflow: hidden;
font-family: arial;
font-weight: normal;
filter:alpha(opacity=80);
-moz-opacity:0.8;
-khtml-opacity: 0.8;
opacity: 0.8;
z-index: 3;
font-size: 10px;
line-height: 0.9em;
padding-top: 2px;
padding-right: 2px;
padding-bottom: 1px;
padding-left: 2px;
}
javascript code:
function fixImage(image) {
// I want an to display an image of 200x331px taken from a directory of image of various dimensions
var show = document.getElementById("showImage");
if (image.height > image.width) {
show.style.height = "331px";
show.style.width = Math.round((image.width / image.height) * 331) + "px";
} else {
show.style.width = "200px";
show.style.height = Math.round((image.height / image.width) * 200) + "px";
}
show.src = image.src;
show.style.visibility = "visible";
}
var MAXPICTURENUMBER = 166; // or whatever you choose
var rn = 1 + Math.floor(MAXPICTURENUMBER * Math.random());
// Here I point to the directory containing the images and the caption
var url = "http://www.test.fr/Images/RandomPictures/" + rn + ".jpg";
var caption = "http://www.test.fr/Images/RandomPictures/" + rn + ".txt";
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET", caption, false);
xmlhttp.send();
captionText = xmlhttp.responseText;
window.onload = function () {
document.getElementById("theCaption").innerHTML = captionText;
document.getElementById("fullSize").src = url;
}
I am sure that there is a much cleaner/smarter solution than the one I am using, but remember, I'm a newbie.
My issue: this is almost working except that the text sometimes disappear according to the mouse position. How can I fix that?
Thkx in advance for your help!!
Upvotes: 0
Views: 1040
Reputation: 159
I would put the image and the caption in the same container. Something like
<div class="container">
<img class="image" src="image.png" />
<div class="caption">The caption</div>
</div>
and in css
.caption{position:relative; top:-20px; height:20px; display:none;}
.container:hover .caption{display:block;}
you can obviously style however.
EDIT: here's the final fiddle: http://jsfiddle.net/ES22a/5 part of the problem was a jQuery conflict so there's also a jQuery.noConflict();
in the head.
Upvotes: 1