/    Sign up×
Community /Pin to ProfileBookmark

please look at the following code:
function don()
{
if(event.srcElement.className== ‘myClass’&& event.button==1 )
{

event.srcElement.style.pixelLeft = event.x;

//event.srcElement.style.pixelTop = event.clientY;

}
}
//–>
</script>
</HEAD>
<body onmousemove=”don()” MS_POSITIONING=”GridLayout”>
</body>

It is so that the usr can move the element witv the mouse. Why does it go only to the right? Please help with a simple code

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@JonaJul 23.2003 — [font=arial][color=maroon]The code will only work in Internet Explorer.[/color][/font]

[font=monospace]

function don(){

if(event.srcElement.className=="myClass" && event.button==1){

event.srcElement.style.pixelLeft=event.x;

event.srcElement.style.pixelTop=event.y;

}

}

</script>

[/font]

[b][J]ona[/b]
Copy linkTweet thisAlerts:
@Mr_JJul 23.2003 — You need a little bit more coding in there.

The following works in IE5.5+ and NS7


<script type="text/javascript">

<!--

var ns=document.getElementById&&!document.all

var moving = false;

var tempX = 0;

var tempY = 0;

var dex=""

function dragnow(id) {

elm_id = document.getElementById(id)

if(dex!=""){

dex.style.zIndex = ""

}

moving=true

posX = tempX - elm_id.offsetLeft;

posY = tempY - elm_id.offsetTop;

elm_id.style.zIndex = 1

dex=elm_id

}

function moveto(e) {

tempX = (!ns) ? event.clientX : e.pageX;

tempY = (!ns) ? event.clientY : e.pageY;

if(moving != false) {

elm_id.style.left = (tempX - posX) + "px";

elm_id.style.top = (tempY - posY) + "px";

}

}

document.onmousemove = moveto;

document.onmouseup=new Function("moving=false");

//-->

</script>

<div id="dis1" onmousedown="dragnow(this.id)" style="position:absolute;top:100px;left:50px;height:100px;width:100px; background:#00FF00"> </div>

<div id="dis2" onmousedown="dragnow(this.id)" style="position:absolute;top:100px;left:200px;height:100px;width:100px; background:#FFFF00"> </div>

<div id="dis3" onmousedown="dragnow(this.id)" style="position:absolute;top:100px;left:350px;height:100px;width:100px; background:#FF0000"></div>

<div id="dis4" onmousedown="dragnow(this.id)" style="position:absolute;top:100px;left:500px;height:100px;width:100px; background:#0000FF"></div>
Copy linkTweet thisAlerts:
@donbesauthorJul 24.2003 — please can u briefly do some comments for the upper part of the code where we have "offsetLeft etc?

Thanks
Copy linkTweet thisAlerts:
@Mr_JJul 24.2003 — Hope this helps


Using an object size of 100 x 100

posX = tempX - elm_id.offsetLeft;

posY = tempY - elm_id.offsetTop;

Taking that the object is positioned at (200,100) if the cursor is positioned at the center of the object the cursor position will be

cursor positionx = 250

cursor positiony =150

Therefore the cursor position within the object is

posX = 250 - 200 = 50

posY = 150 - 100 = 50

The cursor position within the window

tempX = (!ns) ? event.clientX : e.pageX

tempY = (!ns) ? event.clientY : e.pageY;

which gives

tempX = 250 cursor positionx

tempY= 150 cursor positiony


The position of the object

elm_id.style.left = (250 - 50) + "px";

elm_id.style.top = (150 - 50) + "px";

As the mouse is moved these calculation are run and the object is so positioned
×

Success!

Help @donbes spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.30,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...