使用jQuery在波浪路径上移动文本,并在悬停时停止。

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

Moving text on wave path with jQuery and stop on hover

问题

<svg width="100%" height="160px" viewBox="0 0 1098.72 89.55">
    <path id="curve" fill="transparent" d="M0.17,0.23c0,0,105.85,77.7,276.46,73.2s243.8-61.37,408.77-54.05c172.09,7.64,213.4,92.34,413.28,64.19"></path>
    <text width="100%">
        <textPath xlink:href="#curve">*这些图片并不是技术上的自拍照。</textPath>
    </text>
</svg>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
    $(document).ready(function() {
        const textPath = document.querySelector("textPath");
        const textPathLength = textPath.getComputedTextLength();
        const duration = 10000; // 根据需要调整持续时间

        $(textPath).css("startOffset", textPathLength);

        function animateText() {
            $(textPath).animate(
                { "startOffset": -textPathLength },
                {
                    duration: duration,
                    easing: "linear",
                    complete: function() {
                        animateText();
                    }
                }
            );
        }

        animateText();
    });
</script>
英文:
&lt;svg width=&quot;100%&quot; height=&quot;160px&quot; viewBox=&quot;0 0 1098.72 89.55&quot;&gt;
    &lt;path id=&quot;curve&quot; fill=&quot;transparent&quot; d=&quot;M0.17,0.23c0,0,105.85,77.7,276.46,73.2s243.8-61.37,408.77-54.05c172.09,7.64,213.4,92.34,413.28,64.19&quot;&gt;&lt;/path&gt;
    &lt;text width=&quot;100%&quot;&gt;
        &lt;textPath xlink:href=&quot;#curve&quot;&gt;*The pictures are not technically selfies.&lt;/textPath&gt;
    &lt;/text&gt;
&lt;/svg&gt;

&lt;script src=&quot;https://code.jquery.com/jquery-3.6.0.min.js&quot;&gt;&lt;/script&gt;
&lt;script&gt;
    $(document).ready(function() {
        const textPath = document.querySelector(&quot;textPath&quot;);
        const textPathLength = textPath.getComputedTextLength();
        const duration = 10000; // Adjust the duration as needed

        $(textPath).css(&quot;startOffset&quot;, textPathLength);

        function animateText() {
            $(textPath).animate(
                { &quot;startOffset&quot;: -textPathLength },
                {
                    duration: duration,
                    easing: &quot;linear&quot;,
                    complete: function() {
                        animateText();
                    }
                }
            );
        }

        animateText();
    });
&lt;/script&gt;

I'm trying to move text on wave path but its not working. I want it to move on the path so I add it to section on the website. If I try with marquee tag its move but in straight line. I also try many marquee options but the result is not same as I want.

答案1

得分: 0

以下是使用UNPKG splitting脚本可以做的事情。您需要将所需路径放入CSS变量--path中。调整动画将会给您不同的结果。

Splitting({
  whitespace: true
})
html {
  font-family: Arial, Helvetica, sans-serif;
}

:root {
  --path: "M0.17,0.23c0,0,105.85,77.7,276.46,73.2s243.8-61.37,408.77-54.05c172.09,7.64,213.4,92.34,413.28,64.19";
}

.myText {
  height: 100px;
  font-family: Arial, Helvetica, sans-serif;
  text-transform: uppercase;
  color: red;
}

.char {
  position: absolute;
  offset-path: path(var(--path));
  offset-distance: calc(var(--char-index) * 1.5rem);
  animation: loop 4s cubic-bezier(.62, .01, .42, 1.01) infinite alternate calc(var(--char-index) * 10ms);
}

@keyframes loop {
  50% {
    offset-distance: calc((var(--char-index) * 1rem) + 40%);
  }
  100% {
    offset-distance: calc((var(--char-index) * 1rem));
  }
}
<script src="https://unpkg.com/splitting@1.0.6/dist/splitting.min.js"></script>

<p>
  Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type
</p>

<p class="myText" data-splitting>Your text here</p>

<p>
  Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type
</p>
<p>
  Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type
</p>
<p>
  Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type
</p>
英文:

Here is something you could do using UNPKG splitting script. You need to put the needed path in the CSS variable --path. Tweaking the animation will give you different results.

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

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

Splitting({
  whitespace: true
})

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

html {
  font-family: Arial, Helvetica, sans-serif;
}

:root {
  --path: &quot;M0.17,0.23c0,0,105.85,77.7,276.46,73.2s243.8-61.37,408.77-54.05c172.09,7.64,213.4,92.34,413.28,64.19&quot;;
}

.myText {
  height: 100px;
  font-family: Arial, Helvetica, sans-serif;
  text-transform: uppercase;
  color: red;
}

.char {
  position: absolute;
  offset-path: path(var(--path));
  offset-distance: calc(var(--char-index) * 1.5rem);
  animation: loop 4s cubic-bezier(.62, .01, .42, 1.01) infinite alternate calc(var(--char-index) * 10ms);
}

@keyframes loop {
  50% {
    offset-distance: calc((var(--char-index) * 1rem) + 40%);
  }
  100% {
    offset-distance: calc((var(--char-index) * 1rem));
  }
}

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

