js放大镜特效_盂希_前端开发者

  在平时网上商城购物时,我们能够通过放大镜效果来使我们看图片能够更加的清楚,今天我就来给大家分享一下我学习的放大镜特效

  下图是原图的样子                                                              下图是鼠标放上去的效果

                  

 

 

  接下来我们就看一看放大镜效果是如何实现的

  1.首先我们还是先把div布局写出来

<div class="bfdj">
    <div id="box">
        <div id="bug1">
            <img src="../img/b2.jpg" alt="" id="bug">
            <div id="ball"></div>
            <div id="mask"></div>
        </div>

        <div id="bug2">
            <img src="../img/b2.jpg" id="pic">
        </div>
    </div>
    <!--小图片五张-->
    <div id="bimg">
        <div id="imgs">
            <img src="../img/b2.jpg" alt="" id="img1">
            <img src="../img/bbbb.jpg" alt="" id="img2">
            <img src="../img/bbbb1.jpg" alt="" id="img3">
            <img src="../img/bbbb2.jpg" alt="" id="img4">
            <img src="../img/bbbb3.jpg" alt="" id="img5">
        </div>
    </div>
</div>
<script src="../js/bf.js"></script>
<div class="bfdj">
    <div id="box">
        <div id="bug1">
            <img src="../img/b2.jpg" alt="" id="bug">
            <div id="ball"></div>
            <div id="mask"></div>
        </div>

        <div id="bug2">
            <img src="../img/b2.jpg" id="pic">
        </div>
    </div>
    <!--小图片五张-->
    <div id="bimg">
        <div id="imgs">
            <img src="../img/b2.jpg" alt="" id="img1">
            <img src="../img/bbbb.jpg" alt="" id="img2">
            <img src="../img/bbbb1.jpg" alt="" id="img3">
            <img src="../img/bbbb2.jpg" alt="" id="img4">
            <img src="../img/bbbb3.jpg" alt="" id="img5">
        </div>
    </div>
</div>

<div class=”bfdj”><div id=”box”><div id=”bug1″><img src=”../img/b2.jpg” alt=”” id=”bug”><div id=”ball”></div><div id=”mask”></div></div><div id=”bug2″><img src=”../img/b2.jpg” id=”pic”></div></div><!–小图片五张–><div id=”bimg”><div id=”imgs”><img src=”../img/b2.jpg” alt=”” id=”img1″><img src=”../img/bbbb.jpg” alt=”” id=”img2″><img src=”../img/bbbb1.jpg” alt=”” id=”img3″><img src=”../img/bbbb2.jpg” alt=”” id=”img4″><img src=”../img/bbbb3.jpg” alt=”” id=”img5″></div></div></div>

<script src="../js/bf.js"></script>


  2.其次是css部分

/*放大镜*/
*{
margin: 0;
padding: 0;
}
.bfdj{
width: 100%;
height: 570px;
/**/
}
#box{
width: 375px;
height: 350px;
position: relative;
margin-left: 300px;
top:100px;
/*right: ;*/
/**/
}
#bug1{
width: 376px;
height: 350px;
border: 1px solid gainsboro;
position: relative;
display: inline-block;
/*margin-left: 200px;*/
/**/
}
/*.box-right{*/
/*position: absolute;*/
/*float: right;*/
/**/
/*margin-left: 680px;*/
/*margin-top: -250px;*/
/*width: 600px;*/
/*height: 425px;*/
/*font-size: 16px;*/
/*font-family: 微软雅黑;*/
/*border: 1px solid lightgrey;*/
/*background-image: url("../img/bhh.jpg");*/
/*}*/
#bug1 img{
width: 100%;
height: 100%;
display: block;
}
#ball{
width: 150px;
height: 150px;
position: absolute;
top:0;
left: 0;
background-color: navajowhite;
opacity: 0.3;
/*display: none;*/
}
#mask{
width:100%;
height: 100%;
position: absolute;
top:0;
left: 0;
z-index: 5;
cursor: pointer;
}
#bimg{
position: relative;
margin-left: 300px;
margin-top: 100px;
}
#imgs img{
width: 70px;
height: 70px;
float: left;
border: 2px solid transparent;
margin-left: 2px;
margin-top: 2px;
position: relative;
/*margin-left: 400px;*/

}
#imgs img:first-child{
margin-left: 0;
}
#imgs img:hover{
border: 2px solid gray;
}

#bug2{
/**/
width: 376px;
height: 350px;
position: relative;
top:-355px;
left: 1px;
overflow: hidden;
display: none;
z-index: 4;
}
#bug2 img{
width: 400%;
height: 400%;
display: block;
border: none;
position: absolute;
z-index: 2;
}

