The magnifying glass effect is achieved using pure javascript

  • 2020-05-16 06:23:56
  • OfStack

jd or taobao specific products have a magnifying glass effect. Although online similar plug-in dazzling, applied to the project has a lot of inconvenience, their own time to write a similar plug-in, the accumulation of code, why not!! let 'go:

I plan to encapsulate this effect into a plug-in, first implement the most basic algorithm, and then step by step encapsulate it:

Final effect:

html code:


<div id="Magnifier"></div>

css code:


 <style>
        * {
            margin: 0;
            padding: 0;
        }
    </style>

It seems like nothing. Let's start our powerful js tour:

javascript code:


  function createElement(MagnifierId, sImg, bImg) {
            var Magnifier = $(MagnifierId);
            Magnifier.style.position = 'relative';
            // insets div
            var smallDiv = $Create("div");
            smallDiv.setAttribute("id", "small");
            smallDiv.style.position = "absolute";
            // The mask layer
            var mask = $Create("div");
            mask.setAttribute("id", "mask");
            mask.style.position = "absolute";
            // The lens
            var mirror = $Create("div");
            mirror.setAttribute("id", "mirror");
            mirror.style.opacity = 0.3;
            mirror.style.position = "absolute";
            mirror.style.display = "none";
            // insets
            var smallImg = $Create("img");
            smallImg.setAttribute("src", sImg);
            smallImg.setAttribute("id", "smallImg");
            smallImg.onload = function () {
                // If the magnifying glass is not set height or width Set the magnifying glass size according to the small image size
                if (!Magnifier.offsetHeight) {
                    Magnifier.style.width = this.offsetWidth+"px";
                    Magnifier.style.height = this.offsetHeight + "px";
                }
                // Mask size and small image 1 sample
                mask.style.opacity = "0";
                mask.style.width = this.width + 'px';
                mask.style.height = this.height + "px";
                mask.style.zIndex = 2;
                bigDiv.style.left = this.width + 5 + "px";
                bigDiv.style.top = "-5px";
            }
            smallDiv.appendChild(mask);
            smallDiv.appendChild(mirror);
            smallDiv.appendChild(smallImg);
            // Windows
            var bigDiv = $Create("div");
            bigDiv.setAttribute("id", "big");
            bigDiv.style.position = "absolute";
            bigDiv.style.overflow = "hidden";
            bigDiv.style.display = "none";
            var bigImg = $Create("img");
            bigImg.setAttribute("src", bImg);
            bigImg.setAttribute("id", "bigImg");
            bigImg.style.position = "absolute";
            bigDiv.appendChild(bigImg);
            Magnifier.appendChild(smallDiv);
            Magnifier.appendChild(bigDiv);
        }
        function setMagnifierStyle(mirrorStyle,shichuangStyle) {
            //mirror
            for (var item in mirrorStyle) {
                mirror.style[item] = mirrorStyle[item];
            }
            for (var item in shichuangStyle) {
                $("big").style[item] = shichuangStyle[item];
            }
        }
        function registerEvent() {
            $("mask").onmouseover = function () {
                $("big").style.display = "block";
                mirror.style.display = "block";
            }
            $("mask").onmouseout = function () {
                $("big").style.display = "none";
                mirror.style.display = "none";
            }
            $("mask").onmousemove = function (evt) {
                var oEvent = evt || event;
                var disX = oEvent.offsetX;
                var disY = oEvent.offsetY;
                var mirrorLeft = disX - mirror.offsetWidth / 2;
                var mirrorTop = disY - mirror.offsetHeight / 2;
                if (mirrorLeft < 0) {
                    mirrorLeft = 0;
                }
                else if (mirrorLeft > mask.offsetWidth - mirror.offsetWidth) {
                    mirrorLeft = mask.offsetWidth - mirror.offsetWidth;
                }
                if (mirrorTop < 0) {
                    mirrorTop = 0;
                }
                else if (mirrorTop > mask.offsetHeight - mirror.offsetHeight) {
                    mirrorTop = mask.offsetHeight - mirror.offsetHeight;
                }
                mirror.style.top = mirrorTop + "px";
                mirror.style.left = mirrorLeft + "px";
                var paX = mirrorTop / (mask.offsetHeight - mirror.offsetHeight);
                var paY = mirrorLeft / (mask.offsetWidth - mirror.offsetWidth);
                $("bigImg").style.top = -paX * ($("bigImg").offsetHeight - $("big").offsetHeight) + "px";
                $("bigImg").style.left = -paY * ($("bigImg").offsetWidth - $("big").offsetWidth) + "px";
            }
        }
        function $(id) {
            return document.getElementById(id);
        }
        function $Create(type) {
            return document.createElement(type);
        }

Finally, a small call to onload 1:


 window.onload = function () {
            createElement("Magnifier", "images/Magnifier/small.jpg", "images/Magnifier/big.jpg");
            setMagnifierStyle({ "width": "30px", "height": "30px", "backgroundColor": "#fff" }, { "width": "250px", "height": "250px" });
            registerEvent();
        }

The effect is finally out,

2. Let's encapsulate it:

