用原生js简单实现了放大镜效果

<!DOCTYPE html >
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>放大镜</title>
<style>
   #out{
     position:relative;
   }
   #left{
     width:310px;
     height:310px;
     border:1px solid blue;
   }
   #buttom{
     width:310px;
     height:40px;
     margin-top:10px;
   }
   #buttom .small{
     border:1px solid red;
     float:left;
     margin-right:5px;
   }
   #right{
     width:400px;
     height:310px;
     position:absolute;
     top:0px;
     left:320px;
     border: 1px solid black;
     display:none;
     overflow:hidden;
   }
   #right img{
     position:absolute;
     top:0px;
     left:0px;
   }
</style>
<script>
  window.onload=function  () {
     var left=document.getElementById("left");
     var buttom=document.getElementById("buttom");
     var right=document.getElementById("right");
     buttom.onmouseover=function  (e) { 
       var ev=e||window.event; //事件委托
       var imgobj=ev.srcElement||ev.target;  //事件委托 ,取得事件对象
       if(imgobj.nodeName=="IMG"){//事件委托 ,如果事件对象的节点名为IMG,则true 
          var names=imgobj.src; //事件委托 ,取得事件节点的src
          var imgname=names.substring(names.lastIndexOf("/")).replace("1","2"); //names.indexof("/")取得分隔符的位置,然后利用substring从这个节点开始向后剪切,然后利用;取得小图片的名字,把一换成二直接是大图片
          left.innerHTML="<img src=img"+imgname+">";
       }
     }

     left.onmouseover=function  () {
           right.style.display="block"; //框体显示
           var leftimg=left.getElementsByTagName("img")[0]; //取得展示框体对象
           var names=leftimg.src;//取得展示框体对象src值
           var imgname=names.substring(names.lastIndexOf("/")).replace("2","3");//替换展示内容 
           right.innerHTML="<img src=img"+imgname+">";
           var rightimg=right.getElementsByTagName("img")[0];//取得大展示框体对象 
           this.onmousemove=function  (e) {
           var ev=e||window.event;  //兼容事件对象 
           var l=ev.layerX||ev.offsetX;  //取得鼠标事件的坐标,IE和FF
           var r=ev.layerY||ev.offsetY;  //取得鼠标事件的坐标,IE和FF
           rightimg.style.left=-l/left.offsetWidth*(rightimg.offsetWidth-right.offsetWidth)+"px"; 
           rightimg.style.top=-r/left.offsetHeight*(rightimg.offsetHeight-right.offsetHeight)+"px";
            //-值说明展示图像走的过程要与鼠标相反,
            //减去 -right.offsetHeight因为 style.left会出现白色边框,减去展示的窗口,就会解决
                        
           }
     }
     left.onmouseout=function  () {
       right.style.display="none";
     }
  }
</script>
</head>

<body>
  <div id="out">
          <div id="left">
            <img src="img/blue_2.jpg">
          </div>
          <div id="buttom">
             <div class="small">
             <img src="img/blue_1.jpg">
             </div>
             <div class="small">
              <img src="img/yellow_1.jpg">
               </div>
          </div>
          <div id="right">
          </div>
  </div>
</body>
</html>

我在简单封装以下,做成一个插件,稍后更新
来更新了,用原生的js模块,真是累,原理都一样,不过看上去模块化,方便一个页面大量应用。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<style>
    *{
        padding: 0px;
        margin: 0px;
    }
    .content{
        width: 1200px;
        height: 600px;
        position: relative;
    }
    .left{
        width: 310px;
        height: 380px;
    }
    .top{
        width: 310px;
        height: 310px;
        border: 1px solid blue;
        cursor: pointer;
    }
    .bottom{
        position: relative;
        width: 310px;
        height: 60px;
        border: 1px solid black;
    }
    .bottom img{
        display: inline-block;
        width: 60px;
        height: 60px;
        float: left;
        margin: 0 30px;
        cursor: pointer;
    }
    .right{
        border: 1px solid ;
        width: 350px;
        height: 300px;
        position: absolute;
        left: 400px;
        top: 0px;
        overflow: hidden;
    }
    .right img{
        position: absolute;
    }
