0


前端实现逐字展示效果

AI🔥了,发现了一个有点意思的东西,就是逐字展示,以下就是它的实现方式

vue3+ts实现

一、

  1. setTimeout
  1. <template>
  2. <div>
  3. <button @click="startDisplayStr">逐字展示</button><br />
  4. <p>{{ displayedText }}</p>
  5. </div>
  6. </template>
  7. <script setup lang="ts">
  8. import { ref } from 'vue'
  9. const streamList = ref(['首先需要避免暴饮暴食,尽量保持规律的饮食习惯,避免过度饥饿或过饱。', '其次,应避免摄入过多油腻食物和高脂肪食物,以免加重胰腺负担。', '此外,还应注意控制饮酒量,避免酒精对胰腺的损害。'])
  10. const currIndex = ref(0)
  11. const displayedText = ref('') // 正在显示的文字
  12. let timer: number | null = null
  13. const startDisplayStr = () => {
  14. if (timer) {
  15. clearTimeout(timer!)
  16. }
  17. // 将数组中的字符串拼接起来
  18. const res = streamList.value.join('')
  19. if (currIndex.value < res.length) {
  20. displayedText.value += res[currIndex.value]
  21. currIndex.value++
  22. setTimeout(startDisplayStr, 200)
  23. } else {
  24. clearTimeout(timer!)
  25. timer = null
  26. }
  27. }
  28. </script>
  29. <style scoped></style>

二、

  1. setInterval
  1. <template>
  2. <div>
  3. <button @click="getStream">逐字展示</button><br />
  4. <p>{{ displayedText }}</p>
  5. </div>
  6. </template>
  7. <script setup lang="ts">
  8. import { ref } from 'vue'
  9. const streamList = ref(['首先需要避免暴饮暴食,尽量保持规律的饮食习惯,避免过度饥饿或过饱。', '其次,应避免摄入过多油腻食物和高脂肪食物,以免加重胰腺负担。', '此外,还应注意控制饮酒量,避免酒精对胰腺的损害。'])
  10. const currIndex = ref(0)
  11. const displayedText = ref('') // 正在显示的文字
  12. let timer: number | null = null
  13. const getStream = () => {
  14. timer = setInterval(displayAnswer, 200)
  15. })
  16. }
  17. const displayAnswer = () => {
  18. const text = streamList.value.join('')
  19. displayedText.value += text[currIndex.value]
  20. currIndex.value++
  21. if (currIndex.value == text.length) {
  22. clearInterval(timer!)
  23. timer = null
  24. }
  25. }
  26. </script>
  27. <style scoped></style>

react+js

  1. setInterval
  1. import { useState, useRef, useEffect } from 'react';
  2. import { Button } from 'antd';
  3. const Xiaoan = () => {
  4. const [stream, setStream] = useState('')
  5. const [answerOutput, setAnswerOutput] = useState('') // 正在显示的文字
  6. const typingRef = useRef(null); // 使用ref来存储定时器ID
  7. const [isShowing, setIsShowing] = useState(false); // 是否开始展示
  8. const start = () => {
  9. setStream('这是一条测试语句')
  10. setIsShowing(true)
  11. }
  12. // 逐字展示答案
  13. const timeoutTextHandle = (callback) => {
  14. let currIndex = 0
  15. const chars = stream.split('')
  16. const timer = setInterval(() => {
  17. if (currIndex < chars.length) {
  18. setAnswerOutput(prev => prev + chars[currIndex])
  19. callback(chars[currIndex]) // 每增加一个字符调用回调,将当前的字符传给回调函数
  20. currIndex++
  21. } else {
  22. clearInterval(timer)
  23. setIsShowing(false)
  24. }
  25. }, 200)
  26. typingRef.current = timer
  27. }
  28. useEffect(() => {
  29. // 监听变量,触发函数
  30. if (isShowing && stream) {
  31. let fullChar = ''
  32. history.current.push({type: 'ai', message: ''})
  33. timeoutTextHandle(char => {
  34. fullChar += char
  35. let index = history.current.length - 1
  36. history.current[index]['message'] = fullChar
  37. })
  38. }
  39. return () => {
  40. // 组件卸载时,清除定时器
  41. if (typingRef.current) {
  42. clearInterval(typingRef.current);
  43. }
  44. };
  45. }, [isShowing, stream])
  46. return (
  47. <div>
  48. <Button onClick={start}>开始</Button>
  49. <div>展示:{answerOutput}</div>
  50. </div>
  51. )
  52. }
  53. export default Xiaoan

本文转载自: https://blog.csdn.net/weixin_59769277/article/details/137238643
版权归原作者 杨了仰头 所有, 如有侵权,请联系我们删除。

“前端实现逐字展示效果”的评论:

还没有评论