&lt;script src=&quot;https://unpkg.com/splitting@1.0.6/dist/splitting.min.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;
  Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&#39;s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type
&lt;/p&gt;

&lt;p class=&quot;myText&quot; data-splitting&gt;Your text here&lt;/h1&gt;

&lt;p&gt;
  Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&#39;s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type
&lt;/p&gt;
&lt;p&gt;
  Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&#39;s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type
&lt;/p&gt;
&lt;p&gt;
  Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&#39;s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type
&lt;/p&gt;

<!-- end snippet -->

And even if that is not what you want, you could modify it or use it as a base or inspiration for your actual case of use

答案2

得分: 0

Jquery的[`animate()`][1]方法只能动画化**CSS属性** - SVG的**`startOffset`属性无法通过CSS更改**。

## 替代方法:通过[SMIL动画][2]来动画化您的文本

```javascript
let svg = document.querySelector('svg')

// 暂停和恢复
svg.addEventListener('mouseover', e => {
  svg.pauseAnimations();
})

svg.addEventListener('mouseleave', e => {
  svg.unpauseAnimations();
})
<svg width="100%" height="160px" viewBox="0 0 1098.72 89.55">
    <path id="curve" fill="transparent" d="M0.17,0.23c0,0,105.85,77.7,276.46,73.2s243.8-61.37,408.77-54.05c172.09,7.64,213.4,92.34,413.28,64.19"></path>
    <text width="100%">
        <textPath id="textPath" href="#curve" startOffset="100%">*这些图片在技术上不是自拍。
           <animate attributeName="startOffset" from="100%" to="-50%" begin="0s" dur="5s" repeatCount="indefinite" />
      </textPath>
    </text>
</svg>
现在您只需要在您的textpath中添加一个`<animate>`元素,并指定参数`duration`,初始值和最终值,如下所示

<textPath id="textPath" href="#curve" startOffset="100%">*这些图片在技术上不是自拍。
<animate 
    attributeName="startOffset" 
    from="100%" 
    to="-50%" 
    begin="0s" 
    dur="5s" 
    repeatCount="indefinite" />
</textPath>

现在我们可以通过调用pauseAnimations()轻松暂停所有动画

另请参阅"如何暂停并运行svg中的一个SMIL动画?"


<details>
<summary>英文:</summary>

Jquery&#39;s [`animate()`][1] method can only animate **CSS properties** – SVG&#39;s **`startOffset` attribute can&#39;t be changed via CSS**.  

## Alternative: animate your text via [SMIL animation][2]


&lt;!-- begin snippet: js hide: false console: true babel: false --&gt;

&lt;!-- language: lang-js --&gt;

    let svg = document.querySelector(&#39;svg&#39;)

    // pause and resume
    svg.addEventListener(&#39;mouseover&#39;, e =&gt; {
      svg.pauseAnimations();
    })

    svg.addEventListener(&#39;mouseleave&#39;, e =&gt; {
      svg.unpauseAnimations();
    })

&lt;!-- language: lang-html --&gt;

    &lt;svg width=&quot;100%&quot; height=&quot;160px&quot; viewBox=&quot;0 0 1098.72 89.55&quot;&gt;
        &lt;path id=&quot;curve&quot; fill=&quot;transparent&quot; d=&quot;M0.17,0.23c0,0,105.85,77.7,276.46,73.2s243.8-61.37,408.77-54.05c172.09,7.64,213.4,92.34,413.28,64.19&quot;&gt;&lt;/path&gt;
        &lt;text width=&quot;100%&quot;&gt;
            &lt;textPath id=&quot;textPath&quot; href=&quot;#curve&quot; startOffset=&quot;100%&quot;&gt;*The pictures are not technically selfies.
               &lt;animate attributeName=&quot;startOffset&quot; from=&quot;100%&quot; to=&quot;-50%&quot; begin=&quot;0s&quot; dur=&quot;5s&quot; repeatCount=&quot;indefinite&quot; /&gt;
          &lt;/textPath&gt;
        &lt;/text&gt;
    &lt;/svg&gt;

&lt;!-- end snippet --&gt;

You just need to add an `&lt;animate&gt;` elements to your textpath and specify parameters duration, initial and final values like so

    &lt;textPath id=&quot;textPath&quot; href=&quot;#curve&quot; startOffset=&quot;100%&quot;&gt;*The pictures are not technically selfies.
    &lt;animate 
        attributeName=&quot;startOffset&quot; 
        from=&quot;100%&quot; 
        to=&quot;-50%&quot; 
        begin=&quot;0s&quot; 
        dur=&quot;5s&quot; 
        repeatCount=&quot;indefinite&quot; /&gt;
    &lt;/textPath&gt;

Now we can easily pause all animations by calling `pauseAnimations()` 

See also [&quot;How to pause and run just one SMIL animation in svg?&quot;][3] 


  [1]: https://api.jquery.com/animate/
  [2]: https://developer.mozilla.org/en-US/docs/Web/SVG/Element/animate
  [3]: https://stackoverflow.com/questions/72498272/how-to-pause-and-run-just-one-smil-animation-in-svg

</details>



huangapple
  • 本文由 发表于 2023年7月3日 20:06:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76604583.html
匿名

发表评论

匿名网友

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

确定