/*放大镜*/
*{
margin: 0;
padding: 0;
}
.bfdj{
width: 100%;
height: 570px;
/**/
}
#box{
width: 375px;
height: 350px;
position: relative;
margin-left: 300px;
top:100px;
/*right: ;*/
/**/
}
#bug1{
width: 376px;
height: 350px;
border: 1px solid gainsboro;
position: relative;
display: inline-block;
/*margin-left: 200px;*/
/**/
}
/*.box-right{*/
/*position: absolute;*/
/*float: right;*/
/**/
/*margin-left: 680px;*/
/*margin-top: -250px;*/
/*width: 600px;*/
/*height: 425px;*/
/*font-size: 16px;*/
/*font-family: 微软雅黑;*/
/*border: 1px solid lightgrey;*/
/*background-image: url("../img/bhh.jpg");*/
/*}*/
#bug1 img{
width: 100%;
height: 100%;
display: block;
}
#ball{
width: 150px;
height: 150px;
position: absolute;
top:0;
left: 0;
background-color: navajowhite;
opacity: 0.3;
/*display: none;*/
}
#mask{
width:100%;
height: 100%;
position: absolute;
top:0;
left: 0;
z-index: 5;
cursor: pointer;
}
#bimg{
position: relative;
margin-left: 300px;
margin-top: 100px;
}
#imgs img{
width: 70px;
height: 70px;
float: left;
border: 2px solid transparent;
margin-left: 2px;
margin-top: 2px;
position: relative;
/*margin-left: 400px;*/

}
#imgs img:first-child{
margin-left: 0;
}
#imgs img:hover{
border: 2px solid gray;
}

#bug2{
/**/
width: 376px;
height: 350px;
position: relative;
top:-355px;
left: 1px;
overflow: hidden;
display: none;
z-index: 4;
}
#bug2 img{
width: 400%;
height: 400%;
display: block;
border: none;
position: absolute;
z-index: 2;
}

  3.最后是js部分代码

        //放大镜
        var bug = document.getElementById("bug");
        var img1= document.getElementById("img1")
        var img2= document.getElementById("img2")
        var img3= document.getElementById("img3")
        var img4= document.getElementById("img4")
        var img5= document.getElementById("img5")

        img1.addEventListener("mousemove",function () {
            bug.src="../img/b2.jpg"
        })
        img2.addEventListener("mousemove",function () {
            bug.src="../img/bbbb.jpg"
        })
        img3.addEventListener("mousemove",function () {
            bug.src="../img/bbbb1.jpg"
        })
        img4.addEventListener("mousemove",function () {
            bug.src="../img/bbbb2.jpg"
        })
        img5.addEventListener("mousemove",function () {
            bug.src="../img/bbbb3.jpg"
        })

        var bug1 = document.getElementById("bug1");
        var ball = document.getElementById("ball");
        var bug2= document.getElementById("bug2");
        var rightImg = document.getElementById("pic");

        img1.addEventListener("mousemove",function () {
            rightImg.src="../img/b2.jpg"
        })
        img2.addEventListener("mousemove",function () {
            rightImg.src="../img/bbbb.jpg"
        })
        img3.addEventListener("mousemove",function () {
            rightImg.src="../img/bbbb1.jpg"
        })
        img4.addEventListener("mousemove",function () {
            rightImg.src="../img/bbbb2.jpg"
        })
        img5.addEventListener("mousemove",function () {
            rightImg.src="../img/bbbb3.jpg"
        })
        bug1.onmousemove =function (event) {
                var e = event || window.event; //获取鼠标对象
                ball.style.display="block";
                bug2.style.display="block";
                //鼠标居中
                var x = (e.offsetX || e.layerX) - ball.offsetWidth/2//offsetWidth获取 ball的宽度
                var y = (e.offsetY || e.layerY) - ball.offsetHeight/2
                if(x<=0){
                    x=0;
                }else if(x>box.clientWidth-ball.offsetWidth){
                    x=box.clientWidth-ball.offsetWidth
                }
                if(y<=0){
                    y=0;
                }else if(y>box.clientHeight-ball.offsetHeight){
                    y=box.clientHeight-ball.offsetHeight
                }
                ball.style.left=x+"px";
                ball.style.top=y+"px";
        //四倍大小
                rightImg.style.left= x*-4 +"px"
                rightImg.style.top= y*-4 +"px";


                this.onmouseout =function () {
                    ball.style.display="none";
                    bug2.style.display="none";
                }
            }
        //放大镜
        var bug = document.getElementById("bug");
        var img1= document.getElementById("img1")
        var img2= document.getElementById("img2")
        var img3= document.getElementById("img3")
        var img4= document.getElementById("img4")
        var img5= document.getElementById("img5")

        img1.addEventListener("mousemove",function () {
            bug.src="../img/b2.jpg"
        })
        img2.addEventListener("mousemove",function () {
            bug.src="../img/bbbb.jpg"
        })
        img3.addEventListener("mousemove",function () {
            bug.src="../img/bbbb1.jpg"
        })
        img4.addEventListener("mousemove",function () {
            bug.src="../img/bbbb2.jpg"
        })
        img5.addEventListener("mousemove",function () {
            bug.src="../img/bbbb3.jpg"
        })

        var bug1 = document.getElementById("bug1");
        var ball = document.getElementById("ball");
        var bug2= document.getElementById("bug2");
        var rightImg = document.getElementById("pic");

        img1.addEventListener("mousemove",function () {
            rightImg.src="../img/b2.jpg"
        })
        img2.addEventListener("mousemove",function () {
            rightImg.src="../img/bbbb.jpg"
        })
        img3.addEventListener("mousemove",function () {
            rightImg.src="../img/bbbb1.jpg"
        })
        img4.addEventListener("mousemove",function () {
            rightImg.src="../img/bbbb2.jpg"
        })
        img5.addEventListener("mousemove",function () {
            rightImg.src="../img/bbbb3.jpg"
        })
        bug1.onmousemove =function (event) {
                var e = event || window.event; //获取鼠标对象
                ball.style.display="block";
                bug2.style.display="block";
                //鼠标居中
                var x = (e.offsetX || e.layerX) - ball.offsetWidth/2//offsetWidth获取 ball的宽度
                var y = (e.offsetY || e.layerY) - ball.offsetHeight/2
                if(x<=0){
                    x=0;
                }else if(x>box.clientWidth-ball.offsetWidth){
                    x=box.clientWidth-ball.offsetWidth
                }
                if(y<=0){
                    y=0;
                }else if(y>box.clientHeight-ball.offsetHeight){
                    y=box.clientHeight-ball.offsetHeight
                }
                ball.style.left=x+"px";
                ball.style.top=y+"px";
        //四倍大小
                rightImg.style.left= x*-4 +"px"
                rightImg.style.top= y*-4 +"px";


                this.onmouseout =function () {
                    ball.style.display="none";
                    bug2.style.display="none";
                }
            }

