How to animate overflowing text to scroll to the end of the text when hovering and animate back to original position when mouse leaves?

huangapple go评论119阅读模式
英文:

How to animate overflowing text to scroll to the end of the text when hovering and animate back to original position when mouse leaves?

问题

我正在尝试在悬停在具有溢出文本的容器上时实现滚动文本的动画效果。目前,当鼠标悬停在容器上时,动画会滚动到文本的末尾,但当鼠标离开时,文本突然返回到原始位置。我希望在鼠标离开时也能实现回到原始位置的动画效果。

  1. import { useEffect, useRef, useState } from 'react';
  2. import classes from './test.module.css';
  3. const Test = () => {
  4. const textRef = useRef(null);
  5. const containerRef = useRef(null);
  6. const [isHovered, setIsHovered] = useState(false);
  7. const [isOverflowing, setIsOverflowing] = useState(false);
  8. const onMouseEnter = () => {
  9. setIsHovered(true);
  10. };
  11. const onMouseLeave = () => {
  12. setIsHovered(false);
  13. };
  14. useEffect(() => {
  15. const textWidth = textRef.current.offsetWidth;
  16. const containerWidth = containerRef.current.offsetWidth;
  17. if (textWidth > containerWidth) {
  18. setIsOverflowing(true);
  19. }
  20. }, []);
  21. return (
  22. <div>
  23. <div
  24. ref={containerRef}
  25. className={classes.animated}
  26. onMouseEnter={onMouseEnter}
  27. onMouseLeave={onMouseLeave}
  28. >
  29. <h3
  30. className={isHovered && isOverflowing ? classes.textAnimated : classes.text}
  31. ref={textRef}
  32. >
  33. This is some very very very long text
  34. </h3>
  35. </div>
  36. </div>
  37. );
  38. };
  39. export default Test;
  1. .animated {
  2. margin: 200px;
  3. position: relative;
  4. white-space: nowrap;
  5. max-width: 300px;
  6. width: 100%;
  7. overflow: hidden;
  8. background: #0c0c0c;
  9. display: inline-block;
  10. position: relative;
  11. border: 1px solid white;
  12. }
  13. .textAnimated {
  14. color: #fff;
  15. animation: textScroll 3s linear forwards;
  16. display: inline-block;
  17. position: relative;
  18. }
  19. .text {
  20. color: #fff;
  21. display: inline-block;
  22. position: relative;
  23. }
  24. @keyframes textScroll {
  25. 0% {
  26. transform: translateX(0);
  27. left: 0;
  28. }
  29. 10% {
  30. transform: translateX(0);
  31. left: 0;
  32. }
  33. 90% {
  34. transform: translateX(calc(-100%));
  35. left: 100%;
  36. }
  37. 100% {
  38. transform: translateX(calc(-100%));
  39. left: 100%;
  40. }
  41. }
英文:

I am trying to animate scrolling text when hovering over a container that has overflowing text. Right now I have the animation scrolling to the end of the text when hovering over the container, but when the mouse leaves, the text just suddenly goes back to original position. I want it to also animate back to original position when the mouse leaves.

  1. import {useEffect, useRef, useState} from &#39;react&#39;;
  2. import classes from &#39;./test.module.css&#39;;
  3. const Test = () =&gt; {
  4. const textRef = useRef(null);
  5. const containerRef = useRef(null);
  6. const [isHovered, setIsHovered] = useState(false);
  7. const [isOverflowing, setIsOverflowing] = useState(false);
  8. const onMouseEnter = () =&gt; {
  9. setIsHovered(true);
  10. };
  11. const onMouseLeave = () =&gt; {
  12. setIsHovered(false);
  13. };
  14. useEffect(() =&gt; {
  15. const textWidth = textRef.current.offsetWidth;
  16. const containerWidth = containerRef.current.offsetWidth;
  17. if (textWidth &gt; containerWidth) {
  18. setIsOverflowing(true);
  19. }
  20. }, []);
  21. return (
  22. &lt;div&gt;
  23. &lt;div ref={containerRef} className={classes.animated} onMouseEnter={onMouseEnter} onMouseLeave={onMouseLeave}&gt;
  24. &lt;h3 className={isHovered &amp;&amp; isOverflowing ? classes.textAnimated : classes.text} ref={textRef}&gt;
  25. This is some very very very long text
  26. &lt;/h3&gt;
  27. &lt;/div&gt;
  28. &lt;/div&gt;
  29. );
  30. };
  31. export default Test;
  1. .animated {
  2. margin: 200px;
  3. position: relative;
  4. white-space: nowrap;
  5. max-width: 300px;
  6. width: 100%;
  7. overflow: hidden;
  8. background: #0c0c0c;
  9. display: inline-block;
  10. position: relative;
  11. border: 1px solid white;
  12. }
  13. .textAnimated {
  14. color: #fff;
  15. animation: textScroll 3s linear forwards;
  16. display: inline-block;
  17. position: relative;
  18. }
  19. .text {
  20. color: #fff;
  21. display: inline-block;
  22. position: relative;
  23. }
  24. @keyframes textScroll {
  25. 0% {
  26. transform: translateX(0);
  27. left: 0;
  28. }
  29. 10% {
  30. transform: translateX(0);
  31. left: 0;
  32. }
  33. 90% {
  34. transform: translateX(calc(-100%));
  35. left: 100%;
  36. }
  37. 100% {
  38. transform: translateX(calc(-100%));
  39. left: 100%;
  40. }
  41. }

答案1

得分: 0

