Jquery拖動外掛程式,可以任意拖動任意東西

來源:互聯網
上載者:User

剛才看到CSDN的Javascript分類有人問這個問題,所以我就一時心血來潮寫了這個小外掛程式,順便拿出來分享一下.

這個jquery外掛程式理論上可以用來拖動任何的元件,例如常見的div,table等都能拖來拖去.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>http://hi.baidu.com/see7di/home</title>
<script type='text/javascript' src="img/jquery-1.5.2.min.js"></script>

<script type='text/javascript'>
(function($){
    //拖拽外掛程式,參數:id或object
    $.Move = function(_this){
        if(typeof(_this)=='object'){
            _this=_this;
        }else{
            _this=$("#"+_this);
        }
        if(!_this){return false;}

        _this.css({'position':'absolute'}).hover(function(){$(this).css("cursor","move");},function(){$(this).css("cursor","default");})
        _this.mousedown(function(e){//e滑鼠事件歡迎轉載但請註明出處Email:See7di@Gmail.com,Seven的部落格http://hi.baidu.com/see7di/home

            var offset = $(this).offset();
            var x = e.pageX - offset.left;
            var y = e.pageY - offset.top;
            _this.css({'opacity':'0.3'});
            $(document).bind("mousemove",function(ev){//綁定滑鼠的移動事件,因為游標在DIV元素外面也要有效果,所以要用doucment的事件,而不用DIV元素的事件
                _this.bind('selectstart',function(){return false;});
                var _x = ev.pageX - x;//獲得X軸方向移動的值歡迎轉載但請註明出處Email:See7di@Gmail.com,Seven的部落格http://hi.baidu.com/see7di/home
                var _y = ev.pageY - y;//獲得Y軸方向移動的值
                _this.css({'left':_x+"px",'top':_y+"px"});
            });
        });

        $(document).mouseup(function(){
            $(this).unbind("mousemove");
            _this.css({'opacity':''});
        })
    };
})(jQuery)
//外掛程式調用歡迎轉載但請註明出處Email:See7di@Gmail.com,Seven的部落格http://hi.baidu.com/see7di/home
$(function(){
    $.Move('m1');
});
</script>
</head>
<body>
<style type='text/css'>
    #m1{border:1px solid;}
</style>
<div id="m1">
<ul>
    <li><a href="">菜單一</a>
    <ul>
        <li><a href="">子功能表1</a></li>
        <li><a href="">子功能表2</a><ul><li><a href="">子功能表7</a></li></ul></li>
        <li><a href="">子功能表3</a></li>
    </ul>
    </li>
    <li><a href="">菜單二</a>
        <ul>
        <li><a href="">子功能表4</a></li>
        <li><a href="">子功能表5</a></li>
        <li><a href="">子功能表6</a></li>
        </ul>
    </li>
</ul>
</div>
</body>
</html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.