[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

DHTML moving Image question

Posted on 2010-08-21
10
Medium Priority
?
659 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 60

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 60

Accepted Solution

by:
HainKurt earned 2000 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 60

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 60

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 60

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
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…
Suggested Courses

656 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