Simple shake effect problem

This is a discussion on "Simple shake effect problem" within the JavaScript Forum section. This forum, and the thread "Simple shake effect problem are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Sep 24th, 2007, 13:40
marSoul's Avatar
Moderator
Join Date: Sep 2007
Location: Tehran - Iran
Age: 28
Posts: 398
Blog Entries: 2
Thanks: 0
Thanked 4 Times in 4 Posts
Send a message via MSN to marSoul Send a message via Yahoo to marSoul
Question Simple shake effect problem

Hi
I'm a newbie in javascript,
I created a simple shake effect with javascript, but the problem is that this effect only one time works, how can i make it work every click on the text ?
HTML: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
var i=50;
function goleft()
    {
    document.getElementById('title').style.left = 100+i+"px";
    setTimeout("goright()",100);
    i=i/2;
    }
function goright()
    {
    document.getElementById('title').style.left = 100-i+"px";
    setTimeout("goleft()",100);
    i=i/2;
    }
</script>
<style>
#title {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 36px;
    color: #FF0000;
    position: relative;
    top: 0px;
    left: 100px;    
}
</style>
<body>
<div id="title" onclick="goright()">Shake Me !</div>
</body>
</html>
Last Blog Entry: Throughout IRAN (Dec 10th, 2007)
Reply With Quote

  #2 (permalink)  
Old Sep 24th, 2007, 22:52
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Simple shake effect problem

The reason this only works once is becuase var 'i' eventually becomes less than one and the recursive function calls never stop.

You can fix this by having a function that will reset 'i' if is less than 1 and clear the timeouts.

This essentially starts it all again.


Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
var i=50;
var timeout = false; // store the timeout reference in a variable

function init()
{
    if (i > 1 && timeout) // if i is less than 1 and we have started already just exit
      return;
    i = 50; // reset i
    clearTimeout(timeout); // clear the timeout
    goright(); // start the wobbly bits
}

function goleft()
    {
    document.getElementById('title').style.left = 100+i+"px";
    timeout = setTimeout("goright()",100);
    i=i/2;
    }
function goright()
    {
    document.getElementById('title').style.left = 100-i+"px";
    timeout = setTimeout("goleft()",100);
    i=i/2;
    }
</script>
<style>
#title {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 36px;
    color: #FF0000;
    position: relative;
    top: 0px;
    left: 100px;    
}
</style>
<body>
<div id="title" onclick="init()">Shake Me !</div>
</body>
</html>
Every time the text stops, you can click it again now to make it shake.

Cheers,
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Reply With Quote
  #3 (permalink)  
Old Sep 24th, 2007, 23:13
marSoul's Avatar
Moderator
Join Date: Sep 2007
Location: Tehran - Iran
Age: 28
Posts: 398
Blog Entries: 2
Thanks: 0
Thanked 4 Times in 4 Posts
Send a message via MSN to marSoul Send a message via Yahoo to marSoul
Re: Simple shake effect problem

Thanks !
Last Blog Entry: Throughout IRAN (Dec 10th, 2007)
Reply With Quote
  #4 (permalink)  
Old Sep 25th, 2007, 19:15
alexgeek's Avatar
Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,771
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
Re: Simple shake effect problem

god i hate these
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Reply With Quote
  #5 (permalink)  
Old Sep 25th, 2007, 19:19
marSoul's Avatar
Moderator
Join Date: Sep 2007
Location: Tehran - Iran
Age: 28
Posts: 398
Blog Entries: 2
Thanks: 0
Thanked 4 Times in 4 Posts
Send a message via MSN to marSoul Send a message via Yahoo to marSoul
Re: Simple shake effect problem

Hate What ?!
Last Blog Entry: Throughout IRAN (Dec 10th, 2007)
Reply With Quote
  #6 (permalink)  
Old Sep 25th, 2007, 19:22
alexgeek's Avatar
Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,771
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
Re: Simple shake effect problem

Shake effects, they do my nut in!
May be alright for what you're doing though,
so i'll shup
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Reply With Quote
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] simple div problem hhelen Web Page Design 5 Apr 4th, 2008 14:56
CSS Ordered List (Rollover Effect) Problem in I.E 6 Aaron1988 Web Page Design 0 Mar 10th, 2008 03:56
Simple Problem Please Help! bthomson82 JavaScript Forum 3 Jun 22nd, 2007 02:59
Really simple positioning problem :( camcool21 Web Page Design 4 Dec 30th, 2006 18:25


All times are GMT. The time now is 10:13.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43