//放大镜
var bug = document.getElementById(“bug”);
var img1= document.getElementById(“img1”)
var img2= document.getElementById(“img2”)
var img3= document.getElementById(“img3”)
var img4= document.getElementById(“img4”)
var img5= document.getElementById(“img5”)

img1.addEventListener(“mousemove”,function () {
bug.src=”../img/b2.jpg”
})
img2.addEventListener(“mousemove”,function () {
bug.src=”../img/bbbb.jpg”
})
img3.addEventListener(“mousemove”,function () {
bug.src=”../img/bbbb1.jpg”
})
img4.addEventListener(“mousemove”,function () {
bug.src=”../img/bbbb2.jpg”
})
img5.addEventListener(“mousemove”,function () {
bug.src=”../img/bbbb3.jpg”
})

var bug1 = document.getElementById(“bug1”);
var ball = document.getElementById(“ball”);
var bug2= document.getElementById(“bug2”);
var rightImg = document.getElementById(“pic”);

img1.addEventListener(“mousemove”,function () {
rightImg.src=”../img/b2.jpg”
})
img2.addEventListener(“mousemove”,function () {
rightImg.src=”../img/bbbb.jpg”
})
img3.addEventListener(“mousemove”,function () {
rightImg.src=”../img/bbbb1.jpg”
})
img4.addEventListener(“mousemove”,function () {
rightImg.src=”../img/bbbb2.jpg”
})
img5.addEventListener(“mousemove”,function () {
rightImg.src=”../img/bbbb3.jpg”
})
bug1.onmousemove =function (event) {
var e = event || window.event; //获取鼠标对象
ball.style.display=”block”;
bug2.style.display=”block”;
//鼠标居中
var x = (e.offsetX || e.layerX) – ball.offsetWidth/2//offsetWidth获取 ball的宽度
var y = (e.offsetY || e.layerY) – ball.offsetHeight/2
if(x<=0){
x
=0;
}else if(x>box.clientWidth-ball.offsetWidth){
x=box.clientWidth-ball.offsetWidth
}
if(y
<=0){
y
=0;
}else if(y>box.clientHeight-ball.offsetHeight){
y=box.clientHeight-ball.offsetHeight
}
ball.style.left=x+”px”;
ball.style.top=y+”px”;
//四倍大小
rightImg.style.left= x*-4 +”px”
rightImg.style.top= y*-4 +”px”;

this.onmouseout =function () {
ball.style.display=”none”;
bug2.style.display=”none”;
}
}

 

» 本文来自:前端开发者 » 《js放大镜特效_盂希_前端开发者》
» 本文链接地址:https://www.rokub.com/73368.html
» 您也可以订阅本站:https://www.rokub.com
赞(0)
64K

评论 抢沙发

评论前必须登录!