0

I'm making a div appear after another div has scrolled out of view, but the first div has a close button which hides the div. However, when the page is scrolled, the first code fires and reopens the div again.

var element = $("#request-consultation").offset().top + $("#request-consultation").outerHeight();
$(window).scroll(function() {
  var scroll = $(window).scrollTop();
  if (scroll >= element) {
    $("#request-consultation-float").show('slow');
  } else {
    $("#request-consultation-float").hide('slow');
  }
});

$(".request-float-close span").click(function() {       
    $('#request-consultation-float').hide('slow');
});

How can I make the #request-consultation-float div stay hidden after the .request-float-close has been clicked?

Lee
  • 4,187
  • 6
  • 25
  • 71
  • You need to remove the `$(window).scroll(...)` event listener in `$('req span').click(...)`. – rgajrawala Sep 07 '15 at 12:15
  • Add a `data-attribute` to your div, and when you the user clicks to close the div simply add a value to the `data-attribute` and then use that to check in your if statement. The problem you are having is when you scroll your `if` statement is returning true so you need to add another condition – Canvas Sep 07 '15 at 12:16
  • I don't have a window.scroll event in the click event... – Lee Sep 07 '15 at 12:16
  • Can you create a fiddle for your case? – KAD Sep 07 '15 at 12:18

3 Answers3

0

You may use a flag variable to check if it is clicked or not like this:

var isClicked = false;
var element = $("#request-consultation").offset().top + $("#request-consultation").outerHeight();
$(window).scroll(function() {
    if(isClicked) return;
  var scroll = $(window).scrollTop();
  if (scroll >= element) {
    $("#request-consultation-float").show('slow');
  } else {
    $("#request-consultation-float").hide('slow');
  }
});

$(".request-float-close span").click(function() {       
    $('#request-consultation-float').hide('slow');
    isClicked = true;
});
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
0

You can remember the click and behave according to that:

var element = $("#request-consultation").offset().top + $("#request-consultation").outerHeight();
var closeClicked = false;
$(window).scroll(function() {
  var scroll = $(window).scrollTop();
  if (scroll >= element && !closeClicked) {
    $("#request-consultation-float").show('slow');
  } else {
    $("#request-consultation-float").hide('slow');
  }
});

$(".request-float-close span").click(function() {       
    $('#request-consultation-float').hide('slow');
    closeClicked = true;
});
DonPaulie
  • 2,004
  • 17
  • 26
0

Remove the specific event handler:

var element = $("#request-consultation").offset().top + $("#request-consultation").outerHeight();

function myCustomScroll() {
    var scroll = $(window).scrollTop();
    if (scroll >= element) {
        $("#request-consultation-float").show('slow');
    } else {
        $("#request-consultation-float").hide('slow');
    }
}

$(window).scroll(myCustomScroll);

$(".request-float-close span").click(function() {       
    $('#request-consultation-float').hide('slow');
    $(window).unbind('scroll', myCustomScroll);
});

If you don't want to keep a reference to the listener function, you can use namespacing:

var element = $("#request-consultation").offset().top + $("#request-consultation").outerHeight();

$(window).on('scroll.consultationFloat', function() {
    var scroll = $(window).scrollTop();
    if (scroll >= element) {
        $("#request-consultation-float").show('slow');
    } else {
        $("#request-consultation-float").hide('slow');
    }
});

$(".request-float-close span").click(function() {       
    $('#request-consultation-float').hide('slow');
    $(window).unbind('scroll.consultationFloat');
});
Community
  • 1
  • 1
rgajrawala
  • 2,148
  • 1
  • 22
  • 35