Solved

DHTML moving Image question

Posted on 2010-08-21
10
642 Views
Last Modified: 2012-05-10
Hello

I am tying to move an image to right direction very slowly and then return it back to left also very slowly.
W3Schools has a tutorial, but in the tutorial the movement happens very quickly. The W3Schools url that I am talking about is:

http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_img_hspace

The code is at below, slightly modified by me to show the movement, but the movement happens very fast. How to do the movement slowly so that the image moves to right direction very slowly then comes back to left very slowly.


<html>
<head>
<script type="text/javascript">
function setSpace()
{
document.getElementById("compman").hspace="400";
document.getElementById("compman").vspace="70";
alert("HELLO");
set();
}
function set()
{
document.getElementById("compman").hspace="0";
document.getElementById("compman").vspace="0";

}
</script>
</head>
<body>

<img id="compman" src="http://www.w3schools.com/jsref/compman.gif" alt="Computerman" width="107" height="98" />
<p>Some text. Some text. Some text. Some text.</p>

<input type="button" onclick="setSpace()" value="Set hspace and vspace" />

</body>
</html>

.....................

If you are attending this question, please also try to put your guess, advise, and comment on my other question:
http://www.experts-exchange.com/Programming/Languages/Scripting/AJAX/Q_26415372.html
0
Comment
Question by:ashley2009
  • 5
  • 5
10 Comments
 
LVL 51

Expert Comment

by:HainKurt
ID: 33493693
what about this?
<html>
<head>
<script type="text/javascript">
var dir = 1;
function setSpace() {
 var img = document.getElementById("compman");
 if ( img.hspace == 200 ) dir = -1;
 if ( img.hspace == 0 ) dir = 1;
 document.getElementById("compman").hspace = img.hspace + 5*dir;
 setTimeout("setSpace()", 50);
}
</script>
</head>
<body>

<img id="compman" src="http://www.w3schools.com/jsref/compman.gif" alt="Computerman" width="107" height="98" />
<p>Some text. Some text. Some text. Some text.</p>

<input type="button" onclick="setSpace()" value="Set hspace and vspace" />

</body>
</html>

Open in new window

0
 
LVL 3

Author Comment

by:ashley2009
ID: 33493815
Hello Hainkurt,

thank you for your response, and the image moves very slowly. Please note that the example does not work at Firefox but works at IE, Safari, Opera, and Chrome. I guess that the above example does not work at Firefox as the hspace attribute got deprecated....[http://www.w3schools.com/TAGS/att_img_hspace.asp]

Therefore, would you please see this code of W3schools.com. The example is at:

http://www.w3schools.com/dhtml/tryit.asp?filename=trydhtml_img_shake

Here the code is moving the image by changing the image's style.left property, but it happens very first.

What to do so that the image moves very slowly.



<html>
<head>
<script type="text/javascript">
function shakeleft()
{
document.getElementById('image').style.position="relative";
document.getElementById('image').style.left="400";
timer=setTimeout("shakeright()",10);
}

function shakeright()
{
document.getElementById('image').style.left="0";
timer=setTimeout("shakeleft()",10);
}

function stoptimer()
{
clearTimeout(timer);
}
</script>
</head>

<body>
<b>Mouse over the image to see it shake</b><br />
<img id="image" src="smiley.gif"
onmouseover="shakeleft()"
onmouseout="stoptimer()" width="32" height="32" />
</body>

</html>




0
 
LVL 3

Author Comment

by:ashley2009
ID: 33493832
I would also to add that if you know how to make your code work at firefox, where vspace gets used,

please let me know. I am not sure why it is not working at firefox: ( W3Schools's example:

http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_img_hspace) W3School's example using vspace is working.....

Please tell me why it is not working in firefox.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 51

Accepted Solution

by:
HainKurt earned 500 total points
ID: 33493859
try this
<html>
<head>
<script type="text/javascript">
var dir = 1;

function setSpace() {
 var img = document.getElementById("compman");
 var l = img.style.left.replace("px","").replace("pt","");
 if ( l == "200" ) dir = -1;
 if ( l == "0" ) dir = 1;
 img.style.left = l*1 + 5*dir;
  setTimeout("setSpace()", 50);
}
</script>
</head>
<body>

<img id="compman" style="position:relative; left:0" src="http://www.w3schools.com/jsref/compman.gif" alt="Computerman" width="107" height="98" />
<p>Some text. Some text. Some text. Some text.</p>

<input type="button" onclick="setSpace()" value="Set hspace and vspace" />

</body>
</html>

Open in new window

0
 
LVL 3

Author Comment

by:ashley2009
ID: 33493873
I also tried your code at  ID: 33493693 by putting quotation

if ( img.hspace == "200" )
            dir = -1;
       if ( img.hspace == "0" )
            dir = 1;

but not working at Firefox.

0
 
LVL 51

Expert Comment

by:HainKurt
ID: 33493880
this one works with FF & IE
<html>
<head>
<script type="text/javascript">
var dir = 1;

function setSpace() {
 var img = document.getElementById("compman");
 var l = img.style.left.replace("px","").replace("pt","");
 if ( l == "200" ) dir = -1;
 if ( l == "0" ) dir = 1;
 img.style.left = l*1 + 5*dir;
  setTimeout("setSpace()", 50);
}
</script>
</head>
<body>

<img id="compman" style="position:relative; left:0" src="http://www.w3schools.com/jsref/compman.gif" alt="Computerman" width="107" height="98" />
<p>Some text. Some text. Some text. Some text.</p>

<input type="button" onclick="setSpace()" value="Set hspace and vspace" />

</body>
</html>

Open in new window

0
 
LVL 3

Author Comment

by:ashley2009
ID: 33493888
Hello,

I am sorry. Your initial solution was working. What happened let me explain to you.

It seems that in Firefox the image hpace starts from -1 while in other browsers, the hspace starts from 0. Therefore, the code was never reaching
( l == "200" )


But now it works in all browser:

<html>
<head>
<script type="text/javascript">
var dir = 1;
function setSpace()
{
       var img = document.getElementById("compman");
      //alert(document.getElementById("compman").hspace);
       if ( img.hspace.toString() > 200 ){//alert(img.hspace.toString());
            dir = -1;
}
       if ( img.hspace.toString() == 0 )
            dir = 1;
       
      document.getElementById("compman").hspace = img.hspace + 5*dir;
       setTimeout("setSpace()", 50);
}
</script>
</head>
<body>

<img id="compman" src="http://www.w3schools.com/jsref/compman.gif" alt="Computerman" width="107" height="98" />
<p>Some text. Some text. Some text. Some text.</p>

<input type="button" onclick="setSpace()" value="Set hspace and vspace" />

</body>
</html>


0
 
LVL 51

Expert Comment

by:HainKurt
ID: 33493889
also this one works IE & FF
<html>
<head>
<script type="text/javascript">
var dir = 1;
function setSpace() {
 var img = document.getElementById("compman");
//alert("hs = " + img.hspace);
 var l = (img.hspace + "").replace("px","").replace("pt","");
 if (l<0) l=0;
//alert("l = " + l);
 if ( l == "200" ) dir = -1;
 if ( l == "0" ) dir = 1;
 inpLeft.value = l;
 img.hspace = l*1 + 5*dir;
 setTimeout("setSpace()", 50);
}
</script>
</head>
<body>

<img id="compman" src="http://www.w3schools.com/jsref/compman.gif" alt="Computerman" width="107" height="98" />
<p>Some text. Some text. Some text. Some text.</p>

<input type="button" onclick="setSpace()" value="Set hspace and vspace" />
<input id=inpLeft>
</body>
</html>

Open in new window

0
 
LVL 51

Expert Comment

by:HainKurt
ID: 33493892
;) I noticed that, now I have posted solutions with hspace & style.left (both working IE & FF)
0
 
LVL 3

Author Comment

by:ashley2009
ID: 33493912
Hello thank you for seeing and letting me know the solution of other problem, which works in all browser.

Please note that all of solution works in all browser. Thank you.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question