0

如何使用 FancyBox 或 JQuery 在一个页面上创建多个使用相同类(无 ID)的弹出式 DIV?例如,我在一个页面上有 50 个产品,它们都有相同的类名。

<div class="popup_box"> <!-- OUR PopupBox DIV-->
    <h1>This IS A Cool PopUp 1</h1>
    <div class="closePopup">Close</div> 
</div>
<div class="overlay_box"></div>
<div class="menu_item_container"> <!-- Main Page -->
    <h1 class="container_header">Sample 1</h1>
</div>

<div class="popup_box"> <!-- OUR PopupBox DIV-->
    <h1>This IS A Cool PopUp 2</h1>
    <div class="closePopup">Close</div> 
</div>
<div class="overlay_box"></div>
<div class="menu_item_container"> <!-- Main Page -->
    <h1 class="container_header">Sample 2</h1>
</div>
<div class="popup_box"> <!-- OUR PopupBox DIV-->
    <h1>This IS A Cool PopUp 3</h1>
    <div class="closePopup">Close</div> 
</div>
<div class="overlay_box"></div>
<div class="menu_item_container"> <!-- Main Page -->
    <h1 class="container_header">Sample 3</h1>
</div>

<script src="http://jqueryjs.googlecode.com/files/jquery-1.2.6.min.js" type="text/javascript"></script>

<script type="text/javascript">

    $(document).ready( function() {

        // When site loaded, load the Popupbox First
        $('.container_header').click( function() {          
            loadPopupBox();
        });

    $('.closePopup').click( function(){

            unloadPop()

        });



    });

    function loadPopupBox() {   // To Load the Popupbox
            $('.popup_box').fadeIn("slow");
            $('.overlay_box ').fadeIn("slow");      
        }

        function unloadPop(){

            $('.popup_box').fadeOut("slow");
            $('.overlay_box ').fadeOut("slow");
            }


</script>

CSS

<style type="text/css">
/* popup_box DIV-Styles*/
.overlay_box{
    display:none;
    position:fixed;  
    _position:absolute; /* hack for internet explorer 6*/  
    height:100%;  
    width:100%;  
    top:0;  
    left:0;  
    background:#000000;  
    border:1px solid #cecece;  
    z-index:1; 


    }
.popup_box { 
    display:none; /* Hide the DIV */
    position:fixed;  
    _position:absolute; /* hack for internet explorer 6 */  
    height:300px;  
    width:600px;  
    background:#FFFFFF;  
    left: 300px;
    top: 150px;
    z-index:100; /* Layering ( on-top of others), if you have lots of layers: I just maximized, you can change it yourself */
    margin-left: 15px;  

    /* additional features, can be omitted */
    border:2px solid #ff0000;   
    padding:15px;  
    font-size:15px;  
    -moz-box-shadow: 0 0 5px #ff0000;
    -webkit-box-shadow: 0 0 5px #ff0000;
    box-shadow: 0 0 5px #ff0000;

}

.menu_item_container{
    background: #d2d2d2; /*Sample*/
    width:100%;
    height:100%;
}

h1.container_header{  
cursor: pointer;  
} 

/* This is for the positioning of the Close Link */
.closePopup {
    font-size:20px;  
    line-height:15px;  
    right:5px;  
    top:5px;  
    position:absolute;  
    color:#6fa5e2;  
    font-weight:500;    
}
</style>

它应该像这个例子一样工作(请单击产品以测试弹出窗口):

http://www.grubhub.com/order.jsp?custId=263838&cityId=5&orderId=18429055&searchable=true&deliverable=true&verified=false&poiSearchTerm=null

4

1 回答 1

1

你应该这样做:

$('.container_header').click( function() {          
    $(this).parent().prev().prev('.popup_box').fadeIn("slow");
});

$('.closePopup').click( function(){
    $('.popup_box').fadeOut("slow");
    $('.overlay_box ').fadeOut("slow");
});

观看演示(仅限结果)

在此处查看代码

于 2012-08-10T08:59:14.137 回答