【问题标题】:Popup modal to not be jumpy弹出模式不要跳动
【发布时间】:2018-07-18 07:42:58
【问题描述】:

我用 Jquery 创建了一个弹出模式,我有三个关于它的问题。

  1. 现在我可以在单击按钮时关闭弹出窗口,但我也希望在单击弹出窗口外部时关闭它。

  2. 当用户关闭页面中间的弹出窗口时,我希望页面保持在原处而不是跳回顶部。

  3. 一旦用户点击靠近弹出窗口,它就不会再次出现。我猜我可能需要编写另一个条件语句。

你们有什么提示或文档我可以查看吗?很抱歉问了很多问题。我还是 Web 开发的新手。

谢谢你,下面是我的代码。

    $(document).scroll(function(){
    var a = $(this).scrollTop();
    if (a > 500) {
        $("#mc_embed_signup").fadeIn();
    } else 

    $(".popup-close").click(function(e){
            closeSPopup();
        });
});

function closeSPopup(){
    $("#mc_embed_signup").fadeTo(0);
}

【问题讨论】:

  • 创建一个最小的例子来得到答案没有人能猜到为什么你的模态没有看到它跳跃¯\_(ツ)_/¯
  • 嗯,这就是 jquery 弹出窗口的全部内容,其余的是弹出窗口的 html 和 css,我认为没有必要显示其中的 css 和 html 部分。

标签: javascript jquery if-statement popup conditional-statements


【解决方案1】:

0 JS 和你需要的所有功能怎么样?
基本上使用checkboxeslabel 元素。

.modalToggler, 
.modalOuter,
.modal {
  position: absolute;
  visibility: hidden;
  opacity: 0;
  transition: 0.4s;
}
.modalToggler:checked + .modalOuter,
.modalToggler:checked + .modalOuter + .modal{
  visibility: visible;
  opacity: 1;
}
.modalOuter {
  position: fixed;
  top:0; right:0; bottom:0; left:0; 
  background: rgba(0,0,0,0.6);
}
.modal {
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  padding: 24px;
  background: #fff;
  max-width: 300px;  /* as needed */
}
.modalClose {
  position: absolute;
  padding: 4px 8px;
  top: 0; right: 0;
  cursor: pointer;
}
<!-- MY MODALS -->

<input id="modal1" class="modalToggler" type="checkbox">
<label for="modal1" class="modalOuter"></label>
<div class="modal">
  <label for="modal1" class="modalClose">&#x2716;</label>
  Hello, World
</div>

<input id="modal2" class="modalToggler" type="checkbox">
<label for="modal2" class="modalOuter"></label>
<div class="modal">
  <label for="modal2" class="modalClose">&#x2716;</label>
  How cool is that?
</div>

<!-- !MY MODALS -->


<label for="modal1"><a>SHOW-MODAL1</a></label>

<label for="modal2"><a>SHOW-MODAL2</a></label>

滚动的JS!

现在让我们通过添加一点点 jQuery 来添加滚动部分,仅此而已:

$(window).on("scroll", function(){
    var st = $(this).scrollTop();
    if (st > 500) {
        $("#modal1").attr("checked", true);
    }
});
.modalToggler, 
.modalOuter,
.modal {
  position: fixed;
  visibility: hidden;
  opacity: 0;
  transition: 0.4s;
}
.modalToggler:checked + .modalOuter,
.modalToggler:checked + .modalOuter + .modal{
  visibility: visible;
  opacity: 1;
}
.modalOuter {
  top:0; right:0; bottom:0; left:0; 
  background: rgba(0,0,0,0.6);
}
.modal {
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  padding: 24px;
  background: #fff;
  max-width: 300px;  /* as needed */
}
.modalClose {
  position: absolute;
  padding: 4px 8px;
  top: 0; right: 0;
  cursor: pointer;
}
<!-- MY MODALS -->

<input id="modal1" class="modalToggler" type="checkbox">
<label for="modal1" class="modalOuter"></label>
<div class="modal">
  <label for="modal1" class="modalClose">&#x2716;</label>
  <h3>Get 30%</h3>
  <p>On every book from our shelf</p>
  <p><button>APPLY NOW!</button></p>
</div>

<!-- !MY MODALS -->

<div style="border:4px dashed #000; height: 300vh"> <!--Demo sytles! Don't ever inline CSS-->
   <label for="modal1"><a>SHOW-MODAL1</a></label>
   
   <p>Scroll down.......</p>
</div>

<script src="//code.jquery.com/jquery-3.1.0.js"></script>

【讨论】:

  • 这是超级酷的 Roko,但它用于电子邮件订阅,所以我可以让弹出窗口出现在点击时,它需要出现在滚动但我会查看你的代码,看看是否有什么我可以挑出来的。谢谢。
  • 这太棒了,非常感谢。我将用它实现我的html,看看它是否有效。我很感激。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-25
  • 2017-10-05
  • 2012-09-04
  • 2016-05-22
  • 2020-12-25
相关资源
最近更新 更多