首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

js写一个拖动并创设一个新对象

2013-04-05 
js写一个拖动并创建一个新对象例如一个页面下有两个div分别位于左右两边宽度各五百,高六百,在第一个div中

js写一个拖动并创建一个新对象
例如一个页面下有两个div分别位于左右两边宽度各五百,高六百,在第一个div中有个button,点击botton或者拖动左边的button到右边的div,在右边的地中鼠标释放生产一个新的button,(如果是点击左边按钮则,在右边随机位置生产一个button)。在右边的button可以生产很多。ok,question over。实现吧。javascript或者jquery或者jqueryui实现均可
[解决办法]
<!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>无标题文档</title>
<style type="text/css">
div{
float:left;
width:500px;
height:600px;
}
#left{
background-color:#F00;
}
#right{
background-color:#009;
}
input[type="button"]{
width:40px;
}
</style>
<script type="text/javascript">
function init(){
var move=false;
var divx=0;
var divy=0;
var button=document.getElementById("test");
var div=document.getElementById("right");
button.onclick=function(){
var b=button.cloneNode(true);
var x=parseInt(Math.random()*450);
var y=parseInt(Math.random()*600);
div.appendChild(b);
b.style.position="absolute";
b.style.left=500+x+"px";
b.style.top=y+"px";
}
button.onmousedown=function(){
move=true;
}
div.onmousemove=function(e){
var a=e
[解决办法]
window.event;
divx=a.clientX;
divy=a.clientY;
}
div.onmouseup=function(){
if(move){
var b=button.cloneNode(true);
b.style.position="absolute";
b.style.left=parseInt(divx)+"px";
b.style.top=parseInt(divy)+"px";
this.appendChild(b);
}
move=false;
}
document.body.onmouseup=function(){
move=false;
}
}
window.onload=init;
</script>
</head>

<body>
<div id="left">
<input type="button" id="test" value="add">
</div>
<div id="right"></div>
</body>
</html>
大体这样试试
[解决办法]

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>js写一个拖动并创建一个新对象</title>
    <script src="jquery.min.js" type="text/javascript"></script>
    <style type="text/css">
        #left_div,#right_div{
            border:1px solid red;
            width:500px;height: 600px;
            float: left;
        }
        #right_div{position: relative;}
        #move_b,.scs{
            position: absolute;
            width:100px;height:25px;


        }
        #move_b{z-index: 2}
        p{font-size: 12px}
    </style>
</head>
<body>
<div id="left_div">
    <button type="button" id="move_b">移动按钮</button>
    <p style="margin-top:30px">按钮必须完全拖入右边DIV,才生成一个按钮。</p>
    <p>单独点击左按钮,随机在右边生成按钮。</p>
    <p style="color:red">窗口变化造成右边DIV掉下,仍保留功能!</p>
</div>
<div id="right_div"></div>
<script type="text/javascript">
    $(function () {
        var i=0;//生成按钮的编号
        var b_offset=$("#move_b").offset();//拖动按钮原位置
        $("#move_b").mousedown(function (e) {
            var oe=e
[解决办法]
window.event;
            var $this = document.getElementById($(this).attr("id"));
            var startX = oe.clientX - $this.offsetLeft;
            var startY = oe.clientY - $this.offsetTop;
            var move=false;
            document.onmousemove = function (e) {
                var oe = e 
[解决办法]
 window.event;
                $this.style.left = oe.clientX - startX + "px";
                $this.style.top = oe.clientY - startY + "px";
                move=true;
            };
            document.onmouseup = function () {
                document.onmousemove = document.onmouseup = null;
                if(!move){
                    create("");
                }else{
                    create($($this).offset());
                    $($this).animate(b_offset,"fast");
                }
                if ($this.releaseCapture)$this.releaseCapture();
            };


            if ($this.setCapture)$this.setCapture();
            return false;
        });
        function create(offset){
            var r_div=$("#right_div");
            //分别获取按钮可生成时的最大最小坐标
            var min_l=r_div.offset().left;
            var min_t=r_div.offset().top;
            var max_l=min_l+500-100;
            var max_t=min_t+600-25;
            if(""==offset){
                //在指定DIV范围内随机生成按钮
                $('<button type="button" style="top:'+Math.floor(Math.random()*575)+'px;left:'+Math.floor(Math.random()*400)+'px" class="scs">按钮'+i+'</button>').appendTo(r_div);
                i++;
            }else{
                //拖动后必须在指定范围内生成按钮
                if(offset.left>min_l&&offset.left<max_l&&offset.top>min_t&&offset.top<max_t){
                    min_t=offset.top-min_t;
                    min_l=offset.left-min_l;
                    $('<button type="button" style="top:'+min_t+'px;left:'+min_l+'px" class="scs">按钮'+i+'</button>').appendTo(r_div);
                    i++;
                }
            }
        }
    });
</script>
</body>
</html>

热点排行