Home  >  Article  >  Web Front-end  >  JavaScript imitation Taobao product details magnifying glass effect

JavaScript imitation Taobao product details magnifying glass effect

小云云
小云云Original
2018-03-15 16:36:512907browse

When we often visit Taobao and look at Taobao product details, we often use the magnifying glass function of product pictures. I never knew how to implement it before. When I encounter it, I basically find code modifications online. Today I use the native one. js to achieve the following magnifying glass effect.

Principle of implementation:

Prepare the large picture (clear) and the small picture. The two pictures must be the same, just enlarged or reduced. Move the mouse into the small thumbnail. When the mouse is moved into the small picture above, the zoom and large picture boxes are displayed, and then when the mouse moves inside, //The distance from the mouse to the left side of the visualization window minus the small The half width of the image mask layer is the left value of zomm. In the same way, the value of top can be found

zoom.style.left = event.clientX - zoom.offsetWidth/2 + “px” ;
zoom.style.top = event.clientY - zoom.offsetHeight/2 + “px”;

Then divide the size of the large image by the size of the small image size, then you can get the magnification ratio of the two pictures. When the mouse moves on the small picture on the left, through the magnification ratio, you can find the offset value of the large picture on the right to the left or up. You can get the desired results.

//The left value of the small mask layer on the left multiplied by the magnification factor between the pictures is the left offset value of the large picture on the right. The same goes for top

bigimg. style.left = “-” + zoom.offsetLeft*scale + “px”;
bigimg.style.top = “-” + zoom.offsetTop*scale + “px”;

Go directly to the code:

html part:


    
    javascript防淘宝放大镜效果
    
    

javascript part:

    (function(){
        //预先加载大图,避免后面鼠标移入后显示大图时再去请求资源,影响体验
        new Image().src = "b-1.png";
        new Image().src = "b-2.png";
        new Image().src = "b-3.png";
        new Image().src = "b-4.png";

        var smallbox = document.querySelector("#box .smallbox");    //小图片box
        var bigbox = document.querySelector("#box .bigbox");    //大图片box
        var smallimg = smallbox.querySelector(".smallimg"); //小图片
        var bigimg = bigbox.querySelector(".bigimg");   //大图片
        var zoom = document.querySelector(".zoom");
        var itemImg = document.querySelectorAll("#box .item img");

        for(var i = 0, len = itemImg.length; i < len; i++){
            (function(j){
                itemImg[j].onmouseenter = function(){
                    var imgSrc = this.src;
                    smallimg.src = imgSrc;
                    bigimg.src = "b-" + (j+1) + ".png"; 
                }
            })(i);


        }

        //鼠标移入事件
        smallbox.onmouseenter = function(event){
            zoom.style.display = "block";
            bigbox.style.display ="block";
            //注意:offsetWdith的值是要元素为可视状态下才可以计算出来,不然当display:none的时候则为0;

            var scale = bigimg.offsetWidth/smallimg.offsetWidth;    //放大倍数
            var ev = event || window.event;


            //鼠标在内部移动的时候
            this.onmousemove = function(event){
                //鼠标到可视化窗口左边的距离减去小图遮罩层的一半宽度则为zomm的left的值,同理可以求top的值
                zoom.style.left = event.clientX - zoom.offsetWidth/2 + "px";
                zoom.style.top = event.clientY - zoom.offsetHeight/2 + "px";

                //如果向左向上移动小于0,那么则设置left为0,实现紧贴着边框效果
                if(zoom.offsetLeft <= 0){
                    zoom.style.left = 0;
                }

                if(zoom.offsetTop <= 0){
                    zoom.style.top = 0;
                }

                //如果向右向下移动小于0,那么则设置top,实现紧贴着边框效果

                if(zoom.offsetLeft >= smallbox.offsetWidth - zoom.offsetWidth){
                    zoom.style.left = smallbox.offsetWidth - zoom.offsetWidth + "px";
                }

                if(zoom.offsetTop >= smallbox.offsetHeight - zoom.offsetHeight){
                    zoom.style.top = smallbox.offsetHeight - zoom.offsetHeight + "px";
                }

                //左边小遮罩层left的值乘以图片之间的放大倍数,则为右边大图片的left偏移值,top同理
                bigimg.style.left = "-" + zoom.offsetLeft*scale + "px";
                bigimg.style.top = "-" + zoom.offsetTop*scale + "px";

            }
        }

        //鼠标移出事件
        smallbox.onmouseleave = function(){
            bigbox.style.display = "none";
            zoom.style.display = "none";
            this.onmousemove = null;
        }

    }());

Related recommendations:

How to make a magnifying glass effect for Jingdong product details

JavaScript implementation of simple magnifying glass effect code

JS implementation of image magnifying glass plug-in example details

The above is the detailed content of JavaScript imitation Taobao product details magnifying glass effect. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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