繁体   English   中英

使用此代码创建多个弹出窗口

[英]Creating multiple popups with this code

我正在尝试学习jQuery和弹出窗口。 我通过Google发现了这段代码,效果很好。 事实是,这种设计仅允许通过此javascript创建一个弹出窗口。 我看到作者写了;

如果要创建多个弹出窗口而不为每个弹出窗口创建一个分区,则需要创建一个javascript对象,那么您将能够创建该弹出对象的多个实例。

更新:以下是我现在正在使用的代码。 我尝试了您的方式,但仍然没有喜悦。 现在,我无法以我从您的帖子中了解的方式创建和弹出窗口。 我把它带回到下面,仍然有问题。 申请帮助我。 我不明白这是很难做到的事情。

/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!                  
/***************************/


var popupStatus = 0;

function loadPopup()
{
    if(popupStatus == 0)
    {
        $("#backgroundPopup").css({
            "opacity": "0.09"
        });

        $("#backgroundPopup").fadeIn("slow");
        $("#myPopup").fadeIn("slow");
        popupStatus = 1;
    }
}

function disablePopup()
{
    if(popupStatus == 1)
    {
        $("#backgroundPopup").fadeOut("slow");
        $("#myPopup").fadeOut("slow");
        popupStatus = 0;
    }
}

//centering popup
function centerPopup()
{
    //request data for centering
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $("#myPopup").height();
    var popupWidth = $("#myPopup").width();

    $("#myPopup").css({
        "position": "absolute",
        "top": windowHeight / 2 - popupHeight / 2,
        "left": windowWidth / 2 - popupWidth / 2
    });

    $("#backgroundPopup").css({
        "height": windowHeight
    });
}

$(document).ready(function(){

    $("#displaypopup").click(function(){
        //centering with css
        centerPopup();
        //load popup
        loadPopup();
    });

    //CLOSING POPUP
    //Click the x event!
    $("#popupClose").click(function(){
        disablePopup();
    });
    //Click out event!
    $("#backgroundPopup").click(function(){
        disablePopup();
    });
    //Press Escape event!
    $(document).keypress(function(e){
        if(e.keyCode == 27 && popupStatus == 1){
            disablePopup();
        }
    });

});

您可以尝试将此功能以及更重要的popupStatus为隔离的对象,因此popupStatus可以与网页中的许多不同jQuery对象不同。

我看起来像:

function PopUP(backgroundPopup, popupContact)
{
    /***************************/
    //@Author: Adrian "yEnS" Mato Gondelle
    //@website: www.yensdesign.com
    //@email: yensamg@gmail.com
    //@license: Feel free to use it, but keep this credits please!                  
    /***************************/

    //SETTING UP OUR POPUP
    //0 means disabled; 1 means enabled;
    this.popupStatus = 0;

    //loading popup with jQuery magic!

    //notice $("#backgroundPopup") --> 
    this.loadPopup = function(){
        //loads popup only if it is disabled
        if(popupStatus == 0)
        {
            $(backgroundPopup).css({
                "opacity": "0.7"
            });
            $(backgroundPopup).fadeIn("slow");
            $(popupContact).fadeIn("slow");
            popupStatus = 1;
        }
    }

    //disabling popup with jQuery magic!
    this.disablePopup =  function (){
        //disables popup only if it is enabled
        if(popupStatus == 1)
        {
            $(backgroundPopup).fadeOut("slow");
            $(popupContact).fadeOut("slow");
            popupStatus = 0;
        }
    }

    //centering popup
    this.centerPopup = function (){
        //request data for centering
        var windowWidth = document.documentElement.clientWidth;
        var windowHeight = document.documentElement.clientHeight;
        var popupHeight = $(popupContact).height();
        var popupWidth = $(popupContact).width();
        //centering
        $(popupContact).css({
            "position": "absolute",
            "top": windowHeight / 2 - popupHeight / 2,
            "left": windowWidth / 2 - popupWidth / 2
        });
        //only need force for IE6
        $(backgroundPopup).css({
            "height": windowHeight
        });
    }
}

注意,您不能对弹出元素的ID进行硬编码 ,每个弹出窗口都需要使用不同的ID。 这就是为什么此函数中有一个参数,它也是一个对象。 因此,您将使用诸如此类的东西来创建此函数类的实例:

//Click the button event!backgroundPopupID
backgroundPopupID = "#backgroundPopup";
popupContactID = "#popupContact";
var popup1 = new PopUP(backgroundPopupID ,popupContactID);

$("#button").click(popup1.centerPopup(),popup1.loadPopup());//not sure if this works

实际上,我实际上并不经常使用JavaScript对象,因此我可能会删除popupStatus ,而不是我将一个类添加到弹出窗口中,以便检查弹出窗口是启用还是禁用。 您可以使用$(selector).hasClass(className)$(selector).addClass( className ) 您还应该使用弹出选择器对每个函数进行参数设置(就像上面示例中的此函数类一样)。

我花了几个小时来修改此JavaScript的代码,但我放弃了,寻找一种更简单的解决方案。 我选择了非常方便且易于使用的colorbox

http://www.aobaba.com/_multiplepopup/

您可以从此处下载该软件包: http : //www.aobaba.com/_multiplepopup/colorbox_multiplepopup.zip

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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