使用jquery實現放大鏡效果

來源:互聯網
上載者:User

標籤:

原文:使用jquery實現放大鏡效果

實現原理

首先,我們講解一下放大鏡效果的實現方式:

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

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

上面我們介紹了通過兩種方式實現放大鏡效果,接下來,我們將以上的兩種方式應用到我們的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="/UploadFile/Article/image/20140918/"></script><script type="text/javascript" src="/UploadFile/Article/image/20140918/"></script></head><body> <div class="magnify"><div class="large"></div><img class="small" src="/UploadFile/Article/image/20140918/" width="200" /></div><div class="magnify_02"><div class="large_02"></div><img class="small_02" src="/UploadFile/Article/image/20140918/" 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-propertyhttp://www.sitepoint.com/html5-javascript-mouse-wheel/http://thecodeplayer.com/walkthrough/magnifying-glass-for-images-using-jquery-and-css3
http://www.codes51.com/article/detail_618.html

使用jquery實現放大鏡效果

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.