<label id="odgqy"><dfn id="odgqy"></dfn></label>
    <\/div>\"jQuery實(shí)現(xiàn)放大鏡效果_html\/css_WEB-ITnose\"<\/div><\/body><\/html><\/pre>

    上面,我們定義了small對(duì)象用于顯示原圖,而large對(duì)象作為一個(gè)顯示框用來顯示大圖的對(duì)應(yīng)位置。<\/p> mousemove事件

    接下來,我們通過jQuery插件形式來實(shí)現(xiàn)放大鏡效果,當(dāng)鼠標(biāo)移動(dòng)到small對(duì)象上方時(shí),就會(huì)在large對(duì)象中顯示大圖的對(duì)應(yīng)位置,這就涉及到mousemove事件了,所以,我們需要實(shí)現(xiàn)mousemove事件的監(jiān)聽方法(如何定義jQuery插件可以參考《自定義jQuery插件Step by Step》)。<\/p>

    現(xiàn)在,讓我們實(shí)現(xiàn)jquery.imagezoom.js插件吧!<\/p>

    ;(function ($) {    $.fn.imageZoom = function (options) {    \/\/ The native width and height of the image.    var native_width = 0,        native_height = 0,        current_width = 0,        current_height = 0,        $small = $(\".small\"),        $large = $(\".large\");    $(\".magnify\").mousemove(function (e) {        \/* Act on the event *\/        if (!native_width && !native_height) {            var image_object = new Image();            image_object.src = $small.attr('src');            \/\/ Gets the image native height and width.            native_height = image_object.height;            native_width = image_object.width;            \/\/ Gets the image current height and width.            current_height = $small.height();            current_width = $small.width();        } else {            \/\/ Gets .maginfy offset coordinates.            var magnify_offset = $(this).offset(),                \/\/ Gets coordinates within .maginfy.                mx = e.pageX - magnify_offset.left,                my = e.pageY - magnify_offset.top;            \/\/ Checks the mouse within .maginfy or not.            if (mx < $(this).width() && my < $(this).height() && mx > 0 && my > 0) {                $large.fadeIn(100);            } else {                $large.fadeOut(100);            } if ($large.is(\":visible\")) {                \/* Gets the large image coordinate by ratio                    small.x \/ small.width = large.x \/ large.width                   small.y \/ small.height = large.y \/ large.height                   then we need to keep pointer in the centre,                    so deduct the half of .large width and height.                *\/                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                });            }        }    });});<\/pre>   

    上面,我實(shí)現(xiàn)了mousemove事件的監(jiān)聽方法,當(dāng)鼠標(biāo)移動(dòng)到magnify對(duì)象中,我們需要獲取當(dāng)前鼠標(biāo)的相對(duì)坐標(biāo)位置,下面我們通過圖片講解如何獲取鼠標(biāo)的相對(duì)坐標(biāo)位置。<\/p> 相對(duì)坐標(biāo)

       <\/p>

    圖1鼠標(biāo)相對(duì)坐標(biāo)位置<\/p>

    當(dāng)鼠標(biāo)移動(dòng)到magnify對(duì)象中,我們需要獲取鼠標(biāo)在magnify中的相對(duì)坐標(biāo)位置,這里我們把相對(duì)坐標(biāo)定義為(mx,my),通過上圖我們知道相對(duì)坐標(biāo)等于(pageX - offsetLeft, pageY - offsetTop)。<\/p>

    現(xiàn)在,我們已經(jīng)獲取鼠標(biāo)在magnify對(duì)象中的坐標(biāo)值,接下來,需要獲取對(duì)應(yīng)大圖的相應(yīng)坐標(biāo),這里我們把大圖的對(duì)應(yīng)坐標(biāo)定義為(rx,ry),我們可以通過比例關(guān)系獲取(rx,ry)的值。<\/p>

    mx \/ small.width (原圖的寬)= rx \/ native_width(大圖的寬)<\/p>

    my \/ small.height (原圖的長(zhǎng))= ry \/ native_height(大圖的長(zhǎng))<\/p>

    通過上面的比例關(guān)系,我們知道大圖的坐標(biāo)(rx,ry)等于(mx\/small.width*native_width, my\/small.height*native_height)。<\/p>

    通過上述的公式,我們可以獲取大圖對(duì)應(yīng)坐標(biāo)位置,當(dāng)鼠標(biāo)移動(dòng)到magnify對(duì)象中就顯示對(duì)應(yīng)位置的大圖部位,接下來我們需要實(shí)現(xiàn)大圖的加載實(shí)現(xiàn)了。<\/p> background-position屬性

    在實(shí)現(xiàn)大圖加載顯示之前,首先介紹CSS中背景定位background-position的知識(shí)。<\/p>

    <\/p>

    <\/p>

    <\/p>

    圖2 CSS background-position<\/p>

    上面,有一個(gè)100x100像素的圖片它由四種顏色組成,而且每種顏色占50 x50像素,接下來,我們將通過修改該圖片CSS的background-position屬性值來顯示該圖片的不同位置。<\/p>

    <\/p>

    我們看到在大正方形下有兩行小正方形,它們顯示的顏色位置都不相同,這里我們通過修改每個(gè)div元素CSS的background-position屬性值實(shí)現(xiàn)的。<\/p>

    例如:第一行的藍(lán)色方形,我們?cè)O(shè)置CSS的background-position屬性為:0px -50px;這相當(dāng)于原圖往上移動(dòng)50px,第一行的其他方形也通過左右和上下移動(dòng)實(shí)現(xiàn)的。<\/p>

    但第二行的方形就顯得更加奇怪了,因?yàn)樗鼈兌加伤姆N顏色組成,而且顏色的位置都不一樣,這究竟是怎樣實(shí)現(xiàn)的呢?<\/p>

    例如:第二行的第一個(gè)方形,我們?cè)O(shè)置CSS的background-position屬性為:25px 25px;這相當(dāng)于原圖向下和向右移動(dòng)了25px,由于image wrap的作用它會(huì)填充剩余位置的顏色。<\/p>

    現(xiàn)在,我們已經(jīng)了解到了CSS的background-position屬性的作用,所以我們通過修改large對(duì)象的background-position屬性來顯示對(duì)應(yīng)的圖像部分,具體實(shí)現(xiàn)如下:<\/p>

    $large.css({    left: px,    top: py,    backgroundPosition: bgp});<\/pre>   

    上面,我們通過加載大圖的方式來實(shí)現(xiàn)放大鏡效果,接下來,我們將介紹通過調(diào)整原圖的長(zhǎng)和寬來實(shí)現(xiàn)放大鏡效果。<\/p> mousewheel事件

    前面,我們通過mousemove事件來放大圖片,這里我們將通過鼠標(biāo)的滾輪事件實(shí)現(xiàn)圖片放大效果。<\/p>

    由于,不同的瀏覽器有不同的滾輪事件。主要是有三種:onmousewheel(IE 6\/7\/8)、mousewheel(IE9,Chrome,Safari和Opera)和DOMMouseScroll(只有Firefox支持),關(guān)于這三個(gè)事件這里不做詳細(xì)的介紹了。<\/p>

    由于不同瀏覽器之間存在著差異,為了實(shí)現(xiàn)瀏覽器之間的兼容,所以,我們需要監(jiān)聽以上三種滾輪事件(onmousewheel,mousewheel和DOMMouseScroll),具體實(shí)現(xiàn)如下:<\/p>

    $(\".magnify\").bind('DOMMouseScroll mousewheel onmousewheel', function(e) {});<\/pre>   

    上面,我們實(shí)現(xiàn)了兼容不同瀏覽器的滾輪事件監(jiān)聽方法,接下來,判斷滾輪向上或向下也要考慮不同瀏覽器的兼容性,主流的覽器(IE、Opera、Safari、Firefox、Chrome)中Firefox 使用detail,其余四類使用wheelDelta;兩者只在取值上不一致,代表含義一致,detail與wheelDelta只各取兩個(gè)值,detail只取±3,wheelDelta只取±120,其中正數(shù)表示為向上,負(fù)數(shù)表示向下。<\/p>

    由于detail和wheelDelta都有兩個(gè)值表示向上或向下滾動(dòng),所以不同瀏覽器間可以通過以下方式實(shí)現(xiàn)兼容,具體實(shí)現(xiàn)如下:<\/p>

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

    上面,我們已經(jīng)處理了不同瀏覽器滾輪監(jiān)聽方法,當(dāng)用戶滾動(dòng)滾輪時(shí)需要?jiǎng)討B(tài)地修改原圖的尺寸,這里我們定義縮放比scaling為0.3,也就是說每當(dāng)用戶滾動(dòng)一下滾輪原圖就按0.3的比例進(jìn)行縮放,具體實(shí)現(xiàn)如下:<\/p>

    \/\/ 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\");<\/pre>   

    現(xiàn)在,我們已經(jīng)實(shí)現(xiàn)了通過滾輪對(duì)圖片進(jìn)行縮放查看的效果,完整的實(shí)現(xiàn)如下:<\/p>

    \/************************************ Author: Jackson Huang* Blog: http:\/\/www.cnblogs.com\/rush* Date: 8\/23\/2013* Reference:* http:\/\/www.sitepoint.com\/html5-javascript-mouse-wheel\/* http:\/\/thecodeplayer.com\/walkthrough\/magnifying-glass-for-images-using-jquery-and-css3***********************************\/;(function($) {    $.fn.imageZoom = function(options) {        \/\/ The native width and height of the image.        var defaults = {            scaling: 0.3        };        \/\/ Combines object defaults and options.        options = $.extend(defaults, options),            native_width = 0,            native_height = 0,            current_width = 0,            current_height = 0,            $small = $(\".small\"),            $large = $(\".large\");        $(\".magnify\").mousemove(function(e) {            \/* Act on the event *\/            if (!native_width && !native_height) {                var image_object = new Image();                image_object.src = $small.attr('src');                \/\/ Gets the image native height and width.                native_height = image_object.height;                native_width = image_object.width;                \/\/ Gets the image current height and width.                current_height = $small.height();                current_width = $small.width();            } else {                \/\/ Gets .maginfy offset coordinates.                var magnify_offset = $(this).offset(),                \/\/ Gets coordinates within .maginfy.                    mx = e.pageX - magnify_offset.left,                    my = e.pageY - magnify_offset.top;                \/\/ Checks the mouse within .maginfy or not.                if (mx < $(this).width() && my < $(this).height() && mx > 0 && my > 0) {                    $large.fadeIn(100);                } else {                    $large.fadeOut(100);                }                if ($large.is(\":visible\")) {                    \/* Gets the large image coordinate by ratio                     small.x \/ small.width = large.x \/ large.width                    small.y \/ small.height = large.y \/ large.height                    then we need to keep pointer in the centre,                     so deduct the half of .large width and height.                    *\/                    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                    });                }            }        });        $(\".magnify\").bind('DOMMouseScroll mousewheel onmousewheel', function(e) {            var image_object = new Image();            image_object.src = $large.attr('src');            \/\/ cross-browser wheel delta            e = window.event || e; \/\/ old IE support.            var delta = Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail)));            \/\/ Gets the image scaling height and width.            native_height += (native_height * defaults.scaling * delta);            native_width += (native_width * defaults.scaling * delta);            \/\/ The image can't smaller than the original.            if (native_height < current_height) {                native_height = current_height;            }            if (native_width < current_width) {                native_width = current_width;            }            \/\/ console.log(\"native_height: \" + native_height + \" native_width: \" + native_width);            \/\/ Gets .maginfy offset coordinates.            var magnify_offset = $(this).offset(),                mx = e.pageX - magnify_offset.left,                my = e.pageY - magnify_offset.top;            \/\/ Update backgroud image size.            $large.css('background-size', native_width + \"px \" + native_height + \"px\");            \/* Gets the large image coordinate by ratio             small.x \/ small.width = large.x \/ large.width            small.y \/ small.height = large.y \/ large.height            then we need to keep pointer in the centre,             so deduct the half of .large width and height.            *\/            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);<\/pre>   

    ?<\/p>

    圖3 放大鏡效果<\/p>

    上面,我們實(shí)現(xiàn)了放大鏡效果,當(dāng)我們鼠標(biāo)停留在圖片上方會(huì)自動(dòng)放大圖片的相應(yīng)部位,當(dāng)然我們可以通過滾輪調(diào)整放大的比例。<\/p> 1.1.3 總結(jié)

    在本博文中,我們介紹了如何實(shí)現(xiàn)放大鏡效果,總的來說,我們可以通過兩種方式實(shí)現(xiàn)放大鏡效果,而且在博文中都給出了詳細(xì)的介紹,通過mousemove事件實(shí)現(xiàn)加載大圖的效果,mousewheel事件實(shí)現(xiàn)動(dòng)態(tài)修改原圖的尺寸。<\/p>

    這只是一個(gè)簡(jiǎn)單的程序,我們還有很大的改善空間,提供一個(gè)內(nèi)容豐富和功能強(qiáng)大的程序是我們的目標(biāo)。<\/p> 參考 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

    Demo<\/p> "}

    国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

    首頁 web前端 html教學(xué) jQuery實(shí)現(xiàn)放大鏡效果_html/css_WEB-ITnose

    jQuery實(shí)現(xiàn)放大鏡效果_html/css_WEB-ITnose

    Jun 24, 2016 pm 12:33 PM

    1.1.1 摘要

    相信大家都見過或使用過放大鏡效果,甚至實(shí)現(xiàn)過該效果,它一般應(yīng)用于放大查看商品圖片,一些電商網(wǎng)站(例如:凡客,京東商城,阿里巴巴等)都有類似的圖片查看效果。

    在接下來的博文中,我們將向大家介紹通過jQuery實(shí)現(xiàn)放大鏡效果。

    目錄 實(shí)現(xiàn)原理 mousemove事件 相對(duì)坐標(biāo) background-position屬性 mousewheel事件 1.1.2 正文 實(shí)現(xiàn)原理

    首先,我們講解一下放大鏡效果的實(shí)現(xiàn)方式:

    方法一:準(zhǔn)備一張高像素的大圖,當(dāng)鼠標(biāo)放到原圖上,加載顯示大圖的對(duì)應(yīng)位置。

    方法二:對(duì)原圖片進(jìn)行放大,也就是調(diào)整原圖的長(zhǎng)和寬。

    上面我們介紹了通過兩種方式實(shí)現(xiàn)放大鏡效果,接下來,我們將以上的兩種方式應(yīng)用到我們的jQuery插件中。

    首先,我們需要一個(gè)img元素顯示原圖對(duì)象,還需要一個(gè)容器作為顯示框;顯示框里面存放大圖對(duì)象。當(dāng)鼠標(biāo)移動(dòng)到原圖上時(shí),通過對(duì)大圖進(jìn)行絕對(duì)定位來顯示對(duì)應(yīng)的部位,實(shí)現(xiàn)類似放大鏡的效果。

    接下來,讓我們定義Index.html頁面,具體實(shí)現(xiàn)如下:

    <!doctype html><html lang="en-US"><head>  <meta http-equiv="Content-Type" content="text/html;charset=utf-8">  <title>jQuery Image Zoom Demo</title>  <meta name="author" content="Jackson Huang"></head><body><div class="magnify"><div class="large"></div><img  class="small lazy"  src="/static/imghw/default1.png"  data-src="./img/1.jpg"     style="max-width:90%" / alt="jQuery實(shí)現(xiàn)放大鏡效果_html/css_WEB-ITnose" ></div></body></html>

    上面,我們定義了small對(duì)象用于顯示原圖,而large對(duì)象作為一個(gè)顯示框用來顯示大圖的對(duì)應(yīng)位置。

    mousemove事件

    接下來,我們通過jQuery插件形式來實(shí)現(xiàn)放大鏡效果,當(dāng)鼠標(biāo)移動(dòng)到small對(duì)象上方時(shí),就會(huì)在large對(duì)象中顯示大圖的對(duì)應(yīng)位置,這就涉及到mousemove事件了,所以,我們需要實(shí)現(xiàn)mousemove事件的監(jiān)聽方法(如何定義jQuery插件可以參考《自定義jQuery插件Step by Step》)。

    現(xiàn)在,讓我們實(shí)現(xiàn)jquery.imagezoom.js插件吧!

    ;(function ($) {    $.fn.imageZoom = function (options) {    // The native width and height of the image.    var native_width = 0,        native_height = 0,        current_width = 0,        current_height = 0,        $small = $(".small"),        $large = $(".large");    $(".magnify").mousemove(function (e) {        /* Act on the event */        if (!native_width && !native_height) {            var image_object = new Image();            image_object.src = $small.attr('src');            // Gets the image native height and width.            native_height = image_object.height;            native_width = image_object.width;            // Gets the image current height and width.            current_height = $small.height();            current_width = $small.width();        } else {            // Gets .maginfy offset coordinates.            var magnify_offset = $(this).offset(),                // Gets coordinates within .maginfy.                mx = e.pageX - magnify_offset.left,                my = e.pageY - magnify_offset.top;            // Checks the mouse within .maginfy or not.            if (mx < $(this).width() && my < $(this).height() && mx > 0 && my > 0) {                $large.fadeIn(100);            } else {                $large.fadeOut(100);            } if ($large.is(":visible")) {                /* Gets the large image coordinate by ratio                    small.x / small.width = large.x / large.width                   small.y / small.height = large.y / large.height                   then we need to keep pointer in the centre,                    so deduct the half of .large width and height.                */                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                });            }        }    });});

    上面,我實(shí)現(xiàn)了mousemove事件的監(jiān)聽方法,當(dāng)鼠標(biāo)移動(dòng)到magnify對(duì)象中,我們需要獲取當(dāng)前鼠標(biāo)的相對(duì)坐標(biāo)位置,下面我們通過圖片講解如何獲取鼠標(biāo)的相對(duì)坐標(biāo)位置。

    相對(duì)坐標(biāo)

    圖1鼠標(biāo)相對(duì)坐標(biāo)位置

    當(dāng)鼠標(biāo)移動(dòng)到magnify對(duì)象中,我們需要獲取鼠標(biāo)在magnify中的相對(duì)坐標(biāo)位置,這里我們把相對(duì)坐標(biāo)定義為(mx,my),通過上圖我們知道相對(duì)坐標(biāo)等于(pageX - offsetLeft, pageY - offsetTop)。

    現(xiàn)在,我們已經(jīng)獲取鼠標(biāo)在magnify對(duì)象中的坐標(biāo)值,接下來,需要獲取對(duì)應(yīng)大圖的相應(yīng)坐標(biāo),這里我們把大圖的對(duì)應(yīng)坐標(biāo)定義為(rx,ry),我們可以通過比例關(guān)系獲取(rx,ry)的值。

    mx / small.width (原圖的寬)= rx / native_width(大圖的寬)

    my / small.height (原圖的長(zhǎng))= ry / native_height(大圖的長(zhǎng))

    通過上面的比例關(guān)系,我們知道大圖的坐標(biāo)(rx,ry)等于(mx/small.width*native_width, my/small.height*native_height)。

    通過上述的公式,我們可以獲取大圖對(duì)應(yīng)坐標(biāo)位置,當(dāng)鼠標(biāo)移動(dòng)到magnify對(duì)象中就顯示對(duì)應(yīng)位置的大圖部位,接下來我們需要實(shí)現(xiàn)大圖的加載實(shí)現(xiàn)了。

    background-position屬性

    在實(shí)現(xiàn)大圖加載顯示之前,首先介紹CSS中背景定位background-position的知識(shí)。

    圖2 CSS background-position

    上面,有一個(gè)100x100像素的圖片它由四種顏色組成,而且每種顏色占50 x50像素,接下來,我們將通過修改該圖片CSS的background-position屬性值來顯示該圖片的不同位置。

    我們看到在大正方形下有兩行小正方形,它們顯示的顏色位置都不相同,這里我們通過修改每個(gè)div元素CSS的background-position屬性值實(shí)現(xiàn)的。

    例如:第一行的藍(lán)色方形,我們?cè)O(shè)置CSS的background-position屬性為:0px -50px;這相當(dāng)于原圖往上移動(dòng)50px,第一行的其他方形也通過左右和上下移動(dòng)實(shí)現(xiàn)的。

    但第二行的方形就顯得更加奇怪了,因?yàn)樗鼈兌加伤姆N顏色組成,而且顏色的位置都不一樣,這究竟是怎樣實(shí)現(xiàn)的呢?

    例如:第二行的第一個(gè)方形,我們?cè)O(shè)置CSS的background-position屬性為:25px 25px;這相當(dāng)于原圖向下和向右移動(dòng)了25px,由于image wrap的作用它會(huì)填充剩余位置的顏色。

    現(xiàn)在,我們已經(jīng)了解到了CSS的background-position屬性的作用,所以我們通過修改large對(duì)象的background-position屬性來顯示對(duì)應(yīng)的圖像部分,具體實(shí)現(xiàn)如下:

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

    上面,我們通過加載大圖的方式來實(shí)現(xiàn)放大鏡效果,接下來,我們將介紹通過調(diào)整原圖的長(zhǎng)和寬來實(shí)現(xiàn)放大鏡效果。

    mousewheel事件

    前面,我們通過mousemove事件來放大圖片,這里我們將通過鼠標(biāo)的滾輪事件實(shí)現(xiàn)圖片放大效果。

    由于,不同的瀏覽器有不同的滾輪事件。主要是有三種:onmousewheel(IE 6/7/8)、mousewheel(IE9,Chrome,Safari和Opera)和DOMMouseScroll(只有Firefox支持),關(guān)于這三個(gè)事件這里不做詳細(xì)的介紹了。

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

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

    上面,我們實(shí)現(xiàn)了兼容不同瀏覽器的滾輪事件監(jiān)聽方法,接下來,判斷滾輪向上或向下也要考慮不同瀏覽器的兼容性,主流的覽器(IE、Opera、Safari、Firefox、Chrome)中Firefox 使用detail,其余四類使用wheelDelta;兩者只在取值上不一致,代表含義一致,detail與wheelDelta只各取兩個(gè)值,detail只取±3,wheelDelta只取±120,其中正數(shù)表示為向上,負(fù)數(shù)表示向下。

    由于detail和wheelDelta都有兩個(gè)值表示向上或向下滾動(dòng),所以不同瀏覽器間可以通過以下方式實(shí)現(xiàn)兼容,具體實(shí)現(xiàn)如下:

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

    上面,我們已經(jīng)處理了不同瀏覽器滾輪監(jiān)聽方法,當(dāng)用戶滾動(dòng)滾輪時(shí)需要?jiǎng)討B(tài)地修改原圖的尺寸,這里我們定義縮放比scaling為0.3,也就是說每當(dāng)用戶滾動(dòng)一下滾輪原圖就按0.3的比例進(jìn)行縮放,具體實(shí)現(xiàn)如下:

    // 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");

    現(xiàn)在,我們已經(jīng)實(shí)現(xiàn)了通過滾輪對(duì)圖片進(jìn)行縮放查看的效果,完整的實(shí)現(xiàn)如下:

    /************************************ Author: Jackson Huang* Blog: http://www.cnblogs.com/rush* Date: 8/23/2013* Reference:* http://www.sitepoint.com/html5-javascript-mouse-wheel/* http://thecodeplayer.com/walkthrough/magnifying-glass-for-images-using-jquery-and-css3***********************************/;(function($) {    $.fn.imageZoom = function(options) {        // The native width and height of the image.        var defaults = {            scaling: 0.3        };        // Combines object defaults and options.        options = $.extend(defaults, options),            native_width = 0,            native_height = 0,            current_width = 0,            current_height = 0,            $small = $(".small"),            $large = $(".large");        $(".magnify").mousemove(function(e) {            /* Act on the event */            if (!native_width && !native_height) {                var image_object = new Image();                image_object.src = $small.attr('src');                // Gets the image native height and width.                native_height = image_object.height;                native_width = image_object.width;                // Gets the image current height and width.                current_height = $small.height();                current_width = $small.width();            } else {                // Gets .maginfy offset coordinates.                var magnify_offset = $(this).offset(),                // Gets coordinates within .maginfy.                    mx = e.pageX - magnify_offset.left,                    my = e.pageY - magnify_offset.top;                // Checks the mouse within .maginfy or not.                if (mx < $(this).width() && my < $(this).height() && mx > 0 && my > 0) {                    $large.fadeIn(100);                } else {                    $large.fadeOut(100);                }                if ($large.is(":visible")) {                    /* Gets the large image coordinate by ratio                     small.x / small.width = large.x / large.width                    small.y / small.height = large.y / large.height                    then we need to keep pointer in the centre,                     so deduct the half of .large width and height.                    */                    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                    });                }            }        });        $(".magnify").bind('DOMMouseScroll mousewheel onmousewheel', function(e) {            var image_object = new Image();            image_object.src = $large.attr('src');            // cross-browser wheel delta            e = window.event || e; // old IE support.            var delta = Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail)));            // Gets the image scaling height and width.            native_height += (native_height * defaults.scaling * delta);            native_width += (native_width * defaults.scaling * delta);            // The image can't smaller than the original.            if (native_height < current_height) {                native_height = current_height;            }            if (native_width < current_width) {                native_width = current_width;            }            // console.log("native_height: " + native_height + " native_width: " + native_width);            // Gets .maginfy offset coordinates.            var magnify_offset = $(this).offset(),                mx = e.pageX - magnify_offset.left,                my = e.pageY - magnify_offset.top;            // Update backgroud image size.            $large.css('background-size', native_width + "px " + native_height + "px");            /* Gets the large image coordinate by ratio             small.x / small.width = large.x / large.width            small.y / small.height = large.y / large.height            then we need to keep pointer in the centre,             so deduct the half of .large width and height.            */            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);

    ?

    圖3 放大鏡效果

    上面,我們實(shí)現(xiàn)了放大鏡效果,當(dāng)我們鼠標(biāo)停留在圖片上方會(huì)自動(dòng)放大圖片的相應(yīng)部位,當(dāng)然我們可以通過滾輪調(diào)整放大的比例。

    1.1.3 總結(jié)

    在本博文中,我們介紹了如何實(shí)現(xiàn)放大鏡效果,總的來說,我們可以通過兩種方式實(shí)現(xiàn)放大鏡效果,而且在博文中都給出了詳細(xì)的介紹,通過mousemove事件實(shí)現(xiàn)加載大圖的效果,mousewheel事件實(shí)現(xiàn)動(dòng)態(tài)修改原圖的尺寸。

    這只是一個(gè)簡(jiǎn)單的程序,我們還有很大的改善空間,提供一個(gè)內(nèi)容豐富和功能強(qiáng)大的程序是我們的目標(biāo)。

    參考 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

    Demo

    本網(wǎng)站聲明
    本文內(nèi)容由網(wǎng)友自願(yuàn)投稿,版權(quán)歸原作者所有。本站不承擔(dān)相應(yīng)的法律責(zé)任。如發(fā)現(xiàn)涉嫌抄襲或侵權(quán)的內(nèi)容,請(qǐng)聯(lián)絡(luò)admin@php.cn

    熱AI工具

    Undress AI Tool

    Undress AI Tool

    免費(fèi)脫衣圖片

    Undresser.AI Undress

    Undresser.AI Undress

    人工智慧驅(qū)動(dòng)的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片

    AI Clothes Remover

    AI Clothes Remover

    用於從照片中去除衣服的線上人工智慧工具。

    Clothoff.io

    Clothoff.io

    AI脫衣器

    Video Face Swap

    Video Face Swap

    使用我們完全免費(fèi)的人工智慧換臉工具,輕鬆在任何影片中換臉!

    熱工具

    記事本++7.3.1

    記事本++7.3.1

    好用且免費(fèi)的程式碼編輯器

    SublimeText3漢化版

    SublimeText3漢化版

    中文版,非常好用

    禪工作室 13.0.1

    禪工作室 13.0.1

    強(qiáng)大的PHP整合開發(fā)環(huán)境

    Dreamweaver CS6

    Dreamweaver CS6

    視覺化網(wǎng)頁開發(fā)工具

    SublimeText3 Mac版

    SublimeText3 Mac版

    神級(jí)程式碼編輯軟體(SublimeText3)

    我如何了解最新的HTML標(biāo)準(zhǔn)和最佳實(shí)踐? 我如何了解最新的HTML標(biāo)準(zhǔn)和最佳實(shí)踐? Jun 20, 2025 am 08:33 AM

    要跟上HTML標(biāo)準(zhǔn)和最佳實(shí)踐,關(guān)鍵在於有意為之而非盲目追隨。首先,關(guān)注官方來源如WHATWG和W3C的摘要或更新日誌,了解新標(biāo)籤(如)和屬性,將其作為參考解決疑難問題;其次,訂閱可信的網(wǎng)頁開發(fā)新聞通訊和博客,每週花10-15分鐘瀏覽更新,關(guān)注實(shí)際用例而非僅收藏文章;再次,使用開發(fā)者工具和linters如HTMLHint,通過即時(shí)反饋優(yōu)化代碼結(jié)構(gòu);最後,與開發(fā)者社區(qū)互動(dòng),分享經(jīng)驗(yàn)並學(xué)習(xí)他人實(shí)戰(zhàn)技巧,從而持續(xù)提升HTML技能。

    如何使用元素來表示文檔的主要內(nèi)容? 如何使用元素來表示文檔的主要內(nèi)容? Jun 19, 2025 pm 11:09 PM

    使用標(biāo)籤的原因是提升網(wǎng)頁的語義化結(jié)構(gòu)和可訪問性,使屏幕閱讀器和搜索引擎更易理解頁面內(nèi)容,並允許用戶快速跳轉(zhuǎn)至核心內(nèi)容。以下是關(guān)鍵要點(diǎn):1.每個(gè)頁面應(yīng)僅包含一個(gè)元素;2.不應(yīng)包括跨頁面重複的內(nèi)容(如側(cè)邊欄或頁腳);3.可與ARIA屬性結(jié)合使用以增強(qiáng)無障礙體驗(yàn)。通常位於和之後、之前,用於包裹唯一的頁面內(nèi)容,例如文章、表單或產(chǎn)品詳情,並應(yīng)避免嵌套在、或中;為提高輔助功能,可使用aria-labelledby或aria-label明確標(biāo)識(shí)部分。

    如何創(chuàng)建基本的HTML文檔? 如何創(chuàng)建基本的HTML文檔? Jun 19, 2025 pm 11:01 PM

    要?jiǎng)?chuàng)建一個(gè)基本的HTML文檔,首先需要了解其基本結(jié)構(gòu)並按照標(biāo)準(zhǔn)格式編寫代碼。 1.開始時(shí)使用聲明文檔類型;2.使用標(biāo)籤包裹整個(gè)內(nèi)容;3.在其中包含和兩個(gè)主要部分,用於存放元數(shù)據(jù)如標(biāo)題、樣式錶鍊接等,而則包含用戶可見的內(nèi)容如標(biāo)題、段落、圖片和鏈接;4.保存文件為.html格式並在瀏覽器中打開查看效果;5.隨後可逐步添加更多元素以豐富頁面內(nèi)容。遵循這些步驟即可快速構(gòu)建一個(gè)基礎(chǔ)網(wǎng)頁。

    如何使用 如何使用 Jun 19, 2025 pm 11:41 PM

    要?jiǎng)?chuàng)建HTML複選框,需使用type屬性設(shè)為checkbox的元素。 1.基本結(jié)構(gòu)包含id、name和label標(biāo)籤,確保點(diǎn)擊文字可切換選項(xiàng);2.多個(gè)相關(guān)複選框應(yīng)使用相同name但不同value,並用fieldset包裹提升可訪問性;3.自定義樣式時(shí)隱藏原生控件並用CSS設(shè)計(jì)替代元素,同時(shí)保持功能完整;4.確保可用性,配對(duì)label、支持鍵盤導(dǎo)航且避免僅依賴視覺提示。以上步驟能幫助開發(fā)者正確實(shí)現(xiàn)兼具功能與美觀的複選框組件。

    如何使用元素代表文檔或部分的頁腳? 如何使用元素代表文檔或部分的頁腳? Jun 25, 2025 am 12:57 AM

    是HTML5中用於定義頁面或內(nèi)容區(qū)塊底部的語義化標(biāo)籤,通常包含版權(quán)信息、聯(lián)繫方式或?qū)Ш芥溄拥龋凰芍渺俄撁娴撞炕蚯短自?、等?biāo)籤內(nèi)作為區(qū)塊尾部;使用時(shí)應(yīng)注意避免重複濫用及放入無關(guān)內(nèi)容。

    隨著時(shí)間的流逝,HTML如何發(fā)展,其歷史上的關(guān)鍵里程碑是什麼? 隨著時(shí)間的流逝,HTML如何發(fā)展,其歷史上的關(guān)鍵里程碑是什麼? Jun 24, 2025 am 12:54 AM

    htmlhasevolvedscreatscreationtomeetthegrowingdemandsofwebdevelopersandusers.inatelyallyasimplemarkuplanguageforsharingdocuments,ithasundergonemajorupdates,包括html.2.0,包括wheintrodistusefforms;

    如何使用Tabindex屬性來控制元素的選項(xiàng)卡順序? 如何使用Tabindex屬性來控制元素的選項(xiàng)卡順序? Jun 24, 2025 am 12:56 AM

    ThetabindexattributecontrolshowelementsreceivefocusviatheTabkey,withthreemainvalues:tabindex="0"addsanelementtothenaturaltaborder,tabindex="-1"allowsprogrammaticfocusonly,andtabindex="n"(positivenumber)setsacustomtabbing

    See all articles