【发布时间】:2017-07-19 03:12:34
【问题描述】:
在此处编写代码:https://codepen.io/anon/pen/gRJEwx
所以基本上 jQuery toggle 'slide' 将 div 滑入然后又滑出。它只会在您按下按钮的第二次或第三次时发生。它从不会在第一次尝试时发生,但似乎在单击后退按钮后的第二次或第三次尝试时发生。
$('#go').on('click', function() {
$('#s1').fadeOut('slow', function() {
$('#s2').toggle('slide', {
direction: 'right'
}, 800, function() {
$('#s2 .back').on('click', function() {
$('#s2').fadeOut('slow', function() {
$('#s1').toggle('slide', {
direction: 'right'
}, 800);
});
});
});
});
});
body {
overflow: hidden;
width: 400px;
background: gray;
height: 400px;
}
#s1,
#s2 {
width: 100%;
height: 100%;
}
#s1 {
padding: 20px;
display: block;
background: purple;
}
#s2 {
padding: 20px;
display: none;
background: blue;
}
#s3 {
display: none;
background: black;
}
#go,
#go2 {
width: 400px;
padding: 10px 0;
margin: 20px auto;
text-align: center;
font-weight: bold;
background: white;
}
.back {
background: white;
font-weight: bold;
width: 300px;
padding: 10px 20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="s1">
<div id="go">GO TO SCREEN2</div>
</div>
<div id="s2">
<div class="back">GO BACK</div>
</div>
所以基本上它第一次就可以正常工作。但第二次它显示第二个屏幕,然后立即隐藏。知道如何让它滑动然后保持不动直到按下后退按钮?
【问题讨论】:
-
专业提示 将
$('#s2 .back').on移出点击事件。现在,您每次点击$('#go')时都会创建新事件 -
protip #2:SO sn-p 编辑器非常好,并且可以避免人们因离开网站而浪费时间。
-
@Justinas,谢谢。我需要将 $('#s2 .back').on 完全移出 $('#go').on 吗?或者就在 $('#s2').toggle 之外?
-
@Mia 我已经提供了答案。
标签: javascript jquery html css jquery-ui