Js淘宝放大镜实现

更新日期: 2019-01-21阅读: 2.5k标签: 图片

描述:

JS面向对象——淘宝放大镜实现

图片的引用是一个大图,一个小图

传输用的ajax,记得改成自己的ip地址,js和html都改一下


效果:



实现:

js文件:

LoadMethod.js

class LoadMethod{
    static get LOAD_IMG_FINISH(){
        return "load_img_finish";
    }
    static init(sourceList){
        let img=new Image();
        img.num=0;
        img.sourceList=sourceList;
        img.targetList={};
        img.addEventListener("load",LoadMethod.loadHandler);
        img.src=sourceList[0];
 
    }
    static loadHandler(e){
        let images=this.cloneNode(false);
        let name=this.sourceList[this.num];
        name=name.slice(name.lastIndexOf("/")+1,name.lastIndexOf("."));
        this.targetList[name]={src:images.src,elem:images,width:images.width,height:images.height};
        this.num++;
        if(this.num>this.sourceList.length-1){
            this.removeEventListener("load",LoadMethod.loadHandler);
            let evt=new Event(LoadMethod.LOAD_IMG_FINISH);
            evt.targetList=this.targetList;
            document.dispatchEvent(evt);
            return;
        }
        this.src=this.sourceList[this.num];
 
    }
}
class Drag{
    static dragElem(elem,rect,parent){
        Drag.drageHandler=Drag.mouseHandler.bind(elem);
        elem.rect=rect;
        elem.parent=parent;
        elem.addEventListener("mousedown",Drag.drageHandler);
    }
    static removeDrag(elem){
        elem.removeEventListener("mousedown",Drag.drageHandler);
        Drag.drageHandler=null;
    }
    static mouseHandler(e){
        if(e.type==="mousedown"){
            this.addEventListener("mouseup",Drag.drageHandler);
            this.offsetPoint={x:e.offsetX,y:e.offsetY};
            document.addEventListener("mousemove",Drag.drageHandler);
        }else if(e.type==="mousemove"){
           if(!this.rect){
               this.rect=this.parent.getBoundingClientRect();
           }
            let obj={
                left:e.x-this.offsetPoint.x-this.rect.left+"px",
                top:e.y-this.offsetPoint.y-this.rect.top+"px",
                position:"absolute"
            };
            Object.assign(this.style,obj);
            let elemRect=this.getBoundingClientRect();
            if(elemRect.left<this.rect.left){
                this.style.left="0px";
            }
            if(elemRect.right>this.rect.right){
                this.style.left=this.rect.right-elemRect.width-this.rect.left+"px";
            }
            if(elemRect.top<this.rect.top){
                this.style.top="0px";
            }
            if(elemRect.bottom>this.rect.bottom){
                this.style.top=this.rect.bottom-elemRect.height-this.rect.top+"px";
            }
            let evt=new Event(Drag.ELEM_MOVE_EVENT);
            evt.point={x:this.offsetLeft,y:this.offsetTop};
            this.dispatchEvent(evt);
        }else if(e.type==="mouseup"){
            this.removeEventListener("mouseup",Drag.drageHandler);
            document.removeEventListener("mousemove",Drag.drageHandler);
        }
    }
    static get ELEM_MOVE_EVENT(){
        return "elem_move_event";
    }
}


ZoomClasses.js 

class ZoomClasses{
    constructor(panrent){
        this.bindHandler=this.loadFinishHandler.bind(this);
        document.addEventListener(LoadMethod.LOAD_IMG_FINISH,this.bindHandler);
        this.zoomView=this.createView();
        panrent.appendChild(this.zoomView);
    }
    createView(){
        if(this.zoomView) return this.zoomView;
        let div=document.createElement("div");
        this.minDiv=document.createElement("div");
        this.maxDiv=document.createElement("div");
        this.dragDiv=document.createElement("div");
        Object.assign(div.style,{
            position:"relative",
        });
        this.minDiv.appendChild(this.dragDiv);
        div.appendChild(this.minDiv);
        div.appendChild(this.maxDiv);
        this.dragDiv.addEventListener(Drag.ELEM_MOVE_EVENT,this.moveHandler.bind(this));
        Drag.dragElem(this.dragDiv,null,this.minDiv);
        this.minDiv.style.float=this.maxDiv.style.float="left";
        return div;
    }
    set width(value){
        this._width=value;
        this.render();
    }
    get width(){
        if(!this._width) this._width=0;
        return this._width;
    }
    set height(value){
        this._height=value;
        this.render();
    }
    get height(){
        if(!this._height) this._height=0;
        return this._height;
    }
    set imgSource(value){
        if(!Array.isArray(value))return;
        this._imgSource=value;
        LoadMethod.init(value);
    }
    set left(value){
        this.zoomView.style.left=value+"px";
    }
    set top(value){
        this.zoomView.style.top=value+"px";
    }
    loadFinishHandler(e){
        this.targetList=e.targetList;
       this.width=this.width || e.targetList["min"].width;
       this.height=this.height || e.targetList["min"].height;
 
    }
    moveHandler(e){
        if(!this.targetList || this.targetList.length<2) return;
        let widthScale=this.targetList["min"].width/this.targetList["max"].width;
        let heightScale=this.targetList["min"].height/this.targetList["max"].height;
        Object.assign(this.maxDiv.style,{
            backgroundPositionX:-e.point.x/widthScale+"px",
            backgroundPositionY:-e.point.y/widthScale+"px",
        });
    }
    render(){
        if(!this.targetList || this.targetList.length<2) return;
        Object.assign(this.minDiv.style,{
            width:this.width+"px",
            height:this.height+"px",
 
            border:"1px solid #000000",
 
            backgroundImage:`url(${this.targetList["min"].src})`,
            position:"relative"
        });
        Object.assign(this.maxDiv.style,{
            width:this.width+"px",
            height:this.height+"px",
            backgroundImage:`url(${this.targetList["max"].src})`,
            position:"relative"
        });
        let widthScale=this.targetList["min"].width/this.targetList["max"].width;
        let heightScale=this.targetList["min"].height/this.targetList["max"].height;
        Object.assign(this.dragDiv.style,{
            width:this.width*widthScale+"px",
            height:this.height*heightScale+"px",
            backgroundColor:"rgba(255,0,0,0.2)",
            position:"absolute"
        })
 
    }
}


