加载动态图像名称时灯箱问题

use*_*410 5 javascript php

好吧,我有一个灯箱,除1个问题外,效果很好.图像是动态构建的,它获得了10个图像的列表,并通过每个图像显示一行上的每个图像.所以我可以看出出了什么问题.无论我选择哪个图像显示灯箱中的第一个图像,所以我需要传递一个带有图像路径或图像名称的变量.

我真的没有那么多JavaScript的经验,但什么即时希望做的就是把$popup_item->attributes()->name成一个变量,通过它传递onclick的事件,然后里面divid="light"的,而不是通过$popup_item->attributes()->name我传递变量,但不知道这是最好的办法,甚至在哪里开始

有一个像这样的循环循环并打印弹出容器很多次:

foreach($xml->config->popup as $popup_item){

}
Run Code Online (Sandbox Code Playgroud)

和HTML

<div id="popup_container">
    <!-- We use a lightbox to show the image in full size since large popups are scaled -->
    <a href = "javascript:void(0)" onclick = "document.getElementById('light').style.display='block';document.getElementById('fade').style.display='block'">
        <!-- This is the scaled image -->
        <!--popups are stored in images/popups/images/ in the following format -->
        <!--id_popupname.png which we build dynamically below because -->
        <!-- the popup name will always be same name as the popupimage with the user id preceeding it -->
        <img src="images/popups/images/<?php echo $item_id . "_" .  strtolower($popup_item->attributes()->name) . ".png" ;?>" alt="Popup Image"/>
    </a>

    <!--This holds the un-scaled image in the popup box which is hidden initially until you click the image-->
    <div id="light" class="white_content"> 
        <img src="images/popups/images/<?php echo $item_id . "_" .  strtolower($popup_item->attributes()->name) . ".png" ;?>" alt="Popup Image"/></a>
        <!--This allows you to close the lightbox window from within the lightbox window-->
        <a href = "javascript:void(0)" onclick = "document.getElementById('light').style.display='none';document.getElementById('fade').style.display='none'">Close</a>
    </div>
    <div id="fade" class="black_overlay"></div>
</div> <!--end of popup container-->
Run Code Online (Sandbox Code Playgroud)

灯箱css以防万一它有助于:

.black_overlay{
    display: none;
    position: fixed;
    top: 0%;
    left: 0%;
    width: 100%;
    height: 100%;
    background-color: black;
    z-index:1001;
    -moz-opacity: 0.8;
    opacity:.80;
    filter: alpha(opacity=60);
}

.white_content {
    display: none;
    position: fixed;
    top: 25%;
    left: 25%;
    width: 50%;
    height: 50%;
    padding: 16px;
    border: 16px solid orange;
    background-color: white;
    z-index:1002;
    overflow: auto;
}
Run Code Online (Sandbox Code Playgroud)

编辑:其实我需要通过2个变量中,$item_id$popup_item->attributes()->name,但概念是相同的

Gar*_*ing 6

你每次都得到相同的图像因为DOM正在选择它找到的第一个元素,其id为'light'.ID在HTML中应该是唯一的.相反,尝试这个......

<div id="popup_container">
    <a href = "javascript:void(0)" onclick = "document.getElementById('light_<?php echo $item_id ?>').style.display='block';document.getElementById('fade').style.display='block'">
        <img src="images/popups/images/<?php echo $item_id . "_" .  strtolower($popup_item->attributes()->name) . ".png" ;?>" alt="Popup Image"/>
    </a>

    <div id="light_<?php echo $item_id ?>" class="white_content"> 
        <img src="images/popups/images/<?php echo $item_id . "_" .  strtolower($popup_item->attributes()->name) . ".png" ;?>" alt="Popup Image"/></a>
        <a href = "javascript:void(0)" onclick = "document.getElementById('light_<?php echo $item_id ?>').style.display='none';document.getElementById('fade').style.display='none'">Close</a>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

另外,将淡入淡出div移到循环外部.你只需要一个实例,而不是10 ...

因此,如果你在原始PHP中构建它而不是使用模板引擎,它看起来像......

echo '<div id="popup_container">';

foreach($xml->config->popup as $popup_item){
    echo '<a href = "javascript:void(0)" onclick = "document.getElementById(\'light_'.$popup_item->attributes()->item_id.'\').style.display=\'block\';document.getElementById(\'fade\').style.display=\'block\'">
            <img src="images/popups/images/'.$popup_item->attributes()->item_id."_".strtolower($popup_item->attributes()->name).'.png" alt="Popup Image"/>
        </a>

        <div id="light_'.$popup_item->attributes()->item_id.'" class="white_content"> 
            <img src="images/popups/images/'.$popup_item->attributes()->item_id."_".strtolower($popup_item->attributes()->name).'.png" alt="Popup Image"/></a>
            <a href = "javascript:void(0)" onclick = "document.getElementById(\'light_'.$popup_item->attributes()->item_id.'\').style.display=\'none\';document.getElementById(\'fade\').style.display=\'none\'">Close</a>
        </div>';
}

echo '</div>';
echo '<div id="fade" class="black_overlay"></div>';
Run Code Online (Sandbox Code Playgroud)

编辑:我会看下面的一些其他答案.其中一些提供了一种更好的方法来实现这种效果,但是,我的回答是关于手头的问题,如何使原始代码工作.