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();
});
user2127833
  • 179
  • 2
  • 5
  • 16

3 Answers3

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();
});
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
1
if( $('#elem').is(":hover")){
 //then show slide div
}

Check if you are hovering or not

Farhad
  • 742
  • 6
  • 22
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");
});
nbrooks
  • 18,126
  • 5
  • 54
  • 66