请考虑使用 transition 而不是 animation。在悬停时,transition 对于在两个状态之间进行视觉插值非常有用,正如此处的情况一样:

  1. <!-- begin snippet: js hide: false console: false babel: true -->
  2. <!-- language: lang-js -->
  3. const { useEffect, useRef, useState } = React;
  4. const Test = () => {
  5. const textRef = useRef(null);
  6. const containerRef = useRef(null);
  7. const [isOverflowing, setIsOverflowing] = useState(false);
  8. useEffect(() => {
  9. const textWidth = textRef.current.offsetWidth;
  10. const containerWidth = containerRef.current.offsetWidth;
  11. if (textWidth > containerWidth) {
  12. setIsOverflowing(true);
  13. }
  14. }, []);
  15. return (
  16. <div>
  17. <div ref={containerRef} className="animated">
  18. <h3 className={isOverflowing ? 'textAnimated' : 'text'} ref={textRef}>
  19. This is some very very very long text
  20. </h3>
  21. </div>
  22. </div>
  23. );
  24. };
  25. ReactDOM.createRoot(document.getElementById('app')).render(<Test/>);
  26. <!-- language: lang-css -->
  27. .animated {
  28. margin: 200px;
  29. position: relative;
  30. white-space: nowrap;
  31. max-width: 100px;
  32. width: 100%;
  33. overflow: hidden;
  34. background: #0c0c0c;
  35. display: inline-block;
  36. position: relative;
  37. border: 1px solid white;
  38. }
  39. .textAnimated {
  40. color: #fff;
  41. display: inline-block;
  42. position: relative;
  43. left: 0;
  44. transition: transform 3s linear, left 3s linear;
  45. }
  46. .animated:hover .textAnimated {
  47. transform: translateX(-100%);
  48. left: 100%;
  49. }
  50. .text {
  51. color: #fff;
  52. display: inline-block;
  53. position: relative;
  54. }
  55. <!-- language: lang-html -->
  56. <script src="https://cdnjs.cloudflare.com/ajax/libs/react/18.2.0/umd/react.production.min.js" integrity="sha512-8Q6Y9XnTbOE+JNvjBQwJ2H8S+UV4uA6hiRykhdtIyDYZ2TprdNmWOUaKdGzOhyr4dCyk287OejbPvwl7lrfqrQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
  57. <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.2.0/umd/react-dom.production.min.js" integrity="sha512-MOCpqoRoisCTwJ8vQQiciZv0qcpROCidek3GTFS6KTk2+y7munJIlKCVkFCYY+p3ErYFXCjmFjnfTTRSC1OHWQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
  58. <div id="app"></div>
  59. <!-- end snippet -->

注意:以上代码片段中包含了HTML、JavaScript和CSS部分。如果需要进一步的翻译,请提供具体的文本或指令。

英文:

Consider using transition rather than animation. transition is useful for visual interpolation between two states, as would be the case here on hover:

<!-- begin snippet: js hide: false console: false babel: true -->

<!-- language: lang-js -->

  1. const {useEffect, useRef, useState} = React;
  2. const Test = () =&gt; {
  3. const textRef = useRef(null);
  4. const containerRef = useRef(null);
  5. const [isOverflowing, setIsOverflowing] = useState(false);
  6. useEffect(() =&gt; {
  7. const textWidth = textRef.current.offsetWidth;
  8. const containerWidth = containerRef.current.offsetWidth;
  9. if (textWidth &gt; containerWidth) {
  10. setIsOverflowing(true);
  11. }
  12. }, []);
  13. return (
  14. &lt;div&gt;
  15. &lt;div ref={containerRef} className=&quot;animated&quot;&gt;
  16. &lt;h3 className={isOverflowing ? &#39;textAnimated&#39; : &#39;text&#39;} ref={textRef}&gt;
  17. This is some very very very long text
  18. &lt;/h3&gt;
  19. &lt;/div&gt;
  20. &lt;/div&gt;
  21. );
  22. };
  23. ReactDOM.createRoot(document.getElementById(&#39;app&#39;)).render(&lt;Test/&gt;);

<!-- language: lang-css -->

  1. .animated {
  2. margin: 200px;
  3. position: relative;
  4. white-space: nowrap;
  5. max-width: 100px;
  6. width: 100%;
  7. overflow: hidden;
  8. background: #0c0c0c;
  9. display: inline-block;
  10. position: relative;
  11. border: 1px solid white;
  12. }
  13. .textAnimated {
  14. color: #fff;
  15. display: inline-block;
  16. position: relative;
  17. left: 0;
  18. transition: transform 3s linear, left 3s linear;
  19. }
  20. .animated:hover .textAnimated {
  21. transform: translateX(-100%);
  22. left: 100%;
  23. }
  24. .text {
  25. color: #fff;
  26. display: inline-block;
  27. position: relative;
  28. }

<!-- language: lang-html -->

  1. &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/react/18.2.0/umd/react.production.min.js&quot; integrity=&quot;sha512-8Q6Y9XnTbOE+JNvjBQwJ2H8S+UV4uA6hiRykhdtIyDYZ2TprdNmWOUaKdGzOhyr4dCyk287OejbPvwl7lrfqrQ==&quot; crossorigin=&quot;anonymous&quot; referrerpolicy=&quot;no-referrer&quot;&gt;&lt;/script&gt;
  2. &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.2.0/umd/react-dom.production.min.js&quot; integrity=&quot;sha512-MOCpqoRoisCTwJ8vQQiciZv0qcpROCidek3GTFS6KTk2+y7munJIlKCVkFCYY+p3ErYFXCjmFjnfTTRSC1OHWQ==&quot; crossorigin=&quot;anonymous&quot; referrerpolicy=&quot;no-referrer&quot;&gt;&lt;/script&gt;
  3. &lt;div id=&quot;app&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月29日 08:05:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76577371.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定