[英]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.