PDA

View Full Version : جابجایی کنترل



mona11
سه شنبه 14 دی 1389, 10:41 صبح
سلام دوستان.میخوام یه کنترل رو با کلیک چپ موس بکشم اینور اونور.این قسمت از کد رو نمیشناسه.(event.courceElement.id)

<html>
<head>
<title>Title of Page</title>
<script language="javascript">

function sayHi(event)
{

if (event.srcElement.id == "but1" && event.which == 1) {



document.getElementById("but1").style.left = event.clientX;
document.getElementById("but1").style.top = event.clientY;

}

}


</script>
</head>
<body ">
<input type="button" id="but1" style="position:absolute;" value="Call Function" onclick="sayHi(event)" />
</body>
</html>

hossin.esm
سه شنبه 14 دی 1389, 15:27 عصر
<html>
<head>
<title>Title of Page</title>
<script language="javascript">
var el;
function down(th,event)
{

if (th.getAttribute('id') == "but1" && (event.which == 1 || window.event.button==1)) {

el= th;
}
}
function up(event)
{
if(el)
{
if (el.getAttribute('id') == "but1" && (event.which == 1 || window.event.button==1)) {



document.getElementById("but1").style.left = event.clientX;
document.getElementById("but1").style.top = event.clientY;
el=null;

}
}
}

</script>
</head>
<body onMouseUp="up(event)" >
<input type="button" id="but1" style="position:absolute;" value="Call Function" onMouseDown="down(this,event)"
</body>
</html>

mona11
سه شنبه 14 دی 1389, 21:44 عصر
<html>
<head>
<title>Title of Page</title>
<script language="javascript">
var el;
function down(th,event)
{

if (th.getAttribute('id') == "but1" && (event.which == 1 || window.event.button==1)) {

el= th;
}
}
function up(event)
{
if(el)
{
if (el.getAttribute('id') == "but1" && (event.which == 1 || window.event.button==1)) {



document.getElementById("but1").style.left = event.clientX;
document.getElementById("but1").style.top = event.clientY;
el=null;

}
}
}

</script>
</head>
<body onMouseUp="up(event)" >
<input type="button" id="but1" style="position:absolute;" value="Call Function" onMouseDown="down(this,event)"
</body>
</html>
ممنون حسین جان.فقط نمیشه وقتی کلیک موس رو نگه داشتیم،خود کنترل(button) هم،همزمان با نگه داشتن موس باهاش حرکت کنه؟یه سوال دیگه؟اونم اینکه تو تابع down ،خط el=th یعنی چی؟

hossin.esm
سه شنبه 14 دی 1389, 22:00 عصر
el=th برای نگه داری element ی است که روی آن کلید فشرده شده تا در تابع down از آن استفاده شود.
در رابطه با حرکت همزمان اگر درستش کردم کدش را برات میذارم.

hossin.esm
سه شنبه 14 دی 1389, 22:50 عصر
<html>
<head>
<title>Title of Page</title>
<script language="javascript">
document.onmouseup = up;
document.onmousemove=move;
var el;

function down(th,ev)
{

if (th.getAttribute('id') == "but1" && (ev.which == 1 || window.event.button==1)) {

el= th;
}
}
function up()
{

el=null;


}

function move(ev)
{
if(el)
{
if (el.getAttribute('id') == "but1")
{
ev =ev || window.event;


document.getElementById("but1").style.left = ev.clientX;
document.getElementById("but1").style.top = ev.clientY;


}
}
}

</script>
</head>
<body >
<input type="button" id="but1" style="position:absolute;" value="Call Function" onMouseDown="down(this,event)" />
</body>
</html>