1

以下のコードでは、ホバー時に div を所定の位置にスライドさせ、マウスが離れた後にフェードアウトさせようとしています。これを行う最も簡単な方法は何ですか?

$(window).load(function(){  
        $('div.description').each(function(){  
        $(this).css('opacity', 0);  
        $(this).css('width', $(this).siblings('img').width());  
        $(this).parent().css('width', $(this).siblings('img').width());  
        $(this).css('display', 'block');  
    });  

    $('div.wrapper').hover(function(){    
        $(this).children('.description').stop().fadeTo(700, 0.8);  
    },function(){  
        $(this).children('.description').stop().fadeTo(700, 0);  
    });  

});
4

1 に答える 1

1
$('div.wrapper').mouseenter(function(){    
    $(this).children('.description').stop().fadeTo(700, 0.8);  
}).mouseleave(function(){  
    $(this).children('.description').stop().slideUp(700);  
});

かなりシンプルです。コードが機能するという保証はありませんが、正しい考えがあります。ラッパーdiv内の要素を変更するたびにトリガーされるわけではないため、mouseenterとmouseleaveを使用する必要があります。マウスがdivの範囲に入ると追跡し、非常にうまく離れます。

これが問題になるかどうかは思い出せませんが、そうではないと思います。divがheight:0後のslideUp()場合はslideDown(0)hide()前に行う必要がありますfadeTo()

于 2012-08-11T21:28:00.270 に答える