zoukankan      html  css  js  c++  java
  • js拖动效果

    js拖动效果

    原理

      主要思路是鼠标按下,鼠标按下并移动,鼠标松开。以上步骤对应的JS事件就是onmousedown,onmousemove,onmouseup。

    实现代码

            function dragElement(id) {
                this.dom = document.getElementById(id);
                this.isMouseDown = false;
                this.pos = null;
            }
    
            dragElement.prototype = {
                init: function() {
                    var _this = this;
                    this.dom.onmousedown = function(e) {
                        _this.isMouseDown = true;
                        _this.getPos(e);
                    };
                    this.dom.onmouseup = function() {
                        _this.isMouseDown = false;
                    };
                    document.onmousemove = function(e) {
                        _this.move(e);
                    };
                },
                getPos: function(e) {
                    e = e || window.event;
                    this.pos = {
                        x: e.clientX - this.dom.offsetLeft,
                        y: e.clientY - this.dom.offsetTop
                    };
                },
                move: function(e) {
                    var _this = this;
                    this.dom.style.opactiy
                    if (this.isMouseDown) {
                        this.dom.style.left = e.clientX - this.pos.x + "px";
                        this.dom.style.top = e.clientY - this.pos.y  + "px";
                    }
                }
            }
    
            var d = new dragElement("m");
            d.init();
    

    修改

        init: function() {
            var _this = this;
    
            this.moveDom.onmousedown = function(e) {
                _this.isMouseDown = true;
                _this.getPos(e);
    
                document.onmouseup = function() {
                    _this.isMouseDown = false;
                    document.onmousemove = null;
                    document.onmouseup = null;
                };
    
                document.onmousemove = function(e) {
                    _this.move(e);
                };
            };
        }
    

     

     

  • 相关阅读:
    CFS 调度器
    RCU
    linux cfs 负载均衡
    wait_event_interruptible_timeout
    算法(13)Contiguous Array
    算法(12)Pascal's Triangle II
    算法(12)Best Time to Buy and Sell Stock II
    算法(11)Find All Duplicates in an Array
    算法(10)Subarray Sum Equals K
    算法(9)Find the Duplicate Number
  • 原文地址:https://www.cnblogs.com/xqhppt/p/2108905.html
Copyright © 2011-2022 走看看