1

When I hover over my div with class album, the ajax loads and shows the slide div. Then when I leave the album div, it hides the slide div. Exactly what I want. The problem is when i hover over the album div and leave before the ajax loads, it loads when i am not hovering over the album div. How can I have it so if I am not hovering over the div, the slide div will not show no matter what?

JQUERY

$('.album').hover(function(){
    var id = $(this).attr('id');

    var url = "albumphotos.php";
    $.post(url, {contentVar: id}, function(data){
        $("#album_slide").html(data).show();

    });
    }, function(){
    $("#album_slide").html("").hide();
});

3 Answers 3

4

Try to abort the ajax when you leave the album

$('.album').hover(function(){
    var id = $(this).attr('id');

    var url = "albumphotos.php";
    var ajax = $.post(url, {contentVar: id}, function(data){
        $("#album_slide").html(data).show();
        $(this).removeData('slideajax');
    });
    $(this).data('slideajax', ajax);
}, function(){
    var ajax = $(this).data('slideajax');
    if(ajax){
        $(this).removeData('slideajax');
        ajax.abort();
    }

    $("#album_slide").html("").hide();
});
1
  • Works!! Thank you! Do you think you can go even further to explain what you did? Commented Aug 14, 2013 at 4:34
1
if( $('#elem').is(":hover")){
 //then show slide div
}

Check if you are hovering or not

3
  • are you sure it will work stackoverflow.com/questions/8010267/… Commented Aug 14, 2013 at 4:29
  • The problem wasn't as simple as this, see Arun's answer. Commented Aug 14, 2013 at 4:36
  • i see. my idea was in checking by js is elemeent is hovered or not when ajax returns answer
    – Farhad
    Commented Aug 14, 2013 at 4:40
0

Toggle a class on the element when you hover over it, and show the album_slide conditionally, depending on whether the album is visible.

$('.album').mouseenter(function(){
    var self = this;
    var url = "albumphotos.php";
    $(self).addClass("albumShown");
    $.post(url, {contentVar: self.id}, function(data){
        if ($(self).is('.albumShown')) {
            $("#album_slide").html(data).show();
        }
    });
}).mouseleave(function() {
    $("#album_slide").html("").hide();
    $(this).removeClass("albumShown");
});

Not the answer you're looking for? Browse other questions tagged or ask your own question.