简体   繁体   English

使用jQuery对伪元素进行动画处理

[英]Animate of pseudo elements using jQuery

I'm using a:after , it contains right: Ypx . 我正在使用a:after ,它包含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? 我想在jQuery中使用animate方法将元素( a:after )从Ypx移动到Zpx,我该怎么办呢?

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? 我知道怎么做没有动画,只做a:hover:after { right: Zpx }但是动画怎么样?

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. 你要做的是非常hacky,我强烈建议你把make元素放在一起,而不是尝试动画伪元素。 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... 但是,为了将来参考:动画伪元素的唯一方法是插入style标记并更改实际CSS中的值,例如...

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/ 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. 这是vanilla js,但你可以在jquery的animate使用step回调来挂钩到jquery的缓动和计时功能。

You can use CSS3 transitions to accomplish this, but sadly they are only supported (as far as I know) in FireFox 4+ right now. 您可以使用CSS3过渡来完成此任务,但遗憾的是,它们目前仅在FireFox 4+中受支持(据我所知)。

http://jsfiddle.net/XT6qJ/1/ 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;    
}

Use next() instead. 请改用next()。 Here's how you can achieve exactly that. 以下是您如何实现这一目标的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM