简体   繁体   中英

Lightbox Effect JS Help Please

I have the following code:

<script type="text/javascript" >

$(document).ready(function(){
$(".img").click(function(){
var address= $(this).attr("src");
$("#popup").fadeIn("slow");
$("#lightbox").attr("src",address);
});
$("#close").click(function(){
$("#popup").fadeOut("slow");
});
});
    </script>

Apparently in the string $("#lightbox").attr("src",address); the "src",address is a variable that should point the script to display the image associated with the source address in the lightbox popup. However it is not working for me. I thought that it could be that I have an overlay over the image thumbnails, however as you can see from the code below the overlay resides inside of the image class div. Any suggestions?

<div id="indexcontainer">

    <div class="col col-1">
    <div id="effect-1" class="effects clearfix">

<div class="img"><img src="images/photos/_DSC0048.jpg" alt="Symphony In Red -       robertlunaphotography.com" onload="MM_effectGrowShrink(this, 600, '0%', '100%', false, false, true)">
    <div class="overlay">
<h6>&nbsp;</h6>
<h6>&nbsp;</h6>
<h6>&nbsp;</h6>
    <h4>"Symphony in Red"</h4>
    </div>
</div>

    </div>

    <div id="effect-1" class="effects clearfix">

<div class="img"><img src="images/photos/_DSC0046alt.jpg" alt="&quot;Second Thoughts&quot;"  longdesc="robertlunaphotography.com">
    <div class="overlay">
<h6>&nbsp;</h6>
<h6>&nbsp;</h6>
<h6>&nbsp;</h6>
    <h4>"Second Thoughts'</h4>
    </div>
</div>
</div>
       <div id="effect-1" class="effects clearfix">

<div class="img"><img src="images/photos/LNA_4072.jpg" width="375" height="250" alt="&quot;The Angel of Oaks&quot;" longdesc="robertlunaphotography.com" onload="MM_effectGrowShrink(this, 400, '0%', '100%', false, false, true)"></a>
    <div class="overlay">
<h6>&nbsp;</h6>
<h6>&nbsp;</h6>
<h6>&nbsp;</h6>
    <h4>"The Angel of Oaks"</h4>
  </div>
</div>
</div>
              <div id="effect-1" class="effects clearfix">

<div class="img"><img src="images/photos/LUN_0378.jpg" width="514" height="250" alt="&quot;Table Rock Mountain&quot;" longdesc="robertlunaphotography.com">
    <div class="overlay">
<h6>&nbsp;</h6>
<h6>&nbsp;</h6>
<h6>&nbsp;</h6>
    <h4>"Table Rock Mountain"</h4>
  </div>
</div>
</div>
               <div id="effect-1" class="effects clearfix">

<div class="img"><img src="images/photos/_DSC0092_sm.jpg" width="376" height="250" alt="&quot;The  Stranger&quot;" longdesc="robertlunaphotography.com" onload="MM_effectGrowShrink(this, 600, '0%', '100%', false, false, true)">
    <div class="overlay">
<h6>&nbsp;</h6>
<h6>&nbsp;</h6>
<h6>&nbsp;</h6>
    <h4>"The Stranger"</h4>
  </div>
</div>
</div>
                  <div id="effect-1" class="effects clearfix">

<div class="img"><img src="images/photos/_DSC0073.jpg" width="250" height="376" alt="&quot;Jessica&quot;" longdesc="robertlunaphotography.com">
    <div class="overlay">
<h6>&nbsp;</h6>
<h6>&nbsp;</h6>
<h6>&nbsp;</h6>
    <h4>"Jessica"</h4>
  </div>
</div>
</div>
                     <div id="effect-1" class="effects clearfix">

<div class="img"><img src="images/photos/LNA_5203.jpg" width="375" height="250" alt="&quot;Lake Junaluska&quot;" longdesc="robertlunaphotography.com" onload="MM_effectGrowShrink(this, 600, '0%', '100%', false, false, true)">
    <div class="overlay">
<h6>&nbsp;</h6>
<h6>&nbsp;</h6>
<h6>&nbsp;</h6>
    <h4>"Lake Junaluska"</h4>
  </div>
</div>
</div>
                        <div id="effect-1" class="effects clearfix">

<div class="img"><img src="images/photos/_DSC0204copy.jpg" width="250" height="334"  alt="&quot;Brooke&quot;" longdesc="robertlunaphotography.com">
    <div class="overlay">
<h6>&nbsp;</h6>
<h6>&nbsp;</h6>
<h6>&nbsp;</h6>
    <h4>"Brooke"</h4>
</div>
</div>
</div>
</div>
</div>
<div id="popup">
<div id="center">
    <img id="lightbox" src="images/largeones/Symphony-in-Red-Robert-Luna-Photography.com.jpg">
    <img id="close" src="images/index/Close-button-icon-1005031249.png" alt="close" >
</div>  

Your click handler is defined on the <div> element whose class is img . Thus, you're trying to get the src attribute from the <div> element instead of the <img> element:

$(".img").click(function(){
    // In this function, $(this) refers to <div class="img"> (not <img src="...">)
}

If you really need the click handler to be on the <div> element, try this:

$(".img").click(function(){
    var address= $(this).children().attr("src");
}

If instead you want the click handler on the <img> element, try this:

$(".img img").click(function(){
    var address= $(this).attr("src");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM