Solved

DHTML moving Image question

Posted on 2010-08-21
10
649 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 55

Expert Comment

by:Huseyin KAHRAMAN
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 55

Accepted Solution

by:
Huseyin KAHRAMAN 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 55

Expert Comment

by:Huseyin KAHRAMAN
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 55

Expert Comment

by:Huseyin KAHRAMAN
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 55

Expert Comment

by:Huseyin KAHRAMAN
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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Hi all! Recently there was EE question and the person wanted to have a multi-column textbox <div> selection, so as a first step to answer I provided a link but that was not complete with JavaScript selection, but had a good style sheet. So as a ques…
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

707 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