第一百三十七節,JavaScript,封裝庫---修繕拖拽

JavaScript,封裝庫---修繕拖拽css

修繕拖拽html

/** tuo_zhuai()方法,將一個彈窗元素實現拖拽功能
 * 注意:有參設置拖拽點區塊,只有彈窗的這個拖拽點區塊才能拖拽,無參整個彈窗能夠拖拽
 * 注意:通常須要在css文件將彈窗元素裏的某一個區塊光標設置成提示能夠拖拽,如:cursor: move; (設置拖拽點)
 * 有一個參數,參數是彈窗元素裏的拖拽點區塊的字符串class值(設置拖拽點的class值)設置後彈窗元素裏的這個拖拽點區塊才能拖拽
 **/
feng_zhuang_ku.prototype.tuo_zhuai = function (tuo_zhuai_dian) {
    if (this.jie_dian.length == 1) {
        var yan_su = null;
        var sum = arguments.length;
        for (var i = 0; i < this.jie_dian.length; i++) {
            yan_su = this.jie_dian[i];
        }
        addEvent(yan_su, 'mousedown', function (e) {
            if (trim(yan_su.innerHTML).length == 0)e.preventDefault();
            var e1 = getEvent(e);  //getEvent()函數庫函數,跨瀏覽器獲取事件對象,事件event對象,
            var diffx = e1.clientX - yan_su.offsetLeft;
            var diffy = e1.clientY - yan_su.offsetTop;
            if (sum == 1) {
                if (e.target.className === tuo_zhuai_dian) {
                    addEvent(document, 'mousemove', move);
                    addEvent(document, 'mouseup', up);
                }
            } else if (sum == 0) {
                addEvent(document, 'mousemove', move);
                addEvent(document, 'mouseup', up);
            }
            function move(e) {
                var e2 = getEvent(e);
                var left = e2.clientX - diffx;
                var top = e2.clientY - diffy;
                if (left < 0) {
                    left = 0;
                } else if (left > getInner().width - yan_su.offsetWidth) {
                    left = getInner().width - yan_su.offsetWidth;
                }
                if (top < 0) {
                    top = 0;
                } else if (top > getInner().height - yan_su.offsetHeight) {
                    top = getInner().height - yan_su.offsetHeight;
                }
                yan_su.style.left = left + 'px';
                yan_su.style.top = top + 'px';
                if (typeof yan_su.setCapture != 'undefined') {
                    yan_su.setCapture();
                }
            }

            function up() {
                removeEvent(document, 'mousemove', move);
                removeEvent(document, 'mouseup', up);
                if (typeof yan_su.releaseCapture != 'undefined') {
                    yan_su.releaseCapture();
                }
            }
        });
    } else {
        alert("將一個彈窗元素實現拖拽功能,只能設置一個彈窗元素,目前jie_dian數組裏是多個元素請檢查!")
    }
    return this;
};

 

 

前臺代碼數組

html代碼瀏覽器

<div id="login">
    <h2 class="tbu"><img src="img/close.png" alt="" class="close" />網站登陸</h2>
    <div class="user">賬 號:<input type="text" name="user" class="text" /></div>
    <div class="pass">密 碼:<input type="password" name="pass" class="text" /></div>
    <div class="button"><input type="button" class="submit" value="" /></div>
    <div class="other">註冊新用戶 | 忘記密碼?</div>
</div>

前臺js代碼函數

//拖拽
    $().huo_qu_id('login').tuo_zhuai('tbu');
相關文章
相關標籤/搜索