创建一个区分每个按钮的弹出容器(因此它们不会一次全部弹出)[重复]

Posted

技术标签:

【中文标题】创建一个区分每个按钮的弹出容器(因此它们不会一次全部弹出)[重复]【英文标题】:Create a popup container that distinguishes each button (so they don't all popup at once) [duplicate] 【发布时间】:2022-01-13 20:16:09 【问题描述】:

我通常很擅长寻找我的答案,但我似乎无法做到这一点。我对此弹出窗口的设置是完美的(响应迅速,与里面的内容(短代码)等配合得很好)我遇到的问题是因为我已经从 codepen 进行了改装,每个弹出窗口只有 1 个页面。

我正在尝试创建一个项目库,其中他们单击一个锚点,一个带有滑块的容器显示项目的工作(它完美地工作,仅用于一个弹出窗口)但可能有 15 个项目库(弹出窗口)每页。

我是 jQuery 的新手,所以请放轻松,我一直在学习。

html

jQuery(document).ready(function( $ )
   jQuery(document).ready(function($)
  //open popup
  $('.popup-trigger').on('click', function(event)
    event.preventDefault();
    $('.popup').addClass('is-visible');

  );
     

  //close popup
  $('.popup').on('click', function(event)
    if( $(event.target).is('.popup-close') || $(event.target).is('.popup') ) 
      event.preventDefault();
      $(this).removeClass('is-visible');
    
  );
  //close popup when clicking the esc keyboard button
  $(document).keyup(function(event)
      if(event.which=='27')
        $('.popup').removeClass('is-visible');
      
    );
);
);
@media(max-width:1150px) 
  .popup-container
    top: 25% !important;
    width: 90% !important;
    height: 50%;
    padding: 2rem !important;
  

.page-id-474 main#content
    margin: 0px !important

.popup-trigger 
  width: 170px;
  right: 2rem;
  margin: 3em auto;
  text-align: center;
  color: #FFF;
  font-size: 18px;
  padding:1rem 2rem;
  text-decoration:none;
  transition:300ms all;


.popup-trigger:hover 
    opacity:.8;

.popup1 
  position: fixed;
  left: 0;
  top: 0;
  height: 100%;
  z-index: 1000;
  width: 100%;
  background-color: rgba(56, 56, 55, 0.9);
  opacity: 0;
  visibility: hidden;
  transition:500ms all;



.popup1.is-visible 
  opacity: 1;
  visibility: visible;
 transition:1s all;

.popup 
  position: fixed;
  left: 0;
  top: 0;
  height: 100%;
  z-index: 1000;
  width: 100%;
  background-color: rgba(56, 56, 55, 0.9);
  opacity: 0;
  visibility: hidden;
  transition:500ms all;



.popup.is-visible 
  opacity: 1;
  visibility: visible;
 transition:1s all;


.popup-container 
  transform:translateY(-50%);
  transition:500ms all;
  position: relative;
  width: 60%;
  margin: 2em auto;
  top: 5%;
  padding:5rem;
  background: #FFF;
  border-radius: .25em .25em .4em .4em;
  text-align: center;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.2);


.is-visible .popup-container 
  transform:translateY(0);
  transition:500ms all;


.popup-container .popup-close 
  position: absolute;
  top: 8px;
  font-size:0;
  right: 8px;
  width: 30px;
  height: 30px;



.popup-container .popup-close::before,
.popup-container .popup-close::after 
  content: '';
  position: absolute;
  top: 12px;
  width: 14px;
  height: 3px;
  background-color: #8f9cb5;


.popup-container .popup-close::before 
  -webkit-transform: rotate(45deg);
  -moz-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  -o-transform: rotate(45deg);
  transform: rotate(45deg);
  left: 8px;


.popup-container .popup-close::after 
  -webkit-transform: rotate(-45deg);
  -moz-transform: rotate(-45deg);
  -ms-transform: rotate(-45deg);
  -o-transform: rotate(-45deg);
  transform: rotate(-45deg);
  right: 8px;



.popup-container .popup-close:hover:before,
.popup-container .popup-close:hover:after 
  background-color:#35a785;
  transition:300ms all;

/*==========================================================================
    Site grid
==========================================================================  */

.grid-this 
    width: 100%;
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
    padding: 30px;

.grid-this.with-spacing 
    gap: 30px;

