简体   繁体   English

在激活一个单词时移动文本

[英]Move text while one word is being animated

I'm working on an animating text element. 我正在制作动画文本元素。

The element changes one work with animation. 元素更改了一部动画作品。 Much like in this example. 很像这个例子。 http://www.thepetedesign.com/demos/jquery_super_simple_text_rotator_demo.html http://www.thepetedesign.com/demos/jquery_super_simple_text_rotator_demo.html

The thing is I don't like that when I change one word, the other words around it shift abruptly. 问题是,当我改变一个单词时,周围的其他单词突然移动,我不喜欢这样。 I'm trying to use an effect where the other words that move around shift in a nice way. 我正在尝试使用一种效果,使周围移动的其他单词以一种很好的方式移动。 Like in this example. 就像在这个例子中一样。

http://www.thepetedesign.com/demos/jquery_super_simple_text_rotator_demo.html http://www.thepetedesign.com/demos/jquery_super_simple_text_rotator_demo.html

Does anyone know how to do this? 有谁知道如何做到这一点? or link me to a relevant solution? 或将我链接到相关解决方案?

I'm using HTML, CSS, and JS 我正在使用HTML,CSS和JS

There may be other ways of doing it but the most consistent cross browser way of doing it that I can think of is this. 可能还有其他方法可以做到,但是我能想到的最一致的跨浏览器方法就是这样。 This is some guidance but code samples would be helpful. 这是一些指导,但是代码示例会有所帮助。

You'll need to have the animated word wrapped in an extra span . 您需要将动画单词包裹在一个额外的span Then you'll want to define the width of the outer span to be the width of the inner span before animation. 然后,您需要将外部跨度的宽度定义为动画之前的内部跨度的宽度。 You can do this with CSS if you know what the width will be or you can do it dynamically like this: 如果知道宽度是多少,可以使用CSS进行操作,也可以像这样动态地进行操作:

$(outerSelector).width( $(innerSelector).width() + 'px');

Animate the text then animate the width change: 对文本进行动画处理,然后对宽度变化进行动画处理:

$(outerSelector).animate({width: $(innerSelector).width() +'px'}, 500);

This really only works for making it smaller. 这实际上只能使它变小。 You can use this method for making the word bigger but you need to know the final width of the word first. 您可以使用此方法使单词变大,但首先需要知道单词的最终宽度。 Then you could simply have one span and do this: 然后,您可以只跨一个范围并执行以下操作:

$(selector).animate({width:newWidth}, 500);

If you're not worried about cross browser compatibility or if you can assume that everyone using the site will have a CSS 3 enabled browser then you can do it with CSS: 如果您不担心跨浏览器的兼容性,或者可以假设使用该网站的每个人都将启用CSS 3,那么可以使用CSS来做到这一点:

selector {
    transition: width 0.5s;
}

Then you for shrinking text you would need to again set the outer span width like above, but after the animation of the text you could simply do this: 然后,为缩小文本,您将需要像上面一样再次设置外部跨度宽度,但是在对文本进行动画处理之后,您可以简单地执行以下操作:

$(outerSelector).width( $(innerSelector).width() + 'px');

This has the same problem with needing to know that width in advance for transitioning to longer text. 这具有相同的问题,即需要提前知道该宽度以过渡到较长的文本。 You could find out the width using another span with identical text. 您可以使用另一个跨度相同的文本找出宽度。

.copy {
    position: absolute;
    left: -100%;
}

Then you could get the width of the copied version and use that as the new width for animation. 然后,您可以获取复制版本的宽度,并将其用作动画的新宽度。

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

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