如何实现多个弹窗?

发布于 2024-10-22 05:42:58 字数 2941 浏览 1 评论 0原文

我在实现多个弹出窗口时遇到问题。我从网上获得这个脚本,当我应用于单个弹出窗口时它是正确的,但如果我复制它,则它是正确的。您可以在以下位置看到它:Dendrosite。在左边距菜单(Sinopsi/Fitxa/Autors)中,我在一个(Sinopsi)中正确实现了,但现在我可以实现其他(Fitxa/Autors)

HTML:

<li class="sinopsi"><a id="go"><span></span></a></li>
        <div id="popupContact">
    <a id="popupContactClose"></a>
    <h1></h1>
    <h3>
        <br/><br/>
    </h3>
</div>
<div id="backgroundPopup"></div>

Javascript:

var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup(){
    //loads popup only if it is disabled
    if(popupStatus==0){
        $("#backgroundPopup").css({
            "opacity": "0.7"
        });
        $("#backgroundPopup").fadeIn("fast");
        $("#popupContact").fadeIn("fast");
        popupStatus = 1;
    }
}

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



//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){

    //LOADING POPUP
    //Click the button event!
    $("#go").click(function(){
        //centering with css
        //load popup
        loadPopup();
    });

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

});

和CSS:

#backgroundPopup{
display:none;
position:fixed;
_position:absolute; /* hack for internet explorer 6*/
height:100%;
width:100%;
top:0;
left:0;
z-index:1;
}
#popupContact{
    margin-top: -104px;
    margin-left:102px;
    display:none;
    position:fixed;
    _position:absolute; /* hack for internet explorer 6*/
    height:288px;
    width:600px;
    z-index:9;
    padding:12px;
    background-color:  #333;
    filter: alpha(opacity=20); opacity: .5
}
#popupContact h1{
    font-family:Arial, Helvetica, sans-serif;
    font-size:20px;
    color:#FFF;
    text-shadow: 0px 1px 1px #000;
    padding-bottom:10px;
    margin-bottom:30px;
    border-bottom-width: 1px;
    border-bottom-style: solid;
}

#popupContact h3{
    font-family: Arial, Helvetica, sans-serif;
    font-size: 12px;
    color: #FFF;
    text-align: justify;
}

#popupContactClose{
    font-size:18px;
    line-height:14px;
    right:6px;
    top:4px;
    position:absolute;
    color: #ffeb70;
    font-weight:700;
    display:block;
    cursor: pointer;
    font-family: Arial, Helvetica, sans-serif;
}

谢谢!

I have a problem to implement multiple popups. I get this script from net, it's correct when I apply to a single popup but not if i do copies of this. You can see it in: Dendrosite. In the margin left menu (Sinopsi/Fitxa/Autors) i implemented correcly in one (Sinopsi) but now I can implement to other (Fitxa/Autors)

HTML:

<li class="sinopsi"><a id="go"><span></span></a></li>
        <div id="popupContact">
    <a id="popupContactClose"></a>
    <h1></h1>
    <h3>
        <br/><br/>
    </h3>
</div>
<div id="backgroundPopup"></div>

Javascript:

var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup(){
    //loads popup only if it is disabled
    if(popupStatus==0){
        $("#backgroundPopup").css({
            "opacity": "0.7"
        });
        $("#backgroundPopup").fadeIn("fast");
        $("#popupContact").fadeIn("fast");
        popupStatus = 1;
    }
}

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



//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){

    //LOADING POPUP
    //Click the button event!
    $("#go").click(function(){
        //centering with css
        //load popup
        loadPopup();
    });

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

});

and CSS:

#backgroundPopup{
display:none;
position:fixed;
_position:absolute; /* hack for internet explorer 6*/
height:100%;
width:100%;
top:0;
left:0;
z-index:1;
}
#popupContact{
    margin-top: -104px;
    margin-left:102px;
    display:none;
    position:fixed;
    _position:absolute; /* hack for internet explorer 6*/
    height:288px;
    width:600px;
    z-index:9;
    padding:12px;
    background-color:  #333;
    filter: alpha(opacity=20); opacity: .5
}
#popupContact h1{
    font-family:Arial, Helvetica, sans-serif;
    font-size:20px;
    color:#FFF;
    text-shadow: 0px 1px 1px #000;
    padding-bottom:10px;
    margin-bottom:30px;
    border-bottom-width: 1px;
    border-bottom-style: solid;
}

#popupContact h3{
    font-family: Arial, Helvetica, sans-serif;
    font-size: 12px;
    color: #FFF;
    text-align: justify;
}