.grid-this .block img 
    width: 100%;
    height: auto;
    vertical-align: top;


/* Grid spacing in slick slider */

.grid-this.with-spacing .slick-slide 
    margin-right: 30px;

.grid-this.with-spacing .slick-list 
    margin-right: -30px;


@media(max-width:1150px) 
    .grid-this.with-spacing .slick-slide 
        margin-right: 0;
    
    .grid-this.with-spacing .slick-list 
        margin-right: 0;
    
.page-id-324    .site-container
        padding-left: 8px !important;
    padding-right: 8px !important;
    




/* -------------------------------------------------------------
/
/ Main Cat Container
/
/ -------------------------------------------------------------*/
.main-cat-container
    Padding: 0px;
    background-color: #fff;
    border-style: solid;
    border-width: 3px;
    border-color:#e9ecf2;
    transition:all 1s ease !important;


.main-cat-container:hover
    box-shadow: 0px 10px 13px -4px rgba(0,0,0,0.3);
-webkit-box-shadow: 0px 10px 13px -4px rgba(0,0,0,0.3);
-moz-box-shadow: 0px 10px 13px -4px rgba(0,0,0,0.3);
transition:all 1s ease !important;


.main-cat-title
    padding-top: 12px;
    font-size:22px;
    color:#38507b;
    text-align: center;

.main-cat-container:hover i
color:#0e1932;
   
.mc-icons
    text-align: center;

.mc-icons i
    font-size:45px;

.choice-p
    color:#343a40;
    font-size:22px;
    font-weight: bold;

.custom-hr-spacing
    margin-top: 45px !important;
    margin-bottom: 45px !important;

.faux-btn-container
    text-align: center !important;
    margin-top: 20px !important;


.faux-btn
    
    background-color:#4cb150;
    padding: 15px 35px;
    color: #FFF;
    transition:all 1s ease !important;
    border-radius: 8px;

.faux-btn:hover
    background-color:#59cf5d;
    transition:all 1s ease !important;
<div  class="grid-this with-spacing margins-fix">
<div>
<a href="#0" id="info" class="info popup-trigger" title="info">
<div style="padding-bottom: 50px;" class="main-cat-container">
<div class="mc-icons" data-aos="flip-left"  data-aos-delay="50" aos-duration="500"><img src="https://gilbert.level10designdev.com/wp-content/uploads/2021/12/Gilbert-Design-Build-Before-After-Kitchen-Remodel-Bradenton-Florida.jpg" width=100% height=auto></div>
<h3 class="main-cat-title">vEITH</h3>

<div class="faux-btn-container"><span class="faux-btn">See Project</span></div>
</div> 
</a>
<div class="popup" role="alert">
  <div class="popup-container">
    <a href="#0" class="popup-close img-replace">Close</a>
   [metaslider id="2409"]
  </div> 
</div> 
</div>
<div><a href="#0" id="info" class="info popup-trigger" title="info">

<div style="padding-bottom: 50px;" class="main-cat-container">
<div class="mc-icons" data-aos="flip-left"  data-aos-delay="50" aos-duration="500"><img src="https://gilbert.level10designdev.com/wp-content/uploads/2021/12/Gilbert-Design-Build-Before-After-Kitchen-Remodel-Bradenton-Florida.jpg" width=100% height=auto></div>
<h3 class="main-cat-title">vEITH</h3>

<div class="faux-btn-container"><span class="faux-btn">See Project</span></div>
</div> 
</a>
<div class="popup" role="alert">
  <div class="popup-container">
    <a href="#0" class="popup-close img-replace">Close</a>
   [metaslider id="2409"]
  </div> 
</div> 
</div>
<div>.</div>
</div>

我还创建了一个代码笔来直观地向您展示问题:

https://codepen.io/John_Barbagallo/pen/dyVXOvV

非常感谢任何帮助!

【问题讨论】:

【参考方案1】:

您的 html 代码有语法错误。 id 属性为 HTML 元素指定一个唯一的 id。 id 属性的值在 HTML 文档中必须是唯一的。见details

为什么要嵌套这两段代码:

jQuery(document).ready(function( $ )
     jQuery(document).ready(function($)
            
     )
)

我认为您应该删除重复的代码部分。

$('.popup').addClass('is-visible');这段代码意味着所有包含popup类的html元素都将被添加一个is-visible类。这会导致您的错误。

