r/jquery • • Jun 19 '14

Jquery animation without a click trigger.

I'm trying to create an animation that continuously moves a <div> across the page without requiring any click or hover. Is that possible?

Edit: Sleeping.

0 Upvotes

4 comments sorted by

2

u/nicholas-c Jun 19 '14

Knocked this up quickly;

http://codepen.io/anon/pen/nisJm

Whats going on? On page load you're loading the page and calling the function which moves it 200px to the right, which then runs the second part which moves it back to the left on the callback of this is runs the entire function again, restarting the animation.

edit: Also a .delay(1000). as a proof of delaying the time.

1

u/loodroomer Jun 19 '14

Save yourself the hassle and inefficiency of using setTimeout or setInterval and use requestAnimationFrame. It is more efficient and is the recommended way to use timers for animation. Perhaps TweenLite or some other library that makes it easy to work with would be nice.

0

u/[deleted] Jun 19 '14

[deleted]

-1

u/tllnbks Jun 19 '14

I'm not sure. I'm not to familiar with either javascript nor jquery. I can do a bit of research on that.

0

u/[deleted] Jun 19 '14

[deleted]

1

u/Thykka Jun 19 '14 edited Jun 19 '14

w3schools is generally not a good primary reference to pass on to beginners. Their descriptions and example code usually lack consistency and good programming habits.

For example, they present the following line as syntax reference:

window.setInterval("javascript function", milliseconds);

Which is kinda correct, except that wrapping a function within quotes will make it a string. It's a small thing, but if you're a beginner, you might find yourself trying to figure out why this doesn't work:

function myFunc () { console.log("hai"); }
window.setInterval("myFunc", 400);

You should be linking to MDN instead (or devdocs.io, because it combines the best documentations)

Other reputable learning material is available at CSS-Tricks and HTML5 Rocks.