【问题标题】:A popup that slides, delays then slides out automatically滑动、延迟然后自动滑出的弹出窗口
【发布时间】:2019-03-04 07:17:41
【问题描述】:

我正在尝试创建一个弹出窗口,当用户执行操作时,它会从屏幕右下角滑入,停留几秒钟,然后滑出。我的代码有错误。这是我的 JS:

function showModal() {
  $('#sideModal').css('display', 'block').animate({
    width: '20%',
    display: 'block'
  }, {
    queue: false,
    duration: 1000
  }).delay(9000).animate({
    width: '0',
    display: 'none'
  }, {
    duration: 1000
  });
  //$('#sideModal').css('display', 'none');I commented this out because this prevents the div showing in the first place.
}
#sideModal {
    height: 75px;
    width: 0;
    position: fixed;
    bottom: 2%;
    left: 0;
    background: #000;
    z-index: 1000;
    display: none;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="container" style="margin-top:35px;">
<button class="btn btn-warning" onclick="showModal()">Click Me</button>
<div id="sideModal">
  I'm a sliding modal
</div>
</div>

div 会按应有的方式滑入和滑出。但是,文本仍然存在。我该如何解决这个问题?

【问题讨论】:

  • 作为旁注,您不能为显示设置动画,没有什么比 50% 块更重要的了。您可以为此使用 .fadeIn/.fadeOut,它会在不透明度上设置动画并将其设置为 block/none
  • @Dirk。谢谢你。一定忘记了动画也使用百分比。

标签: javascript jquery html css slide


【解决方案1】:

只需将overflow:hidden 添加到模态框,这样如果模态框不存在,文本将不可见。

About overflow property

function showModal() {
  $('#sideModal').css('display', 'block').animate({
    width: '20%',
    display: 'block'
  }, {
    queue: false,
    duration: 1000
  }).delay(9000).animate({
    width: '0',
    display: 'none'
  }, {
    duration: 1000
  });
  //$('#sideModal').css('display', 'none');I commented this out because this prevents the div showing in the first place.
}
#sideModal {
    height: 75px;
    width: 0;
    position: fixed;
    bottom: 2%;
    left: 0;
    background: #000;
    z-index: 1000;
    display: none;
    overflow: hidden;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="container" style="margin-top:35px;">
<button class="btn btn-warning" onclick="showModal()">Click Me</button>
<div id="sideModal">
  I'm a sliding modal
</div>
</div>

【讨论】:

    【解决方案2】:

    您需要将overflow: hidden 添加到您的#sideModal

    function showModal() {
      $('#sideModal').css('display', 'block').animate({
        width: '20%',
        display: 'block'
      }, {
        queue: false,
        duration: 1000
      }).delay(9000).animate({
        width: '0',
        display: 'none'
      }, {
        duration: 1000
      });
      //$('#sideModal').css('display', 'none');I commented this out because this prevents the div showing in the first place.
    }
    #sideModal {
        height: 75px;
        width: 0;
        position: fixed;
        bottom: 2%;
        left: 0;
        background: #000;
        z-index: 1000;
        display: none;
        overflow: hidden;
    }
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <div class="container" style="margin-top:35px;">
    <button class="btn btn-warning" onclick="showModal()">Click Me</button>
    <div id="sideModal">
      I'm a sliding modal
    </div>
    </div>

    【讨论】:

      【解决方案3】:

      如果你想让它从右侧滑入,只需将left: 0; 更改为right: 0;,如下所示:

      function showModal() {
        $('#sideModal').css('display', 'block').animate({
          right: '0%',
          display: 'block'
        }, {
          queue: false,
          duration: 1000
        }).delay(4000).animate({
          right: '-20%',
          display: 'none'
        }, {
          duration: 1000
        });
        //$('#sideModal').css('display', 'none');I commented this out because this prevents the div showing in the first place.
      }
      #sideModal {
          height: 75px;
          width: 20%;
          position: fixed;
          bottom: 2%;
          right: -20%;
          background: #000;
          z-index: 1000;
          display: none;
      }
      <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
      <div class="container" style="margin-top:35px;">
      <button class="btn btn-warning" onclick="showModal()">Click Me</button>
      <div id="sideModal">
        I'm a sliding modal
      </div>
      </div>

      编辑:进行了一些更改,以便在模式出现/消失时文本不会换行。

      希望对你有帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-02-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-23
        • 2015-07-12
        • 1970-01-01
        相关资源
        最近更新 更多