-1

I am new to web development but highly fascinated by it. So, basically I am creating a light-box where thumbnails of images will be appear on screen and they will appear bigger in size when user clicks over them. Now, I want when user hovers over the gallery images/thumbnails then some text should appear over the current image with may be some animation or basically mouser-hover should cause some event to happen but I am unable to do it. Text should be added dynamically or may be previously stored in an array or something of that sort. Please have a look at my code and tell me how to modify it in order to achieve such effect and if you know a better and easier way to do so then feel free to share. Thank you so much!!

HTML:

<div class="gallery">
    <ul id="images"></ul>
    <div class="lightbox">
        <div class='limage'>
        </div>
        <div class='left'>
        </div>
        <div class='right'>
        </div>
        <div class='close'>
            x
        </div>
    </div>
</div>

JAVASCRIPT:

var gallery_slider = new Array();
gallery_slider[0] = "im1.jpg";
gallery_slider[1] = "im2.jpg";
gallery_slider[2] = "im3.jpg";


function displayAllImages() {
    var i = 0,
        len = gallery_slider.length;
    for (; i < gallery_slider.length; i++) {

        var img = new Image();
        img.src = gallery_slider[i];
        img.style.width = '200px';
        img.style.height = '120px';
        img.style.margin = '3px';
        img.style.cursor = 'pointer';

        document.getElementById('images').appendChild(img);
    }
};


$(function() {
    displayAllImages();
});



$(function() {

    $('img').click(function() {
        var hell = (this).src;
        display(hell);
    });
});

function display(hello) {
    $('header').css('display', 'none'); /*for some other purposes*/
    $('.limage').html("<img src=" + hello + " >");
    $('.lightbox').css("display", "block");
    $('.lightbox').fadeIn();

    $('.right').click(function() {
        var im = new Array();
        var x;
        var p;
        for (x = 0; x < gallery_slider.length; x++) {
            im[x] = gallery_slider[x];
        }
        for (p = 0; p < im.length; p++) {
            if (im[p] == hello) {
                break;
            } else {
                continue;
            }
        }
        if (p >= (im.length - 1)) {
            p = -1;
        }

        $('.limage').fadeOut(0);
        $('.limage').html("<img src= " + im[p + 1] + ">");
        $('.limage').fadeIn(500);
        hello = im[p + 1];
    });
    $('.left').click(function() {
        var im = new Array();
        var x;
        var p;
        for (x = 0; x < gallery_slider.length; x++) {
            im[x] = gallery_slider[x];
        }
        for (p = 0; p < im.length; p++) {
            if (im[p] == hello) {
                break;
            } else {
                continue;
            }
        }
        if (p == 0) {
            p = (im.length);
        }

        $('.limage').fadeOut(0);
        $('.limage').html("<img src= " + im[p - 1] + ">");
        $('.limage').fadeIn(500);
        hello = im[p - 1];
    });
    $('.close').click(function() {
        $('.lightbox').fadeOut();
        $('header').css('display', 'block'); /*for some other purposes*/
    });
};

CSS:

.gallery {
    width: 100%;
    height: 400px;
    overflow: hidden;
    margin: auto;
}
.gallery ul {
    list-style: none;
}
.lightbox {
    background-color: rgba(0, 0, 0, 0.3);
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: none;
    z-index: 106;
}
.close {
    color: #fff;
    border: 1px solid #fff;
    border-radius: 100px;
    background-color: #000;
    position: absolute;
    top: 10px;
    right: 20px;
    padding: 10px;
    font-family: firstfont;
    font-size: 30px;
    z-index: 101;
    cursor: pointer;
}
.close:hover {
    background-color: #ebebeb;
    color: #000;
}
.left {
    width: 50%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
    cursor: pointer;
}
.right {
    width: 50%;
    height: 100%;
    position: absolute;
    top: 0;
    right: 0;
    cursor: pointer;
}
.limage {
    position: relative;
    margin: auto;
    top: 17%;
    left: 15%;
    max-width: 90%;
    max-height: 90%;
}

There might be some bugs in coding. Watch out. This code is working for displaying images as thumbnails as a matrix and as slider in lightbox when clicked upon them. I am not able to figure out how to add hover functionality to initial thumbnails.

Jsfiddle : http://jsfiddle.net/psd6cbd7/1/

Yomesh
  • 288
  • 1
  • 4
  • 19
  • Unfortunately your question is too broad for SO. You'll not get too many helpful responses simply asking "make it work". Instead, try to get it working yourself, then come back with a *very* specific question if you get stuck and cannot find the answer to your specific question anywhere else online (hint: you probably can). As an aside, I see you are sometimes using JQuery and sometimes not (i.e. displayAllImages), I would suggest sticking with either one or the other for a single project. – oliakaoil Oct 28 '14 at 18:55
  • I tried and still trying. I just want may be someone will try to run this code and will revert with the answer or at least will tell me that is it a decent way to do the required task or not. This code is working for displaying images as thumbnails and as slider in lightbox when clicked upon them. I am not able to figure out how to add hover functionality to initial thumbnails. P.S. displayAllImages is a user created function in jquery or Am I doing something wrong? – Yomesh Oct 28 '14 at 19:09

2 Answers2

0

I'd suggest putting a div inside the image div containing the text and then using CSS to hide/show it.

HTML:

    <div class="gallery">
        <ul id="images"></ul>
        <div class="lightbox">
            <div class='limage'>
                <div class=".caption">Caption here</div>
            </div>
            <div class='left'>
            </div>
            <div class='right'>
            </div>
            <div class='close'>
                x
            </div>
        </div>
    </div>

CSS:

.limage { position: relative; }
.caption { display: none; }
.limage:hover .caption { display: block; position: absolute;}
arcyqwerty
  • 10,325
  • 4
  • 47
  • 84
  • Thanks but this is not working as images are stored in an array and are appended to the ul tag via function so if we creating a div inside the image then same caption might appear on every image but every image should have a different caption. Moreover, I tried it still no text appears on hover. – Yomesh Oct 28 '14 at 19:04
  • You can change the content of the caption div with Javascript using the `.innerText` property. Can you make a jsfiddle.net with your code? – arcyqwerty Oct 28 '14 at 20:30
0

Why you using array to store the images? Anyways, assume that you still using array, below is some example code that you want try:

HTML:

<ul id="images">
</ul>
<!-- assume this is the place that you want to display the caption -->
<div id="caption"></div>

Javascript:

var images = new Array();
images[0] = "p1.png";
images[1] = "p2.png";
images[2] = "p3.png";
images[3] = "p4.png";

var captions = new Array();
captions[0] = "Picture 1";
captions[1] = "Picture 2";
captions[2] = "Picture 3";
captions[3] = "Picture 4";

var x = $("#images");
var y = $("#caption");
const prefix = "image-";

if you are using HTML5:

for (var i = 0; i < images.length; i++) {
    x.append("<img class='roll' src='" + images[i] + "' data-caption='" + captions[i] + "'>");
}

$(".roll").mouseover(function(){
    //do whatever effect here when mouse over
    y.html($(this).attr("data-caption"));
});

If you want to backward compatible:

for (var i = 0; i < images.length; i++) {
    x.append("<img id='" + prefix + i + "' class='roll' src='" + images[i] + "'>");
}

$(".roll").mouseover(function(){
    //do whatever effect here when mouse over
    var index = $(this).attr("id").substring(prefix.length);
    y.html(captions[index]);
});

Hope that this will help.

Joey Chong
  • 1,470
  • 15
  • 20
  • I tried out your method ( Jsfiddle : http://jsfiddle.net/psd6cbd7/5/ ) but still mouseover is not working. Is it mouseover or onmouseover? Both are not working and I am using array because when user clicks on an image then src of that image is stored and matched against the array elements and once the position is found it is used to display the next image/element of the array as in next image in the slideshow( im[p+1] ). Slideshow works like a charm, I am just having trouble with initial thumbnails. You know any easier way to display them then please share. Thank you so much! – Yomesh Oct 29 '14 at 13:06
  • I just go through your code, move the `mouseover` code after `displayAllImages();` because when it run, the image still not there yet. Also, put the `var y = $("#caption");` out of `displayAllImages()` else code inside `mouseover` will not able to refer the variable y. – Joey Chong Oct 30 '14 at 01:38