最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用js實(shí)現(xiàn)的簡單拖拽效果

 更新時(shí)間:2015年03月18日 09:48:42   投稿:hebedich  
本文給大家分享的是使用純JS實(shí)現(xiàn)的簡單的拖拽效果的插件,算是對(duì)自己javascript學(xué)習(xí)的一個(gè)小的檢驗(yàn),如果小伙伴們需要復(fù)雜的拖拽效果,還是考慮jQuery的draggable吧,更成熟一些。

前端開發(fā)的時(shí)候,有好多地方用到拖拽效果,當(dāng)然 http://jqueryui.com/draggable/  是個(gè)不錯(cuò)的選擇,but 我是個(gè)打破砂鍋問到底的人,抽點(diǎn)時(shí)間用js小小的實(shí)現(xiàn)了類似的插件,話不多說。

 first: html和css

復(fù)制代碼 代碼如下:

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        #div1 {
            position: absolute;
            width: 100px;
            height: 100px;
            cursor: move;
            background-color: red;
        }
    </style>
</head>
<body>
    <div id="div1">
    </div>
</body>
</html>

now,先把主要算法實(shí)現(xiàn)一下:

復(fù)制代碼 代碼如下:

 <script>
        window.onload = function () {
            //獲取需要拖拽的div
            var div1 = document.getElementById("div1");
            //添加鼠標(biāo)按下事件
            div1.onmousedown = function (evt) {
                var oEvent = evt || event;
                //獲取按下鼠標(biāo)到div left  top的距離
                var distanceX = oEvent.clientX - div1.offsetLeft;
                var distanceY = oEvent.clientX - div1.offsetTop;
                //添加doc滑動(dòng)時(shí)間
                document.onmousemove = function (evt) {
                    var oEvent = evt || event;
                    //重新計(jì)算div的left top值
                    var left = oEvent.clientX - distanceX;
                    var top = oEvent.clientY - distanceY;
                    //left  當(dāng)小于等于零時(shí),設(shè)置為零 防止div拖出document之外
                    if (left <= 0) {
                        left = 0;
                    }
                    //當(dāng)left 超過文檔右邊界  設(shè)置為右邊界
                    else if (left >= document.documentElement.clientWidth - div1.offsetWidth) {
                        left = document.documentElement.clientWidth - div1.offsetWidth;
                    }
                    if (top <= 0) {
                        top = 0;
                    }
                    else if (top >= document.documentElement.clientHeight - div1.offsetHeight) {
                        top = document.documentElement.clientHeight - div1.offsetHeight;
                    }
                    //重新給div賦值
                    div1.style.top = top + "px";
                    div1.style.left = left + "px";
                }
                //添加鼠標(biāo)抬起事件
                div1.onmouseup = function () {
                    //清空事件
                    document.onmousemove = null;
                    div1.onmouseup = null;
                }
            }
        }
    </script>

yeah,使用面向?qū)ο髮?shí)現(xiàn)一下

復(fù)制代碼 代碼如下:

<style>
        * {
            margin:0;
            padding:0;
        }
        #div1 {
            width: 100px;
            height: 100px;
            background-color: red;
        }
        #div2 {
            background-color:yellow;
            width:100px;
            height:100px;
        }
    </style>
<div id="div1"></div>
<div id="div2"></div>

js Draggle class:

復(fù)制代碼 代碼如下:

 function Drag(id) {
            this.div = document.getElementById(id);
            if (this.div) {
                this.div.style.cursor = "move";
                this.div.style.position = "absolute";
            }
            this.disX = 0;
            this.disY = 0;
            var _this = this;
            this.div.onmousedown = function (evt) {
                _this.getDistance(evt);
                document.onmousemove = function (evt) {
                    _this.setPosition(evt);
                }
                _this.div.onmouseup = function () {
                    _this.clearEvent();
                }
            }
        }
        Drag.prototype.getDistance = function (evt) {
            var oEvent = evt || event;
            this.disX = oEvent.clientX - this.div.offsetLeft;
            this.disY = oEvent.clientY - this.div.offsetTop;
        }
        Drag.prototype.setPosition = function (evt) {
            var oEvent = evt || event;
            var l = oEvent.clientX - this.disX;
            var t = oEvent.clientY - this.disY;
            if (l <= 0) {
                l = 0;
            }
            else if (l >= document.documentElement.clientWidth - this.div.offsetWidth) {
                l = document.documentElement.clientWidth - this.div.offsetWidth;
            }
            if (t <= 0) {
                t = 0;
            }
            else if (t >= document.documentElement.clientHeight - this.div.offsetHeight) {
                t = document.documentElement.clientHeight - this.div.offsetHeight;
            }
            this.div.style.left = l + "px";
            this.div.style.top = t + "px";
        }
        Drag.prototype.clearEvent = function () {
            this.div.onmouseup = null;
            document.onmousemove = null;
        }

at last:最終實(shí)現(xiàn):

復(fù)制代碼 代碼如下:

  window.onload = function () {
            new Drag("div1");
            new Drag("div2");
        }

效果如下:

以上所述就是本文的全部內(nèi)容了,希望能對(duì)大家更加熟練的掌握javascript有所幫助。

相關(guān)文章

最新評(píng)論

余干县| 沁源县| 绥德县| 子长县| 宜昌市| 平遥县| 曲阳县| 炉霍县| 新干县| 东安县| 依兰县| 曲靖市| 伊金霍洛旗| 肥东县| 宜丰县| 新乡县| 纳雍县| 广宗县| 临汾市| 华坪县| 和政县| 承德市| 泰来县| 塔城市| 抚顺县| 康平县| 临桂县| 遵义县| 华宁县| 徐州市| 达拉特旗| 西乌珠穆沁旗| 台中市| 萨迦县| 英德市| 北海市| 桐庐县| 泸溪县| 义马市| 和龙市| 凤凰县|