0

Intention:

  1. On click of (#clickme) a DIV slides down and comes into view and (var isclicked = true)
  2. When (var isclicked = true) and on click of (#clickme), DIV slides up and (var isclicked = false)
  3. While (var isclicked = true) and DIV is in view, on click of $('html') slides the DIV up and (var isclicked = false)

Issue: While DIV is in the slideDown position, (var isclicked = true), on click of $('html') keeps firing. What am I missing so that on click o f $('html') only fires WHILE DIV is (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/

2
  • what do you mean by "only fires WHILE DIV is (var isclicked = true)"? Commented Jul 30, 2013 at 17:14
  • see my answer do you want this? Commented Jul 30, 2013 at 17:28

3 Answers 3

1

Ditch the global isClicked var, and just use a slideToggle:

$("#clickme").click(function(e) {
    e.stopPropagation();
    $("#myslider").slideToggle("slow");
});

$("body").click(function() {
    $("#myslider").is(":visible") ? $("#myslider").slideUp("slow") : 0
});

Demo: http://jsfiddle.net/7Zfwx/103/

Sign up to request clarification or add additional context in comments.

1 Comment

This works. e.stopPropagation(); and $("#myslider").is(":visible") does the trick. Thank you
1

Remember to stop (and complete) the previous animation first:

if(isclicked == false){    
    $('#myslider').stop(true, true).slideDown('slow');
      isclicked = true;       
}else{
    $('#myslider').stop(true, true).slideUp('slow');
      isclicked = false;   
}

Demo: http://jsfiddle.net/maniator/7Zfwx/94/

Comments

0

use $('body') instead of $('html')

see this JSFiddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.