3

I have a div with scroll bar. Using Firefox when I click on scroll bar to drag it down to see the div list the blur event is fired and hides my div which I have set to hide when blur is fired. How can I prevent the blur to fire when the scroll bar is used:

$("#mydiv").blur(function () {
    $('#mydiv').fadeOut();
    console.log("fadeout blur");
});

I display this div using:

 $('#mydiv').fadeIn();

I want the div to hide when its not active but not hide when I try to click on the scroll bar.

Sachin Prasad
  • 5,365
  • 12
  • 54
  • 101
Justin Homes
  • 3,739
  • 9
  • 49
  • 78
  • can you set up an example on jsfiddle.net? or post your entire code and I'll set it up for you – painotpi Jan 16 '13 at 06:06
  • I couldn't get this to happen for me. Please post your code. – Samuel Edwin Ward Jan 24 '13 at 20:26
  • Seems your scroll bar is not formed within the div & clicking on it causes call to blur. Please check the css/style used for showing scroll for div is doing what you are expecting (forming scroll bar inside div). – Pranav Singh Jan 25 '13 at 13:18

4 Answers4

3

May be this is what you are looking for

$(document).ready(function(){
        $('#mydiv').fadeIn();   

        $("body").bind('click', function(ev) {
        var myID = ev.target.id;
        if (myID !== 'mydiv') {
            $('#mydiv').fadeOut();
        }
    });
});

This will bind the click event with the body and also check the id of the element which triggers the click event. If it doesn't match the DIV, the div will be closed else the div will be always open.

Raja Asthana
  • 2,080
  • 2
  • 19
  • 35
1

You can do this one:

$(window).scroll(function() {
   $('#mydiv').css('display','block');
});
Jai
  • 74,255
  • 12
  • 74
  • 103
1
var scrolling = false, scrollingTimeout, blurTimeout;

$(window).scroll(function () {
    if (scrollingTimeout) {
        clearTimeout(scrollingTimeout);
    }
    scrolling = true;

    scrollingTimeout = setTimeout(function(){
        scrollingTimeout = null;
        scrolling = false;
    }, 300);
});
$("#mydiv").blur(function () {
    var that = $(this);
    if (!scrolling) {
        that.fadeOut();
    } else {
        if (blurTimeout) {
            clearTimeout(blurTimeout);
        }
        blurTimeout = setTimeout(function () {
            blurTimeout = null;
            that.focus();
        }, 600);
    }
});

see jQuery scroll() detect when user stops scrolling and Can I declare logic on jQuery for the start and end of a scrolling event?

Community
  • 1
  • 1
robrich
  • 13,017
  • 7
  • 36
  • 63
1

Seems your scroll bar is not formed within the div & clicking on it causes call to blur. Please check the css/style used for showing scroll for div is doing what you are expecting (forming scroll bar inside div), if this is the case then use a parent div over both(div & scroll bar) & use focusOut/blur event on parent div containing both.

Pranav Singh
  • 17,079
  • 30
  • 77
  • 104