Home > Web Front-end > JS Tutorial > JavaScript implements the effect of a magnifying glass (code example)

JavaScript implements the effect of a magnifying glass (code example)

不言
Release: 2019-01-18 10:41:24
forward
3025 people have browsed it

The content of this article is about realizing the magnifying glass effect in JavaScript (code example). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Implementation principle: Use 2 divs, which enlarge the picture and the small picture respectively. There should be a mask layer on the small picture, through Position the mask layer to position the relative position of the large image, and the movement of the mask layer should be in the opposite direction to the movement of the large image

Key: The size ratio of the large image to the small image It should be the same as the size of the mask layer and the ratio of the enlarged display area;

Difficulty: Calculate the mask layer to display the position of the corresponding large picture

Without further ado, just look at the code

code

html:

<div id="small">
    <div id="pic1">
        <img src="62-130501163925.jpg" alt="">
    </div>
    <div id="mask"></div>
</div>
<div id="big">
    <div id="pic2">
        <img src="62-130501163925.jpg" alt="">
    </div>
</div>
Copy after login

css

#small{
    width: 500px;
    height: 312px;
    position: absolute;
    left: 20px;
    top: 20px;
}
#pic1{
    position: absolute;
    left: 0px;
    top: 0px;
}
#pic1 img{
    width: 100%;
    height: 100%;
}
#big{
    width: 200px;
    height: 200px;
    position: absolute;
    right: 50px;
    top: 50px;
    border: 1px solid blue;
    overflow: hidden;
}
#pic2{
    width: 1000px;
    height: 625px;
    position: absolute;
    left: 0;
    top: 0;
}
#pic2 img{
    width: 100%;
    height: 100%;
}
#mask{
    width: 100px;
    height: 100px;
    background: black;
    opacity: 0.3;/*让遮罩层看起来透明*/
    filter: alpha(opacity = 30);/*兼容不同的浏览器*/
    position: absolute;
    display: none;
}
Copy after login

js

window.onload = function(){//文档内容加载完之后再执行
    //当鼠标移入小图片,显示遮罩层和放大的区域
    $(&#39;small&#39;).onmouseenter = function(){
        $(&#39;mask&#39;).style.display = &#39;block&#39;;
        $(&#39;big&#39;).style.display=&#39;block&#39;;
    }
        //鼠标移出时,隐藏遮罩层和放大的区域
    $(&#39;small&#39;).onmouseleave = function(){
        $(&#39;mask&#39;).style.display=&#39;none&#39;;
        $(&#39;big&#39;).style.display="none";
    }
    //鼠标移动
    $(&#39;small&#39;).onmousemove = function(ev){
        var e = ev || window.event;
        //计算鼠标的位置,并让鼠标显示在遮罩层的中间
        var l = e.clientX - $(&#39;small&#39;).offsetLeft - 50;
        var t = e.clientY - $(&#39;small&#39;).offsetTop -50;
        //别让遮罩层移出图片
        if(l <= 0){
            l = 0;
        }
        if(l >= 500 - 100){
            l = 400;
        }
        if(t <= 0){
            t = 0;
        }
        if(t >= 312 - 100){
            t = 212;
        }
        //遮罩层的移动
        $(&#39;mask&#39;).style.left = l + &#39;px&#39;;
        $(&#39;mask&#39;).style.top = t + &#39;px&#39;;
        //通过遮罩层移动,来计算出放大后图片的显示区域
        $("pic2").style.left = -$("mask").offsetLeft * 2 + &#39;px&#39;;
        $("pic2").style.top = -$("mask").offsetTop * 2 + &#39;px&#39;;
    }
}
//为了更容容易的获取id
function $(id){
    return document.getElementById(id);
}
Copy after login

The above is the detailed content of JavaScript implements the effect of a magnifying glass (code example). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template