</style>
<body>
<div class="content">
    <div class="left" id="left">
        <div class="top" id="top">
            <img src="img/blue_2.jpg" alt="1"/>
        </div>
        <div class="bottom" id="bottom">
            <img src="img/blue_1.jpg" alt="1"/>
            <img src="img/yellow_1.jpg" alt="1"/>
        </div>
    </div>
    <div class="right" id="right">
        <img src="" alt=""/>
    </div>
</div>
</body>
<script>
    window.onload= function () {
        (function (controller) {
            var Mod=new controller();
            Mod.include({
                left:document.getElementById("left"),
                top:document.getElementById("top"),
                bottom:document.getElementById("bottom"),
                img:document.getElementById("bottom").getElementsByTagName("img"),
                right:document.getElementById("right"),
                Event: function (obj,type,done) {
                    if(obj.addEventListener){
                        obj.addEventListener(type,done,false)
                    }else if(obj.attachEvent){
                        obj.attachEvent('on'+type, function () {
                            done.apply(obj,arguments)
                        })
                    }
                },
                /*更换底部小图标*/
                bottom_mouseover: function () {
                    var that=this;
                    for(var i=0;i<that.img.length;i++){
                        that.Event(that.img[i],'click', function () {
                            console.log(this.src);
                            var srcNanme=this.src.substring(this.src.lastIndexOf("/")).replace("1","2");
                            var src=document.createAttribute("src");
                            src.nodeValue="img"+srcNanme;
                            that.top.getElementsByTagName("img")[0].setAttributeNode(src);
                        })
                    }
                },
                right_mouseover: function () {
                    var that=this;
                    this.Event(this.top.getElementsByTagName("img")[0],'mouseover', function () {

                        var rightImg=that.right.getElementsByTagName("img")[0];
                        var topImg=that.top.getElementsByTagName("img")[0];
                        var srcNanme=this.src.substring(this.src.lastIndexOf("/")).replace("2","3");
                        var src=document.createAttribute("src");
                        src.nodeValue="img"+srcNanme;
                        rightImg.setAttributeNode(src);

                        topImg.onmousemove= function (e) {
                            var el=e||window.event;
                            var l= el.layer||el.offsetX;
                            var r=el.layer||el.offsetY;
                            rightImg.style.left=-l*((rightImg.offsetWidth-that.right.offsetWidth)/topImg.offsetWidth)+"px";
                            rightImg.style.top=-r*((rightImg.offsetHeight-that.right.offsetHeight)/topImg.offsetHeight)+"px";
                            console.log(rightImg.style.left);
                        }
                    })
                }
            });
            var mod=new Mod();
            mod.bottom_mouseover();
            mod.right_mouseover();
        })(createClass)
    };

</script>
<script>
    (function (exports) {
        var createClass= function (parents) {
            var kclass= function () {
                this.init.apply(this,arguments);
            };
            if(parents){
                var subclass=function(){};
                subclass.prototype=parents.prototype;
                kclass.prototype=new subclass();
            };
            kclass.prototype.init= function () {

            };
            kclass.fn=kclass.prototype;
            kclass.fn.parent=kclass;
            kclass.extend= function (obj) {
                for(var i in obj){
                    kclass[i]=obj[i];
                }
            };
            kclass.include= function (obj) {
                for(var i in obj){
                    kclass.fn[i]=obj[i];
                }
            };
            kclass.proxy= function (fn) {
                var self=this;
                return function () {
                    return fn.apply(self,arguments)
                }
            };
            return kclass;
        };
        exports.createClass=createClass;
    })(window)
</script>
</html>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 162,825评论 4 377
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,887评论 2 308
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 112,425评论 0 255
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,801评论 0 224
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 53,252评论 3 299
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 41,089评论 1 226
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,216评论 2 322
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 31,005评论 0 215
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,747评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,883评论 2 255
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,354评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,694评论 3 265
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,406评论 3 246
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,222评论 0 9
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,996评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,242评论 2 287
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 36,017评论 2 281

推荐阅读更多精彩内容