首頁  >  文章  >  web前端  >  使用jquery實現放大鏡效果_jquery

使用jquery實現放大鏡效果_jquery

WBOY
WBOY原創
2016-05-16 16:38:001489瀏覽

實作原理

首先,我們先來解釋放大鏡效果的實現方式:

方法一:準備一張高像素的大圖,當滑鼠放到原圖上,載入顯示大圖的對應位置。

方法二:將原圖片放大,也就是調整原圖的長度和寬度。

上面我們介紹了透過兩種方式來實現放大鏡效果,接下來,我們將以上的兩種方式應用到我們的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插件形式來實現放大鏡效果,當滑鼠移動到small物件上方時,就會在large物件中顯示大圖的對應位置,這就涉及到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);

註解:當滑鼠移到magnify物件中,我們需要取得滑鼠在magnify中的相對座標位置,這裡我們把相對座標定義為(mx,my),透過上圖我們知道相對座標等於(pageX - offsetLeft , pageY - offsetTop)。

現在,我們已經取得滑鼠在magnify物件中的座標值,接下來,需要取得對應大圖的對應座標,這裡我們把大圖的對應座標定義為(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)。

mousewheel事件
前面,我們透過mousemove事件來放大圖片,這裡我們將透過滑鼠的滾輪事件實現圖片放大效果。

由於,不同的瀏覽器有不同的滾輪事件。主要有三種:onmousewheel(IE 6/7/8)、mousewheel(IE9,Chrome,Safari和Opera)和DOMMouseScroll(只有Firefox支援),關於這三個事件這裡不做詳細的介紹了。

由於不同瀏覽器之間存在差異,為了實現瀏覽器之間的兼容,所以,我們需要監聽以上三種滾輪事件(onmousewheel,mousewheel和DOMMouseScroll),具體實現如下:

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

上面,我們實作了與不同瀏覽器相容的滾輪事件監聽方法,接下來,判斷滾筒向上或向下也要考慮不同瀏覽器的相容性,主流的覽器(IE、Opera、Safari、Firefox、 Chrome)中Firefox 使用detail,其餘四類使用wheelDelta;兩者只在取值上不一致,代表意義一致,detail與wheelDelta只各取兩個值,detail只取±​​3,wheelDelta只取±120,其中正數表示為向上,負數表示向下。

由於detail和wheelDelta都有兩個值表示向上或向下滾動,所以不同瀏覽器間可以透過以下方式實現相容,具體實現如下:

$(".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)));
});

上面,我們已經處理了不同瀏覽器滾輪監聽方法,當使用者滾動滾輪時需要動態地修改原圖的尺寸,這裡我們定義縮放比scaling為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