Javascript 使用此代码创建多个弹出窗口
我正在努力学习jQuery和弹出窗口。我通过谷歌找到了这段代码,它运行得很好。 问题是,这种设计只允许通过javascript创建一个弹出窗口。我看到作者写的 如果要创建多个弹出窗口,而不为每个弹出窗口创建一个分区,则需要创建一个javascript对象,然后可以创建弹出对象的多个实例 更新:下面是我正在使用的代码。我试过你的方式,但仍然没有快乐。现在我不能用我从你的帖子中理解的方式创建和弹出窗口。 我把它带回到下面,仍然有问题。感谢你帮助我。我不明白做这件事有这么难Javascript 使用此代码创建多个弹出窗口,javascript,jquery,Javascript,Jquery,我正在努力学习jQuery和弹出窗口。我通过谷歌找到了这段代码,它运行得很好。 问题是,这种设计只允许通过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.hasClassclassName和$selector.addClass className。您还应该使用弹出选择器对每个函数进行参数化,就像上面示例中的这个函数类一样。我花了几个小时修改这个JavaScript的代码,但我放弃了,并寻找一个更简单的解决方案。我选择了非常方便和易于使用的 您可以从这里下载软件包:
谢谢你帮助我的朋友。我用fancybox js包解决了所有问题。当心。