最近公司做的艺术品放大效果,和以往的淘宝放大镜效果有些不同,这个需要在本图上放大
<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; // 计算画缩放的宽高 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' }) } // 滚轮滚动 $('.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>
相关推荐:
html5 canvas实现烟花绽放的特效
canvas实现爱心和彩虹雨的效果
以上就是h5实现放大镜效果的代码的详细内容。
