ホームページ  >  記事  >  ウェブフロントエンド  >  jquery を使用して虫眼鏡効果を実現する_jquery

jquery を使用して虫眼鏡効果を実現する_jquery

WBOY
WBOYオリジナル
2016-05-16 16:38:001489ブラウズ

実装原則

まず、虫眼鏡効果を実現する方法を説明しましょう:

方法 1: 高ピクセルの大きな画像を用意します。元の画像上にマウスを置くと、その大きな画像の対応する位置が読み込まれて表示されます。

方法 2: 元の画像を拡大します。つまり、元の画像の長さと幅を調整します。

上記では、虫眼鏡効果を実現する 2 つの方法を紹介しました。次に、上記の 2 つの方法を jQuery プラグインに適用します。

まず、元の画像オブジェクトを表示するための img 要素が必要です。また、表示ボックスとして大きな画像オブジェクトが保存されるコンテナーが必要です。元の画像上にマウスを移動すると、大きな画像の絶対位置に対応する部分が表示され、虫眼鏡のような効果が得られます。

次に、Index.html ページを定義しましょう。具体的な実装は次のとおりです:

<!DOCTYPE html>
<html>
<head>
<title>放大镜效果</title>
<meta charset="utf-8"/>
<meta name="description" content=""/>
<meta name="keywords" content=""/>
<link type="text/css" rel="stylesheet" href="css/reset.css"/>
<link type="text/css" rel="stylesheet" href="css/main.css"/>
<script type="text/javascript" src="js/jquery-1.11.1.js"></script>
<script type="text/javascript" src="js/jquery.imageZoom.js"></script>


</head>
<body> 
<div class="magnify">
<div class="large"></div>
<img class="small" src="images/iphone.jpg" width="200" />
</div>

<div class="magnify_02">
<div class="large_02"></div>
<img class="small_02" src="images/img5.jpg" width="400"/>
</div>
<script type="text/javascript">
$(function(){

  $(".magnify").hover(function(){
      $.fn.imageZoom({
    small :"small",
    large : "large",
    magnify: "magnify"
    });

  },function(){})

  $(".magnify_02").hover(function(){
    $.fn.imageZoom({
    small : "small_02",
    large : "large_02",
    magnify: "magnify_02"
    });

  },function(){})

})
</script>
</body>
</html>

CSS スタイル:

.magnify {width: 200px; margin: 50px auto; position: relative;}
.large {width: 175px; height: 175px;position: absolute;border-radius: 100%;z-index:99;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);background: url('../images/iphone.jpg') no-repeat;display: none;}
.small { display: block; }

.magnify_02 {width: 400px; margin: 50px auto; position: relative;}
.large_02 {width: 175px; height: 175px;position: absolute;border-radius: 100%;z-index:99;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);background: url('../images/iphone.jpg') no-repeat;display: none;}
.small_02 { display: block; }

mousemove イベント
次に、jQuery プラグインを使用して、虫眼鏡効果を実現します。マウスが小さなオブジェクト上に移動すると、大きな画像の対応する位置が大きなオブジェクトに表示されます。これには、mousemove イベントが含まれます。 Mousemove イベントのリッスン メソッドを実装する必要があります。

jquery.imagezoom.js プラグインを実装します:

(function($) {

  $.fn.imageZoom = function(options) {

    var defaults = {
      scaling: 0.3,
      small :"small",
      large : "large",
      magnify:"magnify"


    };

    options = $.extend(defaults, options),
      native_width = 0,
      native_height = 0,
      current_width = 0,
      current_height = 0,

       magnify="."+options.magnify;

       small="."+options.small;
       $small=$(small);

       large="."+options.large;
       $large=$(large);

     $(magnify).mousemove(function(e) {

        var image_object = new Image();

        image_object.src = $small.attr('src');

      if(!+[1,]) {

        native_height = image_object.height;

        native_width = image_object.width; 

        } 
        else {
          image_object.onload = function() {  
          image_object.onload = null;
          native_height = image_object.height;
          native_width = image_object.width;
          }
        }
        current_height = $small.height();
        current_width = $small.width();
        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,
            ry = Math.round(my / $small.height() * native_height - $large.height() / 2) * -1,
            bgp = rx + "px " + ry + "px",
            px = mx - $large.width() / 2,
            py = my - $large.height() / 2;
          $large.css({
            left: px,
            top: py,
            backgroundPosition: bgp
          });
        }

      //}
    });
  };
})(jQuery);

