JQuery插件第三十二个:拖动某个元素并通过提示框告知该元素框位置

/*
* 作者:黑曜石.C 2010-04-22
* 版权没有,随便使用!
* 改编:chinet_bridge 2012-02-22
*/
(function ($) {
    $.fn.dragTip = function (options) {
        var defaults = {
            opacity: 0.6
        };
        var opts = $.extend(defaults, options);
		//主要执行过程		        
        this.each(function(){
            var obj=$(this);	                  
            //add css to the dBox
            function addCSS() {
                obj.css({"position":"absolute","left": obj.offset().left, "top": obj.offset().top});
            }
		//位置框位置
		var tiPos = function(){
			T = obj.offset().top;
			L = obj.offset().left;
			var pt=$("#posTitle");
			pt.css({"top":parseInt(T) - 22 + "px",
			"left": parseInt(L) +obj.width() + "px",
			"display":"block"
			}).text("top:"+T+"px, left:"+L+"px");
		}
		//添加位置框样式
		$("body").append("<style> #posTitle{display:none; height:22px; width:36px; position:absolute;border:1px solid #aaa;'}</style><div id='posTitle' ></div>")

        //drag the dBox
        //this event contains four events(handle.mousedown,move,out,up)
        function _drag() {
            var dx, dy, moveout;
            var handle = obj.find("*").css('cursor', 'move');
            handle.mousedown(function (e) {
                //cal the distance between e and dBox
                dx = e.clientX - parseInt(obj.css("left"));
                dy = e.clientY - parseInt(obj.css("top"));
                //bind mousemove event and mouseout event to the dBox
                obj.mousemove(move).mouseout(out).css({ "opacity": opts.opacity });
                handle.mouseup(up);
            });
            move = function (e) {
                moveout = false;
                win = $(window);
                var x, y;
                if (e.clientX - dx < 0) {
                    x = 0;
                } else {
                    if (e.clientX - dx > (win.width() - obj.width())) {
                        x = win.width() - obj.width();
                    } else {
                        x = e.clientX - dx;
                    }
                }
                if (e.clientY - dy < 0) {
                    y = 0;
                } else {
                    y = e.clientY - dy;
                }
                obj.css({
                    left: x,
                    top: y
                });
                tiPos();
            }
            out = function (e) {
                moveout = true;
                setTimeout(function () {
                    moveout && up(e);
                }, 10);
            }
            up = function (e) {
                obj.unbind("mousemove", move).unbind("mouseout", out).css("opacity", 1);
                handle.unbind("mouseup", up);
            }
        }
        addCSS();
        _drag();
        });			
    };
})(jQuery);
拖动某个元素并通过提示框告知该元素框位置
<!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>
    <title></title>
    <script type="text/javascript" src="../jquery-1.6.js"></script>
    <script type="text/javascript" src="jQuery.dragTip.js" charset="utf-8" ></script>
    <script type="text/javascript">
        $(function () {
            $("#dbox4").dragTip();
        });
    </script>
</head>
<body>
    <div id="dbox4" style="height: 60px; width:50px; background-color:Aqua;" >
    <div>改编自黑耀石.C的拖动特效</div>    
    <input type="button" id="dbox3" value="拖动" />
    </div>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值