Is it possible to transition text-alignment using css3? For example, I'd like to animate text-alignment from left to right, however, adding a transition property on text-align doesn't do the trick.
相关问题
- Adding a timeout to a render function in ReactJS
-
Why does the box-shadow property not apply to a
- Add animation to jQuery function Interval
- jQuery hover to slide?
- Issue with star rating css
This is an old question but I recently had the same issue and found the answers here unsatisfying for my use case.
The biggest concern I have with the solution from dc5 is that it uses the slow position properties for animation which is bad for transitions as they require re-layouting and are not hardware-accelerated.
My solution is solely based on
transform: transitionX()
and leverages the fact thattranslate()
is always based on the target element's width/height:This is my solution:
The way I do it is to simply use text-indent instead of text-align. You have to do trial/error a little to figure out the number of px, rem, em, %, or whatever where you want to move the item via text-indent.
So, I can add the
tsetup
class to my element. When I click it, I add thetright
class. When I click it again, I remove thetright
class. It does a nice gradual fade from left to right.I use this technique with the FontAwesome
fa-circle
icon(content:"\f111";font-family:FontAwesome;)
to make a checkbox toggle slider button.You can align text to the right and increase the width of the container.
Here is an example: CodePen
HTML:
CSS:
If I understand what you are going for, one way to do this is to change your layout a bit and animate left/right properties:
Working CodePen
This takes advantage of overflow being visible by setting the
right
of thespan
element to the left hand side of the screen.On hover,
right
is set to0
, transitioning the element across the screen.I found out a way of not only animating from left to right / right to left, but also from centered text.
The trick is to apply a width of '0'.
https://jsfiddle.net/q5kLcfkx/4/