Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Animation Speed?

Final Edit: The wall of text below can be summed up by simply asking "can I specify the speed of animations using jQuery's animate()? All that is provided is duration."

~~

jQuery's animate() seems to implement easing despite my use of "linear". How can I get the two boxes to stay together until the first finishes @ 250px? The second animates much faster because it has a longer distance to go.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript">
    $(function()
    {
        $('#a').animate({left: '250px'}, 1000, 'linear');
        $('#b').animate({left: '500px'}, 1000, 'linear');
    });
</script>

<div id="a" style="background-color: red; position: relative; width: 50px; height: 50px;"></div>
<br/><br/>
<div id="b" style="background-color: red; position: relative;width: 50px; height: 50px;"></div>

Alternatively is there a jQuery carousel plugin that does this (mouse movement based on where you're mousing) so I don't have to rewrite it? I spent about 20 minutes looking for one on Google but couldn't come up with anything I liked.

ETA: The example I provided is very simple, but the issue as I found it is applied to a more complex code base. (1) Go here. (2) Put mouse on C. Viper, see the speed. (3) Put mouse on Ryu, but before it finishes, move your mouse to the middle of the DIV (so it stops). (4) Put your mouse back on the left side and see how utterly slow it moves.

Calculating the differences in speed and distance seems insurmountable here. All I'm trying to do is recreate a lovely effect I saw a site use today (this site). But it's Mootools, and I'm in jQuery. =[

like image 956
Langdon Avatar asked Jul 07 '10 01:07

Langdon


People also ask

What is jQuery animate function?

Definition and Usage. The animate() method performs a custom animation of a set of CSS properties. This method changes an element from one state to another with CSS styles. The CSS property value is changed gradually, to create an animated effect. Only numeric values can be animated (like "margin:30px").

Is CSS animation faster than JavaScript?

CSS has fairly good performance as it offloads animation logic onto the browser itself. This lets the browser optimize DOM interaction and memory consumption and most importantly, uses the GPU to improve performance. On the other hand, Javascript performance can range from reasonably faster to much slower than CSS.

Is jQuery used for animation?

With jQuery, you can create custom animations.

What is the correct syntax of animate () method?

The animate() is an inbuilt method in jQuery which is used to change the state of the element with CSS style. This method can also be used to change the CSS property to create the animated effect for the selected element. Syntax: (selector).


3 Answers

For the updated question:
First, here's a working demo with the behavior you want. What we're doing here is adjusting the speed based on the amount needed to move, because speed isn't an accurate description, it's the duration, moving a shorter distance over the same duration means a slower move, so we need to scale the duration with the distance we need to move. For moving backwards, it looks like this:

var oldLeft = ul.offset().left;
ul.animate({left: 0}, -oldLeft * speed, 'linear');

Since the <ul> scrolls with a negative left position, we need to move the inverse of that many pixels to get back to the beginning, so we're using -oldLeft (it's current left position).

For the forward direction, a very similar approach:

var newLeft = divWidth - ulWidth,
    oldLeft = ul.offset().left;
ul.animate({left: newLeft + 'px'}, (-newLeft + oldLeft) * speed, 'linear');

This gets the new left property, the end being the width of the <ul> minus the width of the <div> it's in. Then we subtract (it's negative so add) that from the current left position (also negative, so reverse it).

This approach gives your speed variable a whole new meaning, it now means "milliseconds per pixel" rather than the duration it did before, which seems to be what you're after. The other optimization is using that cached <ul> selector you already had, making things a bit faster/cleaner overall.


For the original question:
Keep it simple, just half the time for half the distance, like this:

$(function() {
    $('#a').animate({left: '250px'}, 500, 'linear');
    $('#b').animate({left: '500px'}, 1000, 'linear');
});

You can try a demo here

like image 128
Nick Craver Avatar answered Nov 18 '22 18:11

Nick Craver


I made a plugin that does exactly what you want. You can use Supremation to specify the speed of the animation as opposed to the duration.

like image 38
lukeshumard Avatar answered Nov 18 '22 19:11

lukeshumard


linear only specifies that the animation should be done in linear increments and not speed up or slow down as it finishes. If you want the two animations to be the same speed, just double the time it takes for the animation that is twice the distance:

$('#a').animate({left: '250px'}, 1000, 'linear');
$('#b').animate({left: '500px'}, 2000, 'linear');
like image 43
Mike Sherov Avatar answered Nov 18 '22 18:11

Mike Sherov