Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-26-2012, 12:07 PM   PM User | #1
resin
Regular Coder

 
Join Date: Nov 2011
Posts: 142
Thanks: 5
Thanked 0 Times in 0 Posts
resin is an unknown quantity at this point
buzzer help?

Hello, I have this script which creates a buzzing effect on divs (written by the mysterious Mr. X who has since disappeared). He left me a great script but I'm having one issue that I haven't been able to resolve. When you click the div that is supposed to buzz, the div immediately jumps to the upper left corner and then carries on with the buzzing effect up in the corner. Instead the div should be remaining in its original location, just moving slightly right and left for a few seconds.

Here is the test page- http://resin.webs.com/new3.html . Click the green or red div to see what I'm talking about. Anyone know what's causing this bad behavior, or how to fix it?
resin is offline   Reply With Quote
Old 06-26-2012, 12:22 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,102
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Whatever it is supposed to do, it does not do it in IE9. Something that creates a noise is likely to be seen as an annoyance by your visitors.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 06-26-2012, 12:27 PM   PM User | #3
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,261
Thanks: 10
Thanked 533 Times in 527 Posts
devnull69 will become famous soon enough
Yes, the steps array has absolute left and top settings that point to the top left corner of the screen. And the function buzzStep will set this exact position without taking the current position of the element into consideration.

You should store the current position of the element and then add/substract the values from the steps array to/from it in buzzStep().
devnull69 is offline   Reply With Quote
Old 06-26-2012, 01:22 PM   PM User | #4
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,261
Thanks: 10
Thanked 533 Times in 527 Posts
devnull69 will become famous soon enough
Something in the lines of (untested!)
Code:
function buzzStep( element, leftOffset, topOffset, startLeft, startTop ) {
   element.style.left = startLeft + leftOffset +'px';
   element.style.top = startTop + topOffset +'px';
}

...

// Loop through the defined step definitions
for( var i = 0, stepsQueued = 0, length = steps.length; i < length; i++ ) {
   // Self executing anonmyous function to implement closure, ensuring the correct values are being passed to buzzStep
   ( function( element, position, delay ) {
      // Queue the current step
      setTimeout( function(){ buzzStep( element, position.left, position.top, element.offsetLeft, element.offsetTop ); }, delay );
      stepsQueued++;
      // Return to original position after each step
      setTimeout( function(){ buzzStep( element, 0, 0, element.offsetLeft, element.offsetTop ); }, delay + interval );
      stepsQueued++;
   })( element, steps[ i ], ( stepsQueued * interval ) );
}
devnull69 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 04:31 PM.


Advertisement
Log in to turn off these ads.