html: 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="js/LoadMethod.js"></script>
    <script src="js/ZoomClasses.js"></script>
</head>
<body>
    <script>
        let zoom=new ZoomClasses(document.body);
        zoom.imgSource=["img/max.jpg","img/min.jpg"];
 
    </script>
</body>
</html>


来自:https://blog.csdn.net/qlwangcong518/article/details/86497930


链接: https://www.fly63.com/article/detial/1854

原生Js对文件类型的判断,判断文件是图片,视频等格式

在我们开发中,会遇到这样的场景:1.服务器返回Json数据,根据数据类型来显示是图片还是视频。2.前端上传文件,需要指定文件类型才能上传到服务器。这时候就需要使用Js来判断对应文件的类型

前端图片懒加载的实现,采用Lazy Load 图片延迟加载提高用户体验

对于图片过多的页面,为了加速页面加载速度,所以很多时候我们需要将页面内未出现在可视区域内的图片先不做加载, 等到滚动到可视区域后再去加载。这样子对于页面加载性能上会有很大的提升,也提高了用户体验。

web前端图片加载优化,从图片模糊到清晰的实现过程

在网页图片显示的时候,会发现许多网站采用了先模糊,然后在慢慢清晰的过程,这样的加载用户体验是比较好的,那么如何实现呐?默认加载2张图片,一张缩略图,一张原图,当打开网页的时候默认只显示缩略图

lazysizes.js使用方法_实现图片懒加载、延迟加载的js插件

当你的网站使用了大量图片时候,如果一次性全部加载,那么会严重影响网站的速度。通过lazysizes.js插件就能很好解决这个问题,它可以实现图片的延迟加载【懒加载】

js实现图片局部放大效果

图片局部放大效果结合的知识点主要是DOM的操作,以及事件的应用,所以首先要对DOM的操作有一定了解,其次能对事件的应用有一定的累积。

网站图片优化的重要性与技巧方案

网站图片优化技巧:1、图片名包括关键词,2、Alt标签包括关键词,3、图片周边文本包括关键词,4、GLF和JPGE图画优化,5、在图片链接中运用锚文本关键字

css实现不定宽高的图片img居中裁剪_类似微信朋友圈图片效果

前端需要显示矩形的缩略图,接口返回的图片尺寸大小不一,宽高不相等,需要前端来处理并显示成正方形,类似微信朋友圈图片的效果。那么使用纯css该如何实现不定宽高的图片居中裁剪呢?

原生js 生成海报图_利用canvas合成图片的实现方法

目前浏览器对html5的支持越来越好,我们现在不用服务器端,直接在前端利用canvas就可以进行图片的合成了。下面就介绍下如何通过原生js 来生成海报图

网页中默认图片的几种解决方式

现在网页中图片随处可见,但避免不了有时会出现图片资源失败的情况,这里的alt属性是为了当图片加载失败时告诉用户图片信息的 ,能不能美化一下呢?下面给出几种方式

h5移动端禁止长按图片保存

在移动端访问H5页面的时候,长按图片就会把图片保存起来,为了能够让用户体验更好一些,我们需要长按的时候也不保存图片。那该如何实现呢?下面给出3种解决方案。使用 pointer-events:none、全局属性、加一层遮罩层

点击更多...

内容以共享、参考、研究为目的,不存在任何商业目的。其版权属原作者所有,如有侵权或违规,请与小编联系!情况属实本人将予以删除!