PHP前端开发

h5实现放大镜效果的代码

百变鹏仔 2个月前 (10-18) #H5教程
文章标签 放大镜

这篇文章分享给大家的内容是关于h5实现放大镜效果的代码,内容很有参考价值,希望可以帮到有需要的小伙伴。

最近公司做的艺术品放大效果,和以往的淘宝放大镜效果有些不同,这个需要在本图上放大

  <meta>  <style>    *{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>  <p>    @@##@@    </p><p>    </p>    <script></script>  <script>    $(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 = $(&#39;.small_box&#39;).attr(&#39;src&#39;);      var origin_w = origin_img.width;      var origin_h = origin_img.height;      // 计算画缩放的宽高      var now_h = 480;      var now_w = (origin_w/origin_h)*now_h;      $(&#39;.small_box&#39;).css({&#39;left&#39;:(680 -now_w)/2+&#39;px&#39;,&#39;height&#39;:now_h+&#39;px&#39;})      console.log($(&#39;.small_box&#39;).offset().left);      $(&#39;#demo&#39;)[0].onmousemove = function(ev){        var _event=ev||window.event;        var x = _event.pageX||_event.clientX,            y = _event.pageY||_event.clientY;        var left=x- $(&#39;#big_box&#39;).width()/2 - $(&#39;.demo&#39;).offset().left ;        var top=y - $(&#39;#big_box&#39;).width()/2 - $(&#39;.demo&#39;).offset().top ;        $(&#39;#big_box&#39;)[0].style.left = left+ &#39;px&#39;;        $(&#39;#big_box&#39;)[0].style.top = top + &#39;px&#39;;        var percentX = ($(&#39;.small_box&#39;).offset().left -x)/now_w;        var percentY = ($(&#39;.small_box&#39;).offset().top -y)/now_h;        $(&#39;.big_box&#39;).css({          &#39;background-position-x&#39;:percentX*origin_w  + $(&#39;#big_box&#39;).width()/2+&#39;px&#39;,          &#39;background-position-y&#39;:percentY*origin_h  + $(&#39;#big_box&#39;).height()/2+&#39;px&#39;        })      }      // 滚轮滚动      $(&#39;.big_box&#39;).on(&#39;mousewheel&#39;,function(_event,delta){        var x = _event.pageX||_event.clientX,            y = _event.pageY||_event.clientY;        var left=x- $(&#39;#big_box&#39;).width()/2 - $(&#39;.demo&#39;).offset().left ;        var top=y - $(&#39;#big_box&#39;).width()/2 - $(&#39;.demo&#39;).offset().top ;        var targe_w = $(this).width();        var targe_h = $(this).height();        if (delta>0) {          targe_w += 3;          targe_h += 3;          $(&#39;#big_box&#39;)[0].style.left = left -3+ &#39;px&#39;;          $(&#39;#big_box&#39;)[0].style.top = top -3 + &#39;px&#39;;        }else{          targe_w -= 3;          targe_h -= 3;          $(&#39;#big_box&#39;)[0].style.left = left +3+ &#39;px&#39;;          $(&#39;#big_box&#39;)[0].style.top = top +3 + &#39;px&#39;;        }        $(this).width(targe_w);        $(this).height(targe_h);      })      //显示放大镜      $(&#39;.demo&#39;).on(&#39;mouseover&#39;,function(){        $(&#39;#big_box&#39;).show();      });      $(&#39;.demo&#39;).on(&#39;mouseout&#39;,function(){        $(&#39;#big_box&#39;).hide();      });    })  </script>  <br> <br> <br> <br> <br>