【问题标题】:Make popup div responsive on mobile devices使弹出 div 在移动设备上响应
【发布时间】:2017-07-17 15:29:07
【问题描述】:

我正在尝试让我的弹出式 div 在移动设备上响应。它在网站上运行良好,但在我的移动设备上,您无法退出弹出窗口并且它太大了。我假设我必须使用媒体查询,但我已经尝试了一些,而且我对媒体查询不太有经验。我提供了我的问题的屏幕截图和弹出窗口的代码。

CSS:

    <style>
    #close{
    width: 20px;
    height: 20px;
    background: #000;
    border-radius: 50%;
    border: 3px solid #fff;
    display: block;
    text-align: center;
    color: #fff;
    text-decoration: none;
    position: absolute;
    top: -15px;
    right: -15px;
    cursor: pointer;
    }

    </style>

  <!--Shop Now Button-->
  <style>

    #btn1{
    position:absolute;
    float: center;
    width: 300px;
    height: 50px;
    font-size: 20px;
    font-weight: bold;
   /* box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19); */
    margin-top:320px; /*brings buttons down*/
      margin-left:60px;
    }


    #emailf{
      position:absolute;
      float: center;
    width: 300px;
    height: 40px;
    font-size: 20px;
    color:black;
    font-weight: bold;
   /* box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19); */
    margin-top:200px; /*brings buttons down*/
      margin-left:60px;
    }

    #password1 {
      position:absolute;
    float: center;
    width: 300px;
    height: 40px;
    font-size: 20px;
    color:black;
    font-weight: bold;
   /* box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19); */
    margin-top:260px; /*brings buttons down*/
      margin-left:60px;
    }


  </style>

  <!-- CSS for the Popup Box -->
  <style>




     #hider
    {
        position:fixed;
        top: 0%;
        left: 0%;
        width:9000px;
        height:9000px;
        margin-top: -100px; /*set to a negative number 1/2 of your height*/
        margin-left: -200px; /*set to a negative number 1/2 of your width*/
        opacity:0.8;
        background-color:#666;
        z-index:99999;

    }

    #popup_box  
    {
        background-image: url('https://cdn.shopify.com/s/files/1/0569/4201/files/popupbanner_3.jpg?11297628032998908977');
        position:fixed;
        top: 50%;
        left: 50%;
        width:450px;
        height:570px;
        margin-top: -315px; /*set to a negative number 1/2 of your height*/
        margin-left: -225px; /*set to a negative number 1/2 of your width*/

       z-index:999999; 

        border: none;
        border:  none;
        padding: 40px;


    }    


  </style>

JAVASCRIPT/JQUERY:

  <script>

     $(document).ready(function () {
        //hide hider and popup_box
        $("#hider").hide
        $('#popup_box').fadeIn("slow");

        //on click show the hider div and the message
        $("#showpopup").click(function () {
            $("#hider").fadeIn("slow");

        });


        //on click hide the message
        $("#close").click(function () {

            $("#hider").fadeOut("slow");
            $('#popup_box').fadeOut("slow");
            $('#popup_box').close;

        });

       $("#shop").click(function () {

            $("#hider").fadeOut("slow");
            $('#popup_box').fadeOut("slow");
        });

       $(document).keyup(function(e) {
     if (e.keyCode == 27) { 
       $("#hider").fadeOut("slow");
       $('#popup_box').fadeOut("slow");
            $('#popup_box').close;
    }
});

        });


 function PopUp(hideOrshow) {
    if (hideOrshow == 'hide') {
        document.getElementById('ac-wrapper').style.display = "none";
    }
    else  if(localStorage.getItem("popupWasShown") == null) {
        localStorage.setItem("popupWasShown",1);
        document.getElementById('ac-wrapper').removeAttribute('style');
    }
}
window.onload = function () {
    setTimeout(function () {
        PopUp('show');
    }, 0);
}


function hideNow(e) {
    if (e.target.id == 'ac-wrapper') document.getElementById('ac-wrapper').style.display = 'none';
}

</script>

HTML:

