Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animate of pseudo elements using jQuery

I'm using a:after, it contains right: Ypx.

I want to use animate method in jQuery to move the element (a:after) from Ypx to Zpx, how can I do it?

For example:

[link-text][after]
[link-text]-- moving > --[after]

I know how to do it without animation, just doing a:hover:after { right: Zpx } but how with animation?

like image 665
Luis Avatar asked Sep 12 '12 17:09

Luis


People also ask

Can you animate pseudo elements?

As you have seen, the ::before and ::after pseudo-elements can be used in several different ways to produce interesting animated effects that give life to our designs.

How do you animate in jQuery?

The jQuery animate() method is used to create custom animations. Syntax: $(selector).animate({params},speed,callback);

Which method is available in jQuery for animation?

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.

How use jQuery animate flash?

Pure jQuery solution. var flash = function(elements) { var opacity = 100; var color = “255, 255, 20” // has to be in this format since we use rgba var interval = setInterval(function() { opacity -= 3; if (opacity How can use jQuery to animate a flash to the button? var randomNumber = Math. floor(Math.


2 Answers

What you are trying to do is extremely hacky and I'd strongly recommend you to put make real elements instead of trying to animate pseudo-elements. However, for future reference: the only way of animating a pseudo-element is by inserting a style tag and changing the value in actual CSS, such as...

var styles = document.getElementById("pseudo-mover")
var distance = 100;

var move = function(){
    styles.innerHTML = ".box:after {left: " + distance + "px}";
    distance++;
    if (distance < 200) 
        setTimeout(move, 30);    
}

move()

http://jsfiddle.net/X7aTf/

This is vanilla js, but you could use the step callback in jquery's animate to hook in to jquery's easing and timing functions.

like image 82
methodofaction Avatar answered Sep 24 '22 06:09

methodofaction


You can use CSS3 transitions to accomplish this, but sadly they are only supported (as far as I know) in FireFox 4+ right now.

http://jsfiddle.net/XT6qJ/1/

#foo:after{
    content:'!';
    display:inline-block;
    border:solid 1px #f00;
    padding:3px;
    width:25px;
    text-align:center;
    -webkit-transition:margin-left 1s;
    -moz-transition:margin-left 1s;    
    -ms-transition:margin-left 1s;
    -o-transition:margin-left 1s;    
    transition:margin-left 1s;    
}
#foo:hover:after{
    margin-left:100px;    
}
like image 33
Shmiddty Avatar answered Sep 22 '22 06:09

Shmiddty