Solved

DHTML moving Image question

Posted on 2010-08-21
10
632 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
 
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

DOM Attributes and Properties treatment with jQuery 1.6 by Ivo Stoykov jQuery (http://jquery.com/) 1.6 introduces .prop() (http://api.jquery.com/prop/) and .removeProp() (http://api.jquery.com/removeProp/) methods which allow modifying or removi…
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now