0

マウスを上に置いたときに、大きな画像を表示するには、mouseenter と mouseleave を使用します。マウスの移動が速すぎると、mouseleave が機能せず、拡大された画像が残ります。

$(document).ready(function() {
    $('.productImg').bind('mouseenter', function(){
        if(!$('#zoom_product_image').length){
            var maxWidth=$(this).parents('form').width();
            var maxHeight=$(this).parents('form').height();

            var link=this.src.replace(/small/g, 'big');
            $(this).parents('form').append('<img id="zoom_product_image" style="display: none;" src="'+link+'">');
            var img=new Image();
            img.onload=function(){
                $('#zoom_product_image').css({'position': 'absolute', 'border': '1px solid #78C7FF', 'top': '0px', 'left': '0px', 'z-index': '10000', 'opacity': '0.95', 'cursor': 'pointer'});
                $('#zoom_product_image').fadeIn(200);

                if($('#zoom_product_image').width()>maxWidth){
                    $('#zoom_product_image').css({'width': maxWidth, 'height': $('#zoom_product_image').height()*(maxWidth/$('#zoom_product_image').width())});
                }
                if($('#zoom_product_image').height()>maxHeight){
                    $('#zoom_product_image').css({'height': maxHeight, 'width': $('#zoom_product_image').width()*(maxHeight/$('#zoom_product_image').height())});
                }
            }
            img.src=link;

            $('#zoom_product_image').bind('mouseleave', function(){
                $('#zoom_product_image').detach();
            });

            $('#zoom_product_image').bind('click', function(){
                window.location.href=$('#zoom_product_image').parent().find('.bigtitle').attr('href');
            });
        }
    });
});
4

2 に答える 2

0

また、最初にイメージをロードし、ロード後にイベントをバインドすることも検討してください。

もう 1 つのヒントは、jQuery ホバー機能を使用することです (多少読みやすくなる以外は同じように機能します)。

$(".productImg").hover(
    function() {
        // do mouse enter things
    },
    function() {
        // do mouse leave things
    }
);

これを onload イベントの中に入れます。

img.onload = function() {
    $(".productImg").hover(..);
};
于 2012-10-04T13:44:45.087 に答える
0
$(document).ready(function() {
    $('.productImg').bind('mouseenter', function(){
        if($('#zoom_product_image').length)
            $('#zoom_product_image').detach();

        var maxWidth=$(this).parents('form').width();
        var maxHeight=$(this).parents('form').height();

        var link=this.src.replace(/small/g, 'big');
        $(this).parents('form').append('<img id="zoom_product_image" style="display: none;" src="'+link+'">');
        var img=new Image();
        img.onload=function(){
                $('#zoom_product_image').css({'position': 'absolute', 'border': '1px solid #78C7FF', 'top': '0px', 'left': '0px', 'z-index': '10000', 'opacity': '0.95', 'cursor': 'pointer', 'display': 'block'});

                if($('#zoom_product_image').width()>maxWidth){
                    $('#zoom_product_image').css({'width': maxWidth, 'height': $('#zoom_product_image').height()*(maxWidth/$('#zoom_product_image').width())});
                }
                if($('#zoom_product_image').height()>maxHeight){
                    $('#zoom_product_image').css({'height': maxHeight, 'width': $('#zoom_product_image').width()*(maxHeight/$('#zoom_product_image').height())});
                }
        }
        img.src=link;

        $(this).parents('form').bind('mouseleave', function(){
            $('#zoom_product_image').detach();
        });

        $('#zoom_product_image').bind('click', function(){
            window.location.href=$('#zoom_product_image').parent().find('.bigtitle').attr('href');
        });
    });
});
于 2012-10-28T13:31:32.430 に答える