Magnifer class code:


        function Magnifier(
            MagnifierId,                            // Magnifying glass container ID
            sImg,                                   // Small pictures src
            bImg,                                   // The big picture src
            mirrorStyle,                            // The lens pattern in the little picture, json Format data
            ViewStyle                               // Preview window style, json Format data
            ) {
            var _this = this;
            this.MagnifierContainer = null;         // The container
            this.smallDiv = null;                   // Large container
            this.mask = null;                       // Small image mask layer
            this.mirror = null;                     // The lens picture
            this.smallImg = null;                   // insets
            this.bigDiv = null;                     // The preview view
            this.bigImg = null;                     // A larger version
            var init = function () {
                _this.MagnifierContainer = _this.$(MagnifierId);
                _this.createElement(sImg, bImg);
                _this.setMagnifierStyle(mirrorStyle, ViewStyle);
                _this.MainEvent();
            }
            init();
        }
        Magnifier.prototype.createElement = function (sImg, bImg) {
            var _this = this;
            var $Create = this.$Create;
            this.MagnifierContainer.style.position = 'relative';   // Out of the document stream, modify as necessary
            this.smallDiv = $Create("div");
            this.smallDiv.setAttribute("id", "small");
            this.smallDiv.style.position = "absolute";
            this.mask = $Create("div");
            this.mask.setAttribute("id", "mask");
            this.mask.style.position = "absolute";
            this.mirror = $Create("div");
            this.mirror.setAttribute("id", "mirror");
            this.mirror.style.opacity = 0.3;
            this.mirror.style.position = "absolute";
            this.mirror.style.display = "none";
            this.smallImg = $Create("img");
            this.smallImg.setAttribute("src", sImg);
            this.smallImg.setAttribute("id", "smallImg");
            this.smallImg.onload = function () {
                // If the magnifying glass is not set height or width Set the magnifying glass size according to the small image size
                if (!_this.MagnifierContainer.offsetHeight) {
                    _this.MagnifierContainer.style.width = this.offsetWidth + "px";
                    _this.MagnifierContainer.style.height = this.offsetHeight + "px";
                }
                // Mask size and small image 1 sample
                _this.mask.style.opacity = "0";
                _this.mask.style.width = this.offsetWidth + 'px';
                _this.mask.style.height = this.offsetHeight + "px";
                _this.mask.style.zIndex = 2;
                _this.bigDiv.style.left = this.offsetWidth + 5 + "px";
                _this.bigDiv.style.top = "-5px";
            }
            this.smallDiv.appendChild(this.mask);
            this.smallDiv.appendChild(this.mirror);
            this.smallDiv.appendChild(this.smallImg);
            this.bigDiv = $Create("div");
            this.bigDiv.setAttribute("id", "big");
            this.bigDiv.style.position = "absolute";
            this.bigDiv.style.overflow = "hidden";
            this.bigDiv.style.display = "none";
            this.bigImg = $Create("img");
            this.bigImg.setAttribute("src", bImg);
            this.bigImg.setAttribute("id", "bigImg");
            this.bigImg.style.position = "absolute";
            this.bigDiv.appendChild(this.bigImg);
            this.MagnifierContainer.appendChild(this.smallDiv);
            this.MagnifierContainer.appendChild(this.bigDiv);
        }
        Magnifier.prototype.setMagnifierStyle = function (mirrorStyle, ViewStyle) {
            for (var item in mirrorStyle) {
                this.mirror.style[item] = mirrorStyle[item];
            }
            delete item;
            for (var item in ViewStyle) {
                this.bigDiv.style[item] = ViewStyle[item];
            }
        }
        Magnifier.prototype.MainEvent = function () {
            var _this = this;
            this.mask.onmouseover = function () {
                _this.bigDiv.style.display = "block";
                _this.mirror.style.display = "block";
            }
            this.mask.onmouseout = function () {
                _this.bigDiv.style.display = "none";
                _this.mirror.style.display = "none";
            }
            this.mask.onmousemove = function (evt) {
                var oEvent = evt || event;
                var disX = oEvent.offsetX || oEvent.layerX;  //兼容ff
                var disY = oEvent.offsetY || oEvent.layerY;
                var mirrorLeft = disX - _this.mirror.offsetWidth / 2;
                var mirrorTop = disY - _this.mirror.offsetHeight / 2;
                if (mirrorLeft < 0) {
                    mirrorLeft = 0;
                }
                else if (mirrorLeft > this.offsetWidth - _this.mirror.offsetWidth) {
                    mirrorLeft = this.offsetWidth - mirror.offsetWidth;
                }
                if (mirrorTop < 0) {
                    mirrorTop = 0;
                }
                else if (mirrorTop > this.offsetHeight - _this.mirror.offsetHeight) {
                    mirrorTop = this.offsetHeight - _this.mirror.offsetHeight;
                }
                _this.mirror.style.top = mirrorTop + "px";
                _this.mirror.style.left = mirrorLeft + "px";
                var paX = mirrorTop / (this.offsetHeight - _this.mirror.offsetHeight);
                var paY = mirrorLeft / (this.offsetWidth - _this.mirror.offsetWidth);
                _this.bigImg.style.top = -paX * (_this.bigImg.offsetHeight - _this.bigDiv.offsetHeight) + "px";
                _this.bigImg.style.left = -paY * (_this.bigImg.offsetWidth - _this.bigDiv.offsetWidth) + "px";
            }
        }
        Magnifier.prototype.$ = function (id) {
            return document.getElementById(id);
        }
        Magnifier.prototype.$Create = function (type) {
            return document.createElement(type);
        }

Finally, under the onload call:


window.onload = function () {
            new Magnifier(
                        "Magnifier",
                        "images/Magnifier/small.jpg",
                        "images/Magnifier/big.jpg",
                        { "width": "30px", "height": "30px", "backgroundColor": "#fff" },
                        { "width": "250px", "height": "250px" }
                );
        }

That's all for this article, I hope you enjoy it.


Related articles: