2017-08-31 4 views
2

上に複数の、別々の拡大効果を作成する方法はホバー

$(document).ready(function() { 
 
    var native_width = 0; 
 
    var native_height = 0; 
 
    
 
    $(".large").css("background", "url('" + $(".small").attr("src") + "') no-repeat"); 
 

 
    $(".magnify").mousemove(function(e) { 
 
    if (!native_width && !native_height) { 
 
     var image_object = new Image(); 
 
     image_object.src = $(".small").attr("src"); 
 
     native_width = image_object.width; 
 
     native_height = image_object.height; 
 
    } else { 
 
     var magnify_offset = $(this).offset(); 
 
     var mx = e.pageX - magnify_offset.left; 
 
     var my = e.pageY - magnify_offset.top; 
 

 
     if (mx < $(this).width() && my < $(this).height() && mx > 0 && my > 0) { 
 
     $(".large").fadeIn(100); 
 
     } else { 
 
     $(".large").fadeOut(100); 
 
     } 
 
     if ($(".large").is(":visible")) { 
 

 
     var rx = Math.round(mx/$(".small").width() * native_width - $(".large").width()/2) * -1; 
 
     var ry = Math.round(my/$(".small").height() * native_height - $(".large").height()/2) * -1; 
 
     var bgp = rx + "px " + ry + "px"; 
 

 
     var px = mx - $(".large").width()/2; 
 
     var py = my - $(".large").height()/2; 
 

 
     $(".large").css({ 
 
      left: px, 
 
      top: py, 
 
      backgroundPosition: bgp 
 
     }); 
 
     } 
 
    } 
 
    }) 
 
})
* { 
 
    margin: 0; 
 
    padding: 0; 
 
} 
 

 
.magnify { 
 
    width: 200px; 
 
    margin: 50px auto; 
 
    position: relative; 
 
    cursor: none 
 
} 
 

 
.large { 
 
    width: 175px; 
 
    height: 175px; 
 
    position: absolute; 
 
    border-radius: 100%; 
 
    box-shadow: 0 0 0 7px rgba(255, 255, 255, 0.85), 0 0 7px 7px rgba(0, 0, 0, 0.25), inset 0 0 40px 2px rgba(0, 0, 0, 0.25); 
 
    display: none; 
 
} 
 

 
.small { 
 
    display: block; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="magnify"> 
 
    <div class="large"></div> 
 
    <img class="small" src="http://thecodeplayer.com/uploads/media/iphone.jpg" width="200" /> 
 
</div>

私は何をしたいのですが分離されている複数のインスタンスを持っている

hereを示すように偉大な拡大効果があります。私はHTMLのコピーを作ってCSS/JSを微調整しようとしましたが、拡大効果を分離することはできません。両方の効果を複製するか、まったく発生しません。

+0

私が参考にしたコードペインが大好きです。このような素晴らしいアイデアは、バックグラウンドポジションと画像の余分なコピー(完全な解像度)を使って拡大鏡をシミュレートします。とてもかっこいい! – flyer

答えて

3

これを修正するには、DOM内ですべてを同時に取得するのではなく、それぞれ.magnifyインスタンス内の.small.large要素を参照する必要があります。

$(document).ready(function() { 
 
    var native_width = 0; 
 
    var native_height = 0; 
 
    
 
    $(".large").css("background", function() { 
 
    return "url('" + $(this).next(".small").attr("src") + "') no-repeat" 
 
    }); 
 

 
    $(".magnify").mousemove(function(e) { 
 
    // retrieve the related elements here: 
 
    var $this = $(this), 
 
     $small = $this.find('.small'), 
 
     $large = $this.find('.large'); 
 
    
 
    // ... and use them in the below code block: 
 
    if (!native_width && !native_height) { 
 
     var image_object = new Image(); 
 
     image_object.src = $small.attr("src"); 
 
     native_width = image_object.width; 
 
     native_height = image_object.height; 
 
    } else { 
 
     var magnify_offset = $this.offset(); 
 
     var mx = e.pageX - magnify_offset.left; 
 
     var my = e.pageY - magnify_offset.top; 
 

 
     if (mx < $this.width() && my < $this.height() && mx > 0 && my > 0) { 
 
     $large.fadeIn(100); 
 
     } else { 
 
     $large.fadeOut(100); 
 
     } 
 
     if ($large.is(":visible")) { 
 
     var rx = Math.round(mx/$small.width() * native_width - $(".large").width()/2) * -1; 
 
     var ry = Math.round(my/$small.height() * native_height - $(".large").height()/2) * -1; 
 
     var bgp = rx + "px " + ry + "px"; 
 
     var px = mx - $large.width()/2; 
 
     var py = my - $large.height()/2; 
 

 
     $large.css({ 
 
      left: px, 
 
      top: py, 
 
      backgroundPosition: bgp 
 
     }); 
 
     } 
 
    } 
 
    }) 
 
})
* { 
 
    margin: 0; 
 
    padding: 0; 
 
} 
 

 
.magnify { 
 
    width: 200px; 
 
    margin: 50px auto; 
 
    position: relative; 
 
    cursor: none 
 
} 
 

 
.large { 
 
    width: 175px; 
 
    height: 175px; 
 
    position: absolute; 
 
    border-radius: 100%; 
 
    box-shadow: 0 0 0 7px rgba(255, 255, 255, 0.85), 0 0 7px 7px rgba(0, 0, 0, 0.25), inset 0 0 40px 2px rgba(0, 0, 0, 0.25); 
 
    display: none; 
 
    z-index: 10; /* add this to stop the cropping of the magnifier */ 
 
} 
 

 
.small { 
 
    display: block; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="magnify"> 
 
    <div class="large"></div> 
 
    <img class="small" src="http://thecodeplayer.com/uploads/media/iphone.jpg" width="200" /> 
 
</div> 
 

 
<div class="magnify"> 
 
    <div class="large"></div> 
 
    <img class="small" src="http://thecodeplayer.com/uploads/media/iphone.jpg" width="200" /> 
 
</div>

また.large要素のCSSでz-indexの追加を注意:これは好きですが、.magnify要素にmousemoveイベント内$(this).find()を使用することができることを行うには

ズーム可能な要素間を移動するときにクリッピングを停止します。