弹出窗口,会滑动,延迟然后自动滑出

时间:2018-09-28 09:58:50

标签: javascript jquery html css slide

我正在尝试创建一个弹出窗口,当用户执行操作时,该弹出窗口从屏幕的右下角滑入,停留几秒钟,然后滑出。我的代码错误。这是我的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根据需要滑动进出。但是,文本仍然存在。我该如何解决?

3 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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 :(得分:0)

如果要使其从右侧滑入,只需将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>

编辑:进行了一些更改,以使模态出现/消失时文本不会自动换行。

希望有帮助