Preview effect (here is a static sample, interested can run the following code) :

Implementation method 1.

<! DOCTYPEhtml>
<html>
<head>
    <meta charset="UTF-8">
    <title>E-commerce site magnifying glass effect 1</title>
    <style type="text/css">
        html.body.div {  margin: 0;  padding: 0;  }
        #tab {  position: relative;  width: 300px;  height: 300px;margin: 50px ;  background: #CDE074;  }
        #mark {  position: absolute;  top: 0;  left: 0;  width: 100px;  height: 100px;  background: #B00000;  opacity: 0.5;  filter: alpha(opacity=50);  cursor: move;  }
        #container{ position:absolute; height:360px; width:360px; background:pink; overflow:hidden;left: 350px;top: 0px}
        #bigImg{ position:absolute; display:block; border:none; }</style>
</head>
<body>
    <div id="tab">
        <img src="https://img-blog.csdnimg.cn/2020073120293658.jpg" width="300" height="300" id="img1">
    </div>
<div id="jiance"></div>
</body>
</html>
<script type="text/javascript">
    var oTab = document.getElementById("tab");
    // Get the height and width of oDiv itself; Gets the displacement of oDiv from the top left corner of the browser;
    var tabT = oTab.offsetTop;
    var tabL = oTab.offsetLeft;
    var tabW = oTab.offsetWidth;
    var tabH = oTab.offsetHeight;
    // onmouseEnter limits bubbling
    oTab.onmouseenter = function (e) {
        // Create a large image container
        var container = document.createElement("div");
        container.id = "container";
        this.appendChild(container);
        var bigImg=document.createElement("img");
        bigImg.src="https://img-blog.csdnimg.cn/2020073120293658.jpg";
        bigImg.id="bigImg";
        container.appendChild(bigImg);
        // Dynamically create a DIV element with the id mark when the mouse moves over
        var mark = document.createElement("div");
        mark.id = "mark";
        this.appendChild(mark);// Add mark to oTab;
        / / setMark execution
        setMark(mark, container, e);
    };
    oTab.onmousemove = function (e) {
        var mark = document.getElementById("mark");
        if (mark) {
            // Check whether mark exists. If so, execute setMarksetMark(mark, container, e); }};// Onmouseleave can also display bubbling;
    oTab.onmouseleave = function () {
        var mark = document.getElementById("mark");
        if (mark) {
            // Remove the mark node when leaving;
            this.removeChild(mark);
            this.removeChild(container); }};var jiance=document.getElementById("jiance");
    function setMark(mark,container,e) {
        e = e || window.event;
        // The above is the standard configuration of the binding event;
        // Get the width and height of the mark div; And make the Mark DIV appear right in the middle of the mouse position;
        var markW = mark.offsetWidth;
        var markH = mark.offsetHeight;
        var l = e.clientX - tabL - (markW / 2);
        var t = e.clientY - tabT - (markH / 2);
        mark.style.left = l + "px";
        mark.style.top = t + "px";
        container.style.left=tabW+10+"px";
        container.style.top=0;
        bigImg.style.left="-"+l/tabW*bigImg.width+"px";
        bigImg.style.top="-"+t/tabH*bigImg.height+"px";
        // The following is the judgment boundary, when the mouse moves over the left boundary and the right boundary judgment;
        if (l < 0) {
            mark.style.left = 0;
        } else if (l > (tabW - markW)) {
            mark.style.left = tabW - markW + "px";
            bigImg.style.left="-"+(tabW-markW)/tabW*bigImg.width+"px";
        }
        // When the mouse moves over the upper and lower boundaries;
        if (t < 0) {
            mark.style.top = 0 + "px";
        } else if (t > (tabH - markH)) {
            mark.style.top = tabH - markH + "px";
            bigImg.style.top="-"+(tabH-markH)/tabH*bigImg.height+"px"; }};</script>
Copy the code

Implementation method two:

<! DOCTYPEhtml>
<html>
<head>
    <meta charset="UTF-8">
    <title>E-commerce site magnifying glass effect 2</title>
    <style type="text/css">
        html.body.div {  margin: 0;  padding: 0;  }
        #tab {  position: relative;  width: 300px;  height: 300px;margin: 50px ;  background: #CDE074;  }
        #mark {  position: absolute;  top: 0;  left: 0;  width: 100px;  height: 100px;  background: #B00000;  opacity: 0.5;  filter: alpha(opacity=50);  cursor: move;  }
        #container{ position:absolute; height:360px; width:360px; background:pink; overflow:hidden;left: 350px;top: 0px}
        #bigImg{ position:absolute; display:block; border:none; }</style>
</head>
<body>
    <div id="tab">
        <img src="https://img-blog.csdnimg.cn/2020073120293658.jpg" width="300" height="300" id="img1">
    </div>
<div id="jiance"></div>
</body>
</html>
<script type="text/javascript">
    // Get the height and width of oDiv itself; Gets the displacement of oDiv from the top left corner of the browser;
    var oDiv = document.getElementById("tab");
    var off = offset.call(oDiv);
    oDiv.onmouseenter = function (e) {
        / / insets
        var mark = document.createElement("div");
        mark.id = "mark";
        this.appendChild(mark);
        // Create a large image container
        var container = document.createElement("div");
        container.id = "container";
        this.appendChild(container);
        var bigImg=document.createElement("img");
        bigImg.src="https://img-blog.csdnimg.cn/2020073120293658.jpg";
        bigImg.id="bigImg";
        container.appendChild(bigImg);
        setMark(mark, container, e);
    }
    oDiv.onmousemove = function (e) {
        var mark = document.getElementById("mark");
        if (mark) {
            critical(this, mark, e);
        }
    }
    oDiv.onmouseleave = function (e) {
        var mark = document.getElementById("mark");
        if (mark) {
            this.removeChild(mark); }}function critical(oDiv, mark, e) {
        e = e || window.event;
        var l = e.clientX - off.left - (mark.offsetWidth / 2);
        var t = e.clientY - off.top - (mark.offsetHeight / 2);
        mark.style.top = t + "px";
        mark.style.left = l + "px";
        if (l <= 0) {
            mark.style.left = 0;
        } else if (l >= (oDiv.offsetWidth - mark.offsetWidth)) {
            mark.style.left = oDiv.offsetWidth - mark.offsetWidth + "px";
        }
        if (t <= 0) {
            mark.style.top = 0;
        } else if (t >= (oDiv.offsetHeight - mark.offsetHeight)) {
            mark.style.top = oDiv.offsetHeight - mark.offsetHeight + "px"; }}function setMark(mark,container,e) {
        e = e || window.event;
        // The above is the standard configuration of the binding event;
        // Get the width and height of the mark div; And make the Mark DIV appear right in the middle of the mouse position;
        var markW = mark.offsetWidth;
        var markH = mark.offsetHeight;
        var l = e.clientX - tabL - (markW / 2);
        var t = e.clientY - tabT - (markH / 2);
        mark.style.left = l + "px";
        mark.style.top = t + "px";
        container.style.left=tabW+10+"px";
        container.style.top=0;
        bigImg.style.left="-"+l/tabW*bigImg.width+"px";
        bigImg.style.top="-"+t/tabH*bigImg.height+"px";
        // The following is the judgment boundary, when the mouse moves over the left boundary and the right boundary judgment;
        if (l-15 < 0) {
            mark.style.left = 0;
        } else if (l > (tabW - markW)) {
            mark.style.left = tabW - markW + "px";
            bigImg.style.left="-"+(tabW-markW)/tabW*bigImg.width+"px";
        }
        // When the mouse moves over the upper and lower boundaries;
        if (t < 0) {
            mark.style.top = 0 + "px";
        } else if (t > (tabH - markH)) {
            mark.style.top = tabH - markH + "px";
            bigImg.style.top="-"+(tabH-markH)/tabH*bigImg.height+"px"; }};function offset() {
        var left = this.offsetLeft, top = this.offsetTop, par = this.offsetParent;
        while (par) {
            left += par.offsetLeft;
            top += par.offsetTop;
            if (window.navigator.userAgent.indexOf("MSIE 8.0") < = -1) {
                left += par.clientLeft;
                top += par.clientTop;
            }
            par = par.offsetParent;
        }
        return {
            left: left,
            top: top
        };
    }
</script>
Copy the code

Implementation method three:

<! DOCTYPEhtml>
<html>
<head>
    <meta charset="UTF-8">
    <title>E-commerce site magnifying glass effect</title>
    <style type="text/css">
        html.body.div img{  margin: 0;  padding: 0;  }
        #tab {  position: relative;  width: 450px;  height: 450px;margin: 50px ;  background: #CDE074;  }
        #mark {  position: absolute;  top: 0;  left: 0;  width: 100px;  height: 100px;  background: #B00000;  opacity: 0.5;  filter: alpha(opacity=50);  cursor: move;  }
        #container{ position:absolute; height:450px; width:450px; background:pink; overflow:hidden;left: 350px;top: 0px}
        #bigImg{ position:absolute; display:block; border:none; }</style>
</head>
<body>
<div id="tab">
    <img src="https://img-blog.csdnimg.cn/2020073120293658.jpg" width="450" height="450" id="img1">
</div>
<div id="jiance"></div>
</body>
</html>
<script type="text/javascript">
    var oTab = document.getElementById("tab");
    // Get the height and width of oDiv itself; Gets the displacement of oDiv from the top left corner of the browser;
    var tabT = oTab.offsetTop;
    var tabL = oTab.offsetLeft;
    var tabW = oTab.offsetWidth;
    var tabH = oTab.offsetHeight;
    // onmouseEnter limits bubbling
    oTab.onmouseenter = function (e) {
        // Create a large image container
        var container = document.createElement("div");
        container.id = "container";
        this.appendChild(container);
        var bigImg=document.createElement("img");
        bigImg.src="https://img-blog.csdnimg.cn/2020073120293658.jpg";
        bigImg.id="bigImg";
        container.appendChild(bigImg);
        // Dynamically create a DIV element with the id mark when the mouse moves over
        var mark = document.createElement("div");
        mark.id = "mark";
        this.appendChild(mark);// Add mark to oTab;
        / / setMark execution
        setMark(mark, container, e);
    };
    oTab.onmousemove = function (e) {
        var mark = document.getElementById("mark");
        if (mark) {
            // Check whether mark exists. If so, execute setMarksetMark(mark, container, e); }};// Onmouseleave can also display bubbling;
    oTab.onmouseleave = function () {
        var mark = document.getElementById("mark");
        if (mark) {
            // Remove the mark node when leaving;
            this.removeChild(mark);
            this.removeChild(container); }};var jiance=document.getElementById("jiance");
    function setMark(mark,container,e) {
        e = e || window.event;
        // The above is the standard configuration of the binding event;
        // Get the width and height of the mark div; And make the Mark DIV appear right in the middle of the mouse position;
        var markW = mark.offsetWidth;
        var markH = mark.offsetHeight;
        var l = e.clientX - tabL - (mark.offsetWidth / 2);
        var t = e.clientY - tabT - (mark.offsetHeight / 2);
        mark.style.left = l + "px";
        mark.style.top = t + "px";
        container.style.left=tabW+10+"px";
        container.style.top=0;
// bigImg.style.left="-"+(l/tabW*bigImg.width)+"px";
// bigImg.style.top="-"+(t/tabH*bigImg.height)+"px";
        // If the style is written as above, there are compatibility problems in IE678, it should be changed to the following
        if(l<=0){
            bigImg.style.left=="0px";
        }else{
            bigImg.style.left="-"+l/tabW*bigImg.width+"px";
        }
        if(t<=0){
            bigImg.style.top=="0px";
        }else{
            bigImg.style.top="-"+(t/tabH*bigImg.height)+"px";
        }
        console.log("====t::"+parseInt(t));
        // The following is the judgment boundary, when the mouse moves over the left boundary and the right boundary judgment;
        if (l < 0) {
            mark.style.left = 0;
        } else if (l > (tabW - markW)) {
            mark.style.left = tabW - markW + "px";
            bigImg.style.left="-"+(tabW-markW)/tabW*bigImg.width+"px";
        }
        // When the mouse moves over the upper and lower boundaries;
        if (t < 0) {
            mark.style.top = 0 + "px";
        } else if (t > (tabH - markH)) {
            mark.style.top = tabH - markH + "px";
            bigImg.style.top="-"+(tabH-markH)/tabH*bigImg.height+"px"; }};</script>
Copy the code