Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-11-2003, 11:50 PM   PM User | #1
cwl157
New Coder

 
Join Date: Jun 2003
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
cwl157 is an unknown quantity at this point
javascript with images

is there a way for the user to control images in javascript. For example like use the arrow keys to move a picture around the screen or click a picture with the mouse and drag it around the screen. If there is can someone explain to me how to do it.
thanks
cwl157 is offline   Reply With Quote
Old 08-12-2003, 12:32 AM   PM User | #2
scriptkeeper
Regular Coder

 
Join Date: Apr 2003
Location: Northern California
Posts: 169
Thanks: 0
Thanked 0 Times in 0 Posts
scriptkeeper is an unknown quantity at this point
Courtisy of Mr. J

<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:100p
x;width:100px; background:#FFFF00"> </div>

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

<div id="dis4" onmousedown="dragnow(this.id)" style=" position:absolute;top:100px;left:500px;height:100p
x;width:100px; background:#0000FF"></div>
scriptkeeper is offline   Reply With Quote
Old 08-12-2003, 03:54 AM   PM User | #3
cwl157
New Coder

 
Join Date: Jun 2003
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
cwl157 is an unknown quantity at this point
what about using the arrow keys

is there a way to use the arrow keys or other keys to move or create images. i was also wondering what does the ! mark in javascripts do.
thanks
ps the script you gave me works perfectly for dragging the boxes thanks for it.

Last edited by cwl157; 08-12-2003 at 03:56 PM..
cwl157 is offline   Reply With Quote
Old 08-12-2003, 07:28 PM   PM User | #4
shlagish
Senior Coder

 
Join Date: Apr 2003
Location: Canada
Posts: 1,063
Thanks: 2
Thanked 0 Times in 0 Posts
shlagish is an unknown quantity at this point
The ! mark means "does not" or "is not"
It's basically a negation sign I guess

In this example:

tempX = (!ns) ? event.clientX : e.pageX;
He creates a variable (tempX)
If ns is not True (not beign the ! mark), tempX will be event.clientX, otherwise, it'll be e.pageX

Which brings me to my own question:

wouldn't
tempX = (ns) ? e.pageX : event.clientX
do the same thing?
__________________
Shawn
shlagish is offline   Reply With Quote
Old 08-12-2003, 11:32 PM   PM User | #5
cwl157
New Coder

 
Join Date: Jun 2003
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
cwl157 is an unknown quantity at this point
is there something for true

So if ! means no or false is there something in javascript that means yes or true.
cwl157 is offline   Reply With Quote
Old 08-13-2003, 04:36 AM   PM User | #6
shlagish
Senior Coder

 
Join Date: Apr 2003
Location: Canada
Posts: 1,063
Thanks: 2
Thanked 0 Times in 0 Posts
shlagish is an unknown quantity at this point
Putting nothing:
var x=1
if(x==1){
do something
}
else{
do something else
}

var x=1
if(x!=1}
do something else
}
else{
do something
}
__________________
Shawn
shlagish is offline   Reply With Quote
Old 08-13-2003, 08:57 AM   PM User | #7
cwl157
New Coder

 
Join Date: Jun 2003
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
cwl157 is an unknown quantity at this point
does anyone know

Oh yeah that makes sense thanks and sorry for the stupid question but does anyone know how to make an image move or create in image with keyboard commands?
Thanks

Last edited by cwl157; 08-14-2003 at 03:52 AM..
cwl157 is offline   Reply With Quote
Old 08-14-2003, 11:37 PM   PM User | #8
cwl157
New Coder

 
Join Date: Jun 2003
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
cwl157 is an unknown quantity at this point
no one knows

No one knows?
cwl157 is offline   Reply With Quote
Old 08-15-2003, 03:00 AM   PM User | #9
shlagish
Senior Coder

 
Join Date: Apr 2003
Location: Canada
Posts: 1,063
Thanks: 2
Thanked 0 Times in 0 Posts
shlagish is an unknown quantity at this point
I once saw something like this:

if(e.which==120)
do something
else if(e.which==122)
do something else.

120 is the unicode to "x" and 122 is the unicode to "z".

So by using unicodes, you can trigger functions (move an image for example).

Maybe this will give you an idea.
If you want, I can elaborate on this technique, but I don't have the time today...
__________________
Shawn
shlagish is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:13 PM.


Advertisement
Log in to turn off these ads.