<div id="ac-wrapper" style='display:none' onClick="hideNow(event)"> 
    <div id="hider"></div>
    <div id="popup_box" >




      <br>
       {% form 'create_customer' %}


    <center>
       <div id="emailf" class="clearfix large_form">
              <label for="email" class="label"></label>
              <input type="email" value="" name="customer[email]"placeholder="Email" id="email" class="text" style="text-align:center; margin: 0 auto; width:300px; height:30px;" />
            </div>
       <div id="password1" class="clearfix large_form">
              <label for="password" class="label"></label>
              <input type="password" value="" name="customer[password]"placeholder="Password" id="password" class="password text" style="text-align:center; margin: 0 auto; width:300px; height:30px;" />
            </div>



      <div id="btn1" class="clearfix large_form">
              <label for="Create" class="btn1"></label>
              <input type="submit" value="SIGN UP!" style="text-align:center; font-weight:bold;margin: 0 auto; width:310px; height:40px;background-color:#852B8A;color:white;" />
            </div>
      <div id="close">X</div>
     <!--  <input class="btn1" type="submit" value="Create" />-->
     <!-- <button id="shop"><a href="https://www.alzerina.com/collections/ready-to-wear">Sign Up Now!</a></button> -->

    </center>
    </div>   {% endform %}
</div>

您可以在 alzerina.com 上查看弹出窗口的网页版

【问题讨论】:

    标签: jquery css mobile responsive-design media-queries


    【解决方案1】:

    你可以像这样使用媒体查询:

    <style>
      @media (max-width: 600px) {
       #popup_box{
        top: 0;
        left: 0;
        width:100%;
        max-width: 100%;
        height:100%;
       }
     }
    </style>
    

    【讨论】:

    • 谢谢我试过了,但现在我遇到了另一个问题!我的 div popup_box 未居中,由于某种原因无法居中?
    • 嘿詹姆斯,只需从这个媒体查询中删除顶部和左侧,这样它就应该居中:)
    【解决方案2】:

    试试这个。 仅简单的 CSS 和 Div:

    <style>
         *, *:after, *:before {
      box-sizing: border-box;
      margin: 0;
      padding: 0;
    }
    
    @keyframes fade {
      from {
        opacity: 0;
        transform: translate(-50%, -50%) scale(0.8);
      }
      to {
        opacity: 1;
        transform: translate(-50%, -50%) scale(1);
      }
    }
    .confirm {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      width: 80%;
      min-width: 280px;
      max-width: 500px;
      height: auto;
      background: #E2E2E2;
      border-radius: 10px;
      padding: 0;
      margin: 0;
      border-top: 1px solid white;
      animation: fade 1s ease 1 forwards;
    }
    .confirm h1 {
      text-align: center;
      font-size: 1.2rem;
      margin: 1.5rem 1rem 0.5rem;
    }
    .confirm p {
      text-align: center;
      font-size: 1rem;
      margin: 0 2rem 4.5rem;
    }
    .confirm button {
      background: transparent;
      border: none;
      color: #1678E5;
      height: 3rem;
      font-size: 1rem;
      width: 50%;
      position: absolute;
      bottom: 0;
      cursor: pointer;
    }
    .confirm button:nth-of-type(1) {
      border-top: 1px solid #B4B4B4;
      border-right: 1px solid #B4B4B4;
      left: 0;
      border-radius: 0 0 0 10px;
    }
    .confirm button:nth-of-type(2) {
      border-top: 1px solid #B4B4B4;
      right: 0;
      border-radius: 0 0 10px 0;
    }
    .confirm button:focus,
    .confirm button:hover {
      font-weight: bold;
      background: #EFEFEF;
    }
    .confirm button:active {
      background: #D6D6D6;
    }
    
     </style>
    
    
    <div class="confirm">
      <h1>Text Head</h1>
       <h1></h1>
      <p><strong>Text Deatils </strong></p>
        <form action="">
       <button>OK</button>
      <button>Cancel</button>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-06
      • 1970-01-01
      • 2016-04-23
      • 2018-07-15
      • 2014-04-24
      • 2017-10-22
      • 1970-01-01
      • 2013-12-31
      相关资源
      最近更新 更多