注: マウスが拡大オブジェクトに移動するとき、拡大におけるマウスの相対座標位置を取得する必要があります。ここでは、相対座標を (mx, my) として定義します。相対座標は (pageX - offsetLeft 、pageY - offsetTop) に等しくなります。

拡大オブジェクト内のマウスの座標値を取得しました。次に、大きな画像の対応する座標を取得する必要があります。ここでは、大きな画像の対応する座標を (rx, ry) として定義します。比例関係を使用して (rx,ry) の値を取得します。

mx / small.width (元の画像の幅) = rx /native_width (大きい画像の幅)

my / small.height (元の画像の長さ) = ry /native_height (大きい画像の長さ)

上記の比例関係により、大きい画像の座標 (rx, ry) は (mx/small.widthnative_width, my/small.heightnative_height) に等しいことがわかります。

マウスホイールイベント
先ほどは、mousemove イベントを使用して画像を拡大しました。ここでは、マウス ホイール イベントを使用して画像の拡大効果を実現します。

ブラウザごとにスクロール ホイール イベントが異なるためです。主に 3 つのタイプがあります: onmousewheel (IE 6/7/8)、mousewheel (IE9、Chrome、Safari、Opera)、および DOMMouseScroll (Firefox でのみサポート) これら 3 つのイベントについては、ここでは詳しく紹介しません。

異なるブラウザ間の違いにより、ブラウザ間の互換性を実現するには、上記の 3 つのホイール イベント (onmousewheel、mousewheel、DOMMouseScroll) を監視する必要があります。具体的な実装は次のとおりです。

$(".magnify").bind('DOMMouseScroll mousewheel onmousewheel', function(e) {
});

上記では、さまざまなブラウザと互換性のあるホイール イベント リスニング方法を実装しました。次に、ホイールが上か下かを判断する際には、さまざまなブラウザ (IE、Opera、Safari) の互換性も考慮する必要があります。 、Firefox、Chrome では)、Firefox は詳細を使用し、他の 4 つのカテゴリは WheelDelta を使用します。これら 2 つは値が一致していないだけです。つまり、detail と WheelDelta はそれぞれ 2 つの値のみを受け取り、detail のみを受け取ります。 ±3、wheelDelta は ±120 のみを取り、正の数値は上向きを表し、負の数値は下向きを表します。

detail と WheelDelta は両方とも上下スクロールを示す 2 つの値を持っているため、異なるブラウザ間の互換性は次の方法で実現できます。具体的な実装は次のとおりです。

$(".magnify").bind('DOMMouseScroll mousewheel onmousewheel', function(e) {

  // cross-browser wheel delta
  var e = window.event || e; // old IE support.
  var delta = Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail)));
});
上記では、ユーザーがホイールをスクロールするたびに、元の画像のサイズを動的に変更する必要があることを説明しました。これは、ユーザーがスクロールするたびに、スケーリング率を 0.3 として定義します。ホイールを回転すると、元の画像は 0.3 の比率に従ってジャストスケールされます。具体的な実装は次のとおりです。

// Gets the image scaling height and width.
native_height += (native_height * scaling * delta);
native_width += (native_width * scaling * delta);

// Update backgroud image size.
$large.css('background-size', native_width + "px " + native_height + "px");
上記では、虫眼鏡効果を実装しました。画像の上にマウスを置くと、画像の対応する部分が自動的に拡大されます。もちろん、スクロール ホイールを使用して拡大率を調整できます。

参考


http://tech.pro/tutorial/681/css-tutorial-the-background-position-property
http://www.sitepoint.com/html5-javascript-mouse-wheel/
http://thecodeplayer.com/walkthrough/magnifying-glass-for-images-using-jquery-and-css3
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。