你应该Jquery traversing找到popup属于被点击的项目 改成这样:

$('.popup-trigger').on('click', function(event)
     event.preventDefault();
     $(this).parent().find('.popup').addClass('is-visible');
);

这里的所有代码:

$(document).ready(function()
    //open popup
    $('.popup-trigger').on('click', function(event)
        event.preventDefault();
        $(this).parent().find('.popup').addClass('is-visible');
    );

    //close popup
    $('.popup').on('click', function(event)
        if( $(event.target).is('.popup-close') || $(event.target).is('.popup') ) 
            event.preventDefault();
            $(this).removeClass('is-visible');
        
    );

    //close popup when clicking the esc keyboard button
    $(document).keyup(function(event)
        if(event.which=='27')
            $('.popup').removeClass('is-visible');
        
    );
);
    @media(max-width:1150px) 
        .popup-container
            top: 25% !important;
            width: 90% !important;
            height: 50%;
            padding: 2rem !important;
        

    
    .page-id-474 main#content
        margin: 0px !important
    
    .popup-trigger 
        width: 170px;
        right: 2rem;
        margin: 3em auto;
        text-align: center;
        color: #FFF;
        font-size: 18px;
        padding:1rem 2rem;
        text-decoration:none;
        transition:300ms all;
    

    .popup-trigger:hover 
        opacity:.8;
    
    .popup1 
        position: fixed;
        left: 0;
        top: 0;
        height: 100%;
        z-index: 1000;
        width: 100%;
        background-color: rgba(56, 56, 55, 0.9);
        opacity: 0;
        visibility: hidden;
        transition:500ms all;

    

    .popup1.is-visible 
        opacity: 1;
        visibility: visible;
        transition:1s all;
    
    .popup 
        position: fixed;
        left: 0;
        top: 0;
        height: 100%;
        z-index: 1000;
        width: 100%;
        background-color: rgba(56, 56, 55, 0.9);
        opacity: 0;
        visibility: hidden;
        transition:500ms all;

    

    .popup.is-visible 
        opacity: 1;
        visibility: visible;
        transition:1s all;
    

    .popup-container 
        transform:translateY(-50%);
        transition:500ms all;
        position: relative;
        width: 60%;
        margin: 2em auto;
        top: 5%;
        padding:5rem;
        background: #FFF;
        border-radius: .25em .25em .4em .4em;
        text-align: center;
        box-shadow: 0 0 20px rgba(0, 0, 0, 0.2);
    

    .is-visible .popup-container 
        transform:translateY(0);
        transition:500ms all;
    

    .popup-container .popup-close 
        position: absolute;
        top: 8px;
        font-size:0;
        right: 8px;
        width: 30px;
        height: 30px;
    


    .popup-container .popup-close::before,
    .popup-container .popup-close::after 
        content: '';
        position: absolute;
        top: 12px;
        width: 14px;
        height: 3px;
        background-color: #8f9cb5;
    

    .popup-container .popup-close::before 
        -webkit-transform: rotate(45deg);
        -moz-transform: rotate(45deg);
        -ms-transform: rotate(45deg);
        -o-transform: rotate(45deg);
        transform: rotate(45deg);
        left: 8px;
    

    .popup-container .popup-close::after 
        -webkit-transform: rotate(-45deg);
        -moz-transform: rotate(-45deg);
        -ms-transform: rotate(-45deg);
        -o-transform: rotate(-45deg);
        transform: rotate(-45deg);
        right: 8px;
    


    .popup-container .popup-close:hover:before,
    .popup-container .popup-close:hover:after 
        background-color:#35a785;
        transition:300ms all;
    
    /*==========================================================================
        Site grid
    ==========================================================================  */

    .grid-this 
        width: 100%;
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
        padding: 30px;
    
    .grid-this.with-spacing 
        gap: 30px;
    
    .grid-this .block img 
        width: 100%;
        height: auto;
        vertical-align: top;
    

    /* Grid spacing in slick slider */

    .grid-this.with-spacing .slick-slide 
        margin-right: 30px;
    
    .grid-this.with-spacing .slick-list 
        margin-right: -30px;
    

    @media(max-width:1150px) 
        .grid-this.with-spacing .slick-slide 
            margin-right: 0;
        
        .grid-this.with-spacing .slick-list 
            margin-right: 0;
        
        .page-id-324    .site-container
            padding-left: 8px !important;
            padding-right: 8px !important;
        
    



    /* -------------------------------------------------------------
    /
    / Main Cat Container
    /
    / -------------------------------------------------------------*/
    .main-cat-container
        Padding: 0px;
        background-color: #fff;
        border-style: solid;
        border-width: 3px;
        border-color:#e9ecf2;
        transition:all 1s ease !important;
    

    .main-cat-container:hover
        box-shadow: 0px 10px 13px -4px rgba(0,0,0,0.3);
        -webkit-box-shadow: 0px 10px 13px -4px rgba(0,0,0,0.3);
        -moz-box-shadow: 0px 10px 13px -4px rgba(0,0,0,0.3);
        transition:all 1s ease !important;
    

    .main-cat-title
        padding-top: 12px;
        font-size:22px;
        color:#38507b;
        text-align: center;
    
    .main-cat-container:hover i
        color:#0e1932;
    
    .mc-icons
        text-align: center;
    
    .mc-icons i
        font-size:45px;
    
    .choice-p
        color:#343a40;
        font-size:22px;
        font-weight: bold;
    
    .custom-hr-spacing
        margin-top: 45px !important;
        margin-bottom: 45px !important;
    
    .faux-btn-container
        text-align: center !important;
        margin-top: 20px !important;
    

    .faux-btn

        background-color:#4cb150;
        padding: 15px 35px;
        color: #FFF;
        transition:all 1s ease !important;
        border-radius: 8px;
    
    .faux-btn:hover
        background-color:#59cf5d;
        transition:all 1s ease !important;
    
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="grid-this with-spacing margins-fix">
    <div>
        <a href="#0" id="project01" class="info popup-trigger" title="info">

            <div style="padding-bottom: 50px;" class="main-cat-container">
                <div class="mc-icons" data-aos="flip-left" data-aos-delay="50" aos-duration="500"><img
                        src="https://gilbert.level10designdev.com/wp-content/uploads/2021/12/Gilbert-Design-Build-Before-After-Kitchen-Remodel-Bradenton-Florida.jpg"
                        width=100% height=auto></div>
                <h3 class="main-cat-title">vEITH</h3>

                <div class="faux-btn-container"><span class="faux-btn">See Project</span></div>
            </div>
        </a>

        <div class="popup" role="alert">
            <div class="popup-container">
                <a href="#0" class="popup-close img-replace">Close</a>
                [project01]
            </div>
        </div>
    </div>


    <div>
        <a href="#0" id="project02" class="info popup-trigger" title="info">
        <div style="padding-bottom: 50px;" class="main-cat-container">
            <div class="mc-icons" data-aos="flip-left" data-aos-delay="50" aos-duration="500"><img
                    src="https://gilbert.level10designdev.com/wp-content/uploads/2021/12/Gilbert-Design-Build-Before-After-Kitchen-Remodel-Bradenton-Florida.jpg"
                    width=100% height=auto></div>
            <h3 class="main-cat-title">vEITH</h3>

            <div class="faux-btn-container"><span class="faux-btn">See Project</span></div>
        </div>
    </a>
        <div class="popup" role="alert">
            <div class="popup-container">
                <a href="#0" class="popup-close img-replace">Close</a>
                [project02]
            </div>
        </div>
    </div>

    <div>.</div>

</div>

【讨论】:

林陈德,谢谢! 如果我的回答对您有帮助,请点赞或接受。【参考方案2】:

您好,您需要获取答题器,然后在答题器的父级中找到弹出窗口,如下所示:

//open popup
$('.popup-trigger').on('click', function(event)
  event.preventDefault();
  // Next line is what changed.
  $(this).parent().find(".popup").addClass('is-visible');
);
     

【讨论】:

以上是关于创建一个区分每个按钮的弹出容器(因此它们不会一次全部弹出)[重复]的主要内容,如果未能解决你的问题,请参考以下文章

jquery easyUI 第一次点击按钮加载两个tab的datagrid 脚本报undefined的弹出框, 第二次就正常不报错了呢

创建没有导航栏按钮的弹出菜单

Kivy:访问不同类的方法

XCode5 中 UITableviewCell 的弹出视图

如何显示弹出视图

弹出确认窗口的 Web UI 自动化