意向:
单击(#clickme),DIV向下滑动并进入视图,并且(var isclicked = true)
当(var isclicked = true)并单击(#clickme)时,DIV会向上滑动,并且(var isclicked = false)
当(var isclicked = true)和DIV在视图中时,单击$('html')会将DIV向上滑动,并且(var isclicked = false)
问题:
当DIV处于slideDown位置时,(var isclicked = true),在单击$('html')时不断触发。我缺少什么,因此在单击$('html')时仅触发WHILE DIV(var isclicked = true)
var isclicked = false;
$('#clickme').click(function(event){
doslider();
console.log(isclicked);
if(isclicked == true){
$('html').click(function(event){
console.log('is box opened? '+isclicked);
});
}
});
function doslider(){
if(isclicked == false){
$('#myslider').slideDown('slow');
isclicked = true;
}else{
$('#myslider').slideUp('slow');
isclicked = false;
}
return isclicked; }
JSFiddle:http://jsfiddle.net/7Zfwx/92/
最佳答案
抛弃全局isClicked
var,只使用slideToggle
:
$("#clickme").click(function(e) {
e.stopPropagation();
$("#myslider").slideToggle("slow");
});
$("body").click(function() {
$("#myslider").is(":visible") ? $("#myslider").slideUp("slow") : 0
});
演示:http://jsfiddle.net/7Zfwx/103/