美文网首页
元素的拖拽.js

元素的拖拽.js

作者: 塔库纳玛哈哈 | 来源:发表于2017-09-03 17:32 被阅读0次
window.onload=function(){
    var div=document.querySelector('div');
    //页面中拖拽
    /*
    div.onmousedown=move;
    function move(e){
        var ox=e.offsetX;
        var oy=e.offsetY;
        var ow=div.offsetWidth;
        var oh=div.offsetHeight;
        var cw=document.documentElement.clientWidth;
        var ch=document.documentElement.clientHeight;
        document.onmousemove=function(e){
            var cx=e.clientX;
            var cy=e.clientY;
            var lefts=cx-ox;
            var tops=cy-oy;
            if(lefts<=0){
                lefts=0
            }
            if(tops<=0){
                tops=0;
            }
            if(lefts>cw-ow){
                lefts=cw-ow;
            }
            if(tops>ch-oh){
                tops=ch-oh;
            }
            div.style.left=lefts+'px';
            div.style.top=tops+'px';
        }
    }
    div.onmouseup=function(){
        document.onmousemove=null;
    }
    */




    //元素拖拽(创建对象的方法封装函数)
    /*
    function Drag(domobj){
        this.domobj=domobj;
        this.cw=document.documentElement.clientWidth;
        this.ch=document.documentElement.clientHeight;
        this.ow=domobj.offsetWidth;
        this.oh=domobj.offsetHeight;
    }
    Drag.prototype={
        drag:function(){
            var that=this;
            this.domobj.onmousedown=function(e){
                that.ox=e.offsetX;
                that.oy=e.offsetY;
                document.onmousemove=function(e){
                    that.cx=e.clientX;
                    that.cy=e.clientY;
                    var lefts=that.cx-that.ox;
                    var tops=that.cy-that.ox;
                    if(lefts<=0){
                        lefts=0;
                    }
                    if(tops<=0){
                        tops=0;
                    }
                    if(lefts>that.cw-that.ow){
                        lefts=that.cw-that.ow;
                    }
                    if(tops>that.ch-that.oh){
                        tops=that.ch-that.oh;
                    }
                    that.domobj.style.left=lefts+'px';
                    that.domobj.style.top=tops+'px';
                }
            }
            this.domobj.onmouseup=function(){
                document.onmousemove=null;
            }
        }
    }
    var obj=new Drag(div);
    var div=document.querySelector('div');
    obj.drag();
    */






 //元素拖拽(创建对象的方法封装函数)

    function Drag(domobj){
        this.domobj=domobj;
        this.cw=document.documentElement.clientWidth;
        this.ch=document.documentElement.clientHeight;
        this.ow=domobj.offsetWidth;
        this.oh=domobj.offsetHeight;
    }
    Drag.prototype={
        drag:function(){
            this.down();
        },
        down:function(){
            var that=this;
            this.domobj.onmousedown=function(e){
                that.ox=e.offsetX;
                that.oy=e.offsetY;
                that.move();
            }
            
            this.up();
        },  
        move:function(){
            var that=this;
            document.onmousemove=function(e){
                that.cx=e.clientX;
                that.cy=e.clientY;
                var lefts=that.cx-that.ox;
                var tops=that.cy-that.ox;
                if(lefts<=0){
                    lefts=0;
                }
                if(tops<=0){
                    tops=0;
                }
                if(lefts>that.cw-that.ow){
                    lefts=that.cw-that.ow;
                }
                if(tops>that.ch-that.oh){
                    tops=that.ch-that.oh;
                }
                that.domobj.style.left=lefts+'px';
                that.domobj.style.top=tops+'px';
            }
        },
        up:function(){
            this.domobj.onmouseup=function(){
                document.onmousemove=null;
            }
        }
    }
    var div=document.querySelector('div');
    var obj=new Drag(div);
    obj.drag();
    
}

相关文章

  • js拖拽html元素工具类

    复制就能用的拖拽js方法 原生js实现拖拽元素方法 使用(注意拖拽目标元素需绝对定位 position: 'abs...

  • 元素的拖拽.js

  • js实现元素拖拽

    被移动元素必须为绝对定位 Dom Javasrtipt

  • js 拖拽元素(vue)

    标题为啥写vue呢?原因我的这个功能是在vue的项目里面的,然而实际上是用js实现的,与vue并无太大的关系。 如...

  • 拖拽上传

    拖拽事件 拖拽元素ondrag 应用于拖拽元素,整个拖拽过程都会调用ondragstart应用于拖拽元素,...

  • HTML5拖拽drag

    通过拖拽实现页面元素的位置改变 实现拖拽效果 源元素 - 要拖拽的文件 目标元素 - 要拖拽到哪里去 目前实现拖拽...

  • 元素拖曳 H5

    拖拽 如何让一个元素变成拖拽元素 draggable=‘true’ ondrag 应用于拖拽元素,整个拖拽过程都会...

  • Drag Drop---API

    在h4标准中,我们定义个拖拽事件是很麻烦的,用原生的js来实现拖拽的效果。我们需要计算拖拽的元素的坐标位置和距离之...

  • 拖拽元素JS实现方法

    思路: onmousedown :鼠标按下的时候,记录鼠标所在的位置pageX,pageY记录鼠标相对于盒子的位置...

  • 实现可拖拽插件

    一、js拖拽插件的原理常见的拖拽操作是什么样的呢?整过过程大概有下面几个步骤: 1、用鼠标点击被拖拽的元素 2、按...

网友评论

      本文标题:元素的拖拽.js

      本文链接:https://www.haomeiwen.com/subject/wssjjxtx.html