質問

I've been looking around much today and spend a few hours trying to get something done. For a client I am creating a slideshow with a lightbox when clicked on an image. The slideshow and lightbox both work, but I don't get the right image in the lightbox yet.

This is the code that loads the slideshow and when clicked on an image opens the lightbox. (The images for the slideshow get loaded by a php script and turned into a Javascript array)

<script type="text/javascript">

var curimg=0;
function rotateimages(){
document.getElementById("slideshow").setAttribute("src", "images/"+galleryarray[curimg]);
curimg=(curimg<galleryarray.length-1)? curimg+1 : 0;
}

window.onload = function(){
setInterval("rotateimages()", 1000);
}
</script>
<div style="width: 170px; height: 160px">
<a href = "javascript:void(0)" onclick =          "document.getElementById('light').style.display='block';document.getElementById('fade').style.    display='block'">
<img id="slideshow" src="" />
</a>
<div id="light" class="white_content">
<img id="lightImg" src="" />
<script>
var image = document.getElementById("slideshow").src;
document.getElementById("lightImg").setAttribute("src", image);
</script>

I now try to create a variable named "image"and let this contain the src of the current image in the slideshow. So I can load this to the image in the lightbox.

Hopefully some one can give me some usefull tips. I am pretty new in the Javascript language.

The script for the slideshow came from: http://www.javascriptkit.com/javatutors/externalphp2.shtml

Regards Koen.

役に立ちましたか?

解決

These days there really is no excuse for using obtrusive Javascript (Stuff inside your HTML attributes, ideally it should be in an external file. http://en.wikipedia.org/wiki/Unobtrusive_JavaScript).

I have done you the favour of cleaning up your code a bit, and changed it where you seemed to be going wrong. As DotNetter has already pointed out it would be sensible to use jQuery in this instance, as it really does simplify things. However, I'm going to assume that for some reason you want it in plain js. Below is a simplification of the code that you posted with the correct change.

<style type="text/css">
    .wrapper {
        width: 170px;
        height: 160px;
    }
</style>

<script type="text/javascript">
    var curimg=0;
    function rotateimages(){
        document.getElementById("slideshow").setAttribute("src", "images/" + galleryarray[curimg]);
        curimg=(curimg<galleryarray.length-1)? curimg+1 : 0;
    }

    window.onload = function(){
        setInterval("rotateimages()", 1000);

        document.getElementById("slideshow").onclick = function () {
            var imageSrc = document.getElementById("slideshow").src;
            document.getElementById("lightImg").setAttribute("src", imageSrc);
            document.getElementById('light').style.display = 'block';
            document.getElementById('fade').style.display = 'block';
        }
    }
</script>

<div class='wrapper'>
    <img id="slideshow" src="" />
    <div id="light" class="white_content">
        <img id="lightImg" src="" />
    </div>
</div>

Before, you were getting the src of the current image when the page loaded, you need to be getting the src of the image when the user clicks on the

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top