您好,登錄后才能下訂單哦!
小編給大家分享一下h5實(shí)現(xiàn)放大鏡效果的方法,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
最近公司做的藝術(shù)品放大效果,和以往的淘寶放大鏡效果有些不同,這個(gè)需要在本圖上放大
<html> <head> <meta charset="UTF-8"> <style type="text/css"> *{margin:0;padding:0} .demo{width: 680px;height: 480px;position:relative;background:#333;margin:20px auto;overflow:hidden;} .small_box{position:absolute;/*left:100px;*/top: 0;/*height: 480px;*/} .big_box{width: 100px;height: 100px;border:1px solid #999;background:url(./images/cc.jpg) no-repeat;position:absolute;top: 0;left: 0;border-radius:50%;cursor:none;display:none;box-shadow: 0px 0px 8px #000;} </style> </head> <body> <p id="demo" class="demo"> <img id="small_box" class="small_box" src="images/cc.jpg"/> <p id="big_box" class="big_box"> </p> </p> <script type="text/javascript" src="./js/jquery-1.10.1.min.js"></script> <script type="text/javascript"> $(function(){ (function(a){function d(b){var c=b||window.event,d=[].slice.call(arguments,1),e=0,f=!0,g=0,h=0;return b=a.event.fix(c),b.type="mousewheel",c.wheelDelta&&(e=c.wheelDelta/120),c.detail&&(e=-c.detail/3),h=e,c.axis!==undefined&&c.axis===c.HORIZONTAL_AXIS&&(h=0,g=-1*e),c.wheelDeltaY!==undefined&&(h=c.wheelDeltaY/120),c.wheelDeltaX!==undefined&&(g=-1*c.wheelDeltaX/120),d.unshift(b,e,g,h),(a.event.dispatch||a.event.handle).apply(this,d)}var b=["DOMMouseScroll","mousewheel"];if(a.event.fixHooks)for(var c=b.length;c;)a.event.fixHooks[b[--c]]=a.event.mouseHooks;a.event.special.mousewheel={setup:function(){if(this.addEventListener)for(var a=b.length;a;)this.addEventListener(b[--a],d,!1);else this.onmousewheel=d},teardown:function(){if(this.removeEventListener)for(var a=b.length;a;)this.removeEventListener(b[--a],d,!1);else this.onmousewheel=null}},a.fn.extend({mousewheel:function(a){return a?this.bind("mousewheel",a):this.trigger("mousewheel")},unmousewheel:function(a){return this.unbind("mousewheel",a)}})})(jQuery) // 畫原本的寬高 var origin_img = new Image(); origin_img.src = $('.small_box').attr('src'); var origin_w = origin_img.width; var origin_h = origin_img.height; // 計(jì)算畫縮放的寬高 var now_h = 480; var now_w = (origin_w/origin_h)*now_h; $('.small_box').css({'left':(680 -now_w)/2+'px','height':now_h+'px'}) console.log($('.small_box').offset().left); $('#demo')[0].onmousemove = function(ev){ var _event=ev||window.event; var x = _event.pageX||_event.clientX, y = _event.pageY||_event.clientY; var left=x- $('#big_box').width()/2 - $('.demo').offset().left ; var top=y - $('#big_box').width()/2 - $('.demo').offset().top ; $('#big_box')[0].style.left = left+ 'px'; $('#big_box')[0].style.top = top + 'px'; var percentX = ($('.small_box').offset().left -x)/now_w; var percentY = ($('.small_box').offset().top -y)/now_h; $('.big_box').css({ 'background-position-x':percentX*origin_w + $('#big_box').width()/2+'px', 'background-position-y':percentY*origin_h + $('#big_box').height()/2+'px' }) } // 滾輪滾動(dòng) $('.big_box').on('mousewheel',function(_event,delta){ var x = _event.pageX||_event.clientX, y = _event.pageY||_event.clientY; var left=x- $('#big_box').width()/2 - $('.demo').offset().left ; var top=y - $('#big_box').width()/2 - $('.demo').offset().top ; var targe_w = $(this).width(); var targe_h = $(this).height(); if (delta>0) { targe_w += 3; targe_h += 3; $('#big_box')[0].style.left = left -3+ 'px'; $('#big_box')[0].style.top = top -3 + 'px'; }else{ targe_w -= 3; targe_h -= 3; $('#big_box')[0].style.left = left +3+ 'px'; $('#big_box')[0].style.top = top +3 + 'px'; } $(this).width(targe_w); $(this).height(targe_h); }) //顯示放大鏡 $('.demo').on('mouseover',function(){ $('#big_box').show(); }); $('.demo').on('mouseout',function(){ $('#big_box').hide(); }); }) </script> <br/> <br/> <br/> <br/> <br/> </body> </html>
以上是h5實(shí)現(xiàn)放大鏡效果的方法的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。