【问题标题】:jQuery slide toggle shows then disappearsjQuery幻灯片切换显示然后消失
【发布时间】: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


【解决方案1】:

每次单击#go 时,您都会绑定新的#s2 .back 单击事件。将其移出点击事件。

另外请注意.on 的工作方式。它必须绑定到静态元素,所以它的形式是$(staticElement).on(event, dynamicElement, callback)。因此,如果您的 #go 有一段时间会被删除并使用 JS 添加,那么您提供的表单将不起作用。

$(document).on('click', '#go', function() {
  $('#s1').fadeOut('slow', function() {
    $('#s2').toggle('slide', {
      direction: 'right'
    }, 800);
  });
});


$(document).on('click', '#s2 .back', 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>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<div id="s1">
  <div id="go">GO TO SCREEN2</div>
</div>
<div id="s2">
  <div class="back">GO BACK</div>
</div>

【讨论】:

    【解决方案2】:

    你有第二个点击事件监听器作为第一个的回调。 取消嵌套,使它们在代码中处于相同的深度:

    $('#go').on('click', function(){
                        $('#s1').fadeOut('slow', function(){
                            $('#s2').toggle('slide', {direction: 'right'}, 800);
                        });
                    });
    
    $('#s2 .back').on('click', function(){
                                    $('#s2').fadeOut('slow', function(){
                                        $('#s1').toggle('slide', {direction: 'right'}, 800);
                                    });
                                });
    

    【讨论】:

    • 并且在您的示例中没有动态添加任何内容,因此@Justinas 对动态元素的点是无关紧要的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多