#popupContactClose{
    font-size:18px;
    line-height:14px;
    right:6px;
    top:4px;
    position:absolute;
    color: #ffeb70;
    font-weight:700;
    display:block;
    cursor: pointer;
    font-family: Arial, Helvetica, sans-serif;
}

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄居人 2024-10-29 05:42:58

它不起作用的原因是,对于每个 #go #go1 #go2 弹出链接,您需要三个单独的事件处理程序,而不仅仅是一个。这就是为什么当您单击其中一个框时会弹出多个框,而当您单击另外两个框时则不会弹出任何框。

为了使其正常工作,我将重写弹出脚本以支持多个弹出窗口。但您可以简单地按程序执行此操作,而无需使用函数。

HTML(将下面的代码与您的代码合并。ID、类名与 javascript 相关)

<li id='sinopsi' class='openlink'>Sinopsi
  <div class='popup' id='popup1'>Popup 1<span class='closex'>x</span></div>
</li>

<li id='fitxa' class='openlink'>Fitxa
  <div class='popup' id='popup2'>Popup 2<span class='closex'>x</span></div>
</li>

<li id='autors' class='openlink'>Autors
  <div class='popup' id='popup3'>Popup 2<span class='closex'>x</span></div>
</li>

JavaScript

<script>
$(function(){

$('#sinopsi').click(function(){  // Activates the popups
  $('#popup1').fadeIn('fast')
});

$('#fitxa').click(function(){
  $('#popup2').fadeIn('fast')
});

$('#sinopsi').click(function(){
  $('#popup3').fadeIn('fast')
});

/* //Note that if your HTML is properly nested you could easily have this command execute your popups instead of the three above

$('.openlink').click(function(){
  $(this)
    .find('.popup')  // finds your nested popup div
    .fadeIn('fast')
  ;
});

*/

$('.closex').click(function(){  // closes the popup, when X is clicked
  $('.popup').fadeOut('fast');
});

$('.popup').keypress(function(e){  // close popup via ESC key.
  if(e.keyCode==27){
    $(this).fadeOut('fast');
  }
}); 

$('.popup').css({opacity: "0.7"}); // copies over the transparency 

});
</script>

CSS(确保弹出窗口加载隐藏。)

.popup{display:none}

祝你好运,伙计,顺便说一句,该网站看起来不错。您确实应该更多地了解 jQuery,与 JavaScript 相比,它非常强大且易于使用。我想你会喜欢的。

http://jquery.com/
http://api.jquery.com/click/

ps 不要担心所有这些匿名函数 function(){//do stuff},它只是 function bar(){//do stuff} $('.open').click(bar)< 的简写/代码>

The reason it didn't work was that for every #go #go1 #go2 popup links, you need three separate event handlers, not just one. That's why multiple boxes pop-ups when you click on one, and none pop-ups when you click on the other two.

To get this to work I would rewrite the popup script to support multiple popups. But you can simply do this procedurally without using functions.

HTML (Merge the code below with yours. The IDs, Class names relate to the javascript)

<li id='sinopsi' class='openlink'>Sinopsi
  <div class='popup' id='popup1'>Popup 1<span class='closex'>x</span></div>
</li>

<li id='fitxa' class='openlink'>Fitxa
  <div class='popup' id='popup2'>Popup 2<span class='closex'>x</span></div>
</li>

<li id='autors' class='openlink'>Autors
  <div class='popup' id='popup3'>Popup 2<span class='closex'>x</span></div>
</li>

JavaScript

<script>
$(function(){

$('#sinopsi').click(function(){  // Activates the popups
  $('#popup1').fadeIn('fast')
});

$('#fitxa').click(function(){
  $('#popup2').fadeIn('fast')
});

$('#sinopsi').click(function(){
  $('#popup3').fadeIn('fast')
});

/* //Note that if your HTML is properly nested you could easily have this command execute your popups instead of the three above

$('.openlink').click(function(){
  $(this)
    .find('.popup')  // finds your nested popup div
    .fadeIn('fast')
  ;
});

*/

$('.closex').click(function(){  // closes the popup, when X is clicked
  $('.popup').fadeOut('fast');
});

$('.popup').keypress(function(e){  // close popup via ESC key.
  if(e.keyCode==27){
    $(this).fadeOut('fast');
  }
}); 

$('.popup').css({opacity: "0.7"}); // copies over the transparency 

});
</script>

CSS (make sure the popups loaded hidden.)

.popup{display:none}

Good luck mate, the site looks good btw. You should really learn more about jQuery, it's pretty powerful and really easy to use compared to just JavaScript. I'd think you'll enjoy it.

http://jquery.com/
http://api.jquery.com/click/

p.s. Don't get to worried about all those anonymous functions function(){//do stuff}, it's just a shorthand of function bar(){//do stuff} $('.open').click(bar)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文