首页 > 其他分享 >react 横向文字滚动动画 ,及纵向文字滚动动画带有间歇时间 交替渐变显示文字动画

react 横向文字滚动动画 ,及纵向文字滚动动画带有间歇时间 交替渐变显示文字动画

时间:2024-01-11 11:24:49浏览次数:20  
标签:文字 动画 滚动 text fatherHeight transform setStopInterval useState const

水平滚动

demo.less #scroll_x {   width: 300px;   height: 30px;   background-color: #ccc;   color: green;   position: relative;   overflow: hidden; } #scroll_x_text {   font-size: 20px;   position: absolute;   white-space: nowrap;   word-wrap: normal;   top: 0; }   demo.tsx import React, { useEffect, useState } from "react" import './demo.less' const Demo = () => {     const [left, setLeft] = useState(9999)     const [transform, setTransform] = useState(0)     const [transition, setTransition] = useState(`transform 5s linear`)     const [stopInterval, setStopInterval] = useState(false)     let scrollXStyle = {         left,         transform: `translateX(${transform}px)`,         transition     }     let timerX     const text = 'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeerrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrtttttttttttttttttttttttyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'     const setPosition = () => {         const textWidth = document.getElementById('scroll_x_text')?.clientWidth         const fatherWidth = document.getElementById('scroll_x')?.clientWidth         setStopInterval(true)         if (fatherWidth && textWidth) {             setLeft(fatherWidth)             if (transform < 0 && ((Math.abs(transform) - fatherWidth) > textWidth)) {                 setTransition('')                 setTransform(0)                 setTimeout(() => {                     setStopInterval(false)                 }, 100)
            } else {                 setTransition(`transform 5s linear`)                 setTransform(transform - fatherWidth)                 setTimeout(() => {                     setStopInterval(false)                 }, 5000)             }         }     }     useEffect(() => {         if (stopInterval) {             clearInterval('timerX')         } else {             timerX = setInterval(setPosition, 1000)         }         return () => {             clearInterval(timerX)         }     }, [stopInterval])     return <div id="scroll_x">         <div id="scroll_x_text" style={scrollXStyle}>             {text}         </div>     </div> } export default Demo     纵向滚动 demo1.less #scroll_y {   width: 300px;   height: 60px;   background-color: #ccc;   color: green;   position: relative;   top: 0px;   left: 300px;   overflow: hidden; } #scroll_y_text {   position: absolute;   word-wrap: break-word;   top: 50px;   left: 0;   font-size: 20px;   line-height: 30px;   color: green; }   demo.tsx import React, { useEffect, useState } from "react" import './demo1.less' const Demo = () => {     const [top, setTop] = useState(9999)     const [width, setWidth] = useState(9999)     const [transform, setTransform] = useState(0)     const [transition, setTransition] = useState('transform 5s linear')     const [stopInterval, setStopInterval] = useState(false)     let scrollYStyle = {         width,         top,         transform: `translateY(${transform}px)`,         transition     }     let timerY     const text = 'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeerrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrtttttttttttttttttttttttyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'     const setPosition = () => {         const fatherWidth = document.getElementById('scroll_y')?.clientWidth         const textHeight = document.getElementById('scroll_y_text')?.clientHeight         const fatherHeight = document.getElementById('scroll_y')?.clientHeight         fatherWidth && setWidth(fatherWidth)         setStopInterval(true)         if (fatherHeight && textHeight) {             setTop(fatherHeight)             if (transform < 0 && ((Math.abs(transform) - fatherHeight) > textHeight)) {                 setTransition('')                 setTransform(0)                 setTimeout(() => {                     setStopInterval(false)                 }, 100)
            } else {                 setTransition('transform 5s linear')                 setTransform(transform - fatherHeight)                 setTimeout(() => {                     setStopInterval(false)                 }, 5000)             }         }

    }     useEffect(() => {         if (stopInterval) {             clearInterval('timerY')         } else {             timerY = setInterval(setPosition, 1000)         }         return () => {             clearInterval(timerY)         }     }, [stopInterval])     return <div id="scroll_y">         <div id="scroll_y_text" style={scrollYStyle}>             {text}         </div>     </div> } export default Demo   文字渐变交替显示 demo3.less #gradient_father {   width: 300px;   height: 60px;   font-size: 12px;   background-color: #ccc;   color: green;   position: relative;   top: 200px;   left: 300px;   overflow: hidden; } #gradient_text_top {   position: absolute;   word-wrap: break-word;   top: 50px;   left: 0;   font-size: 20px;   line-height: 30px; } #gradient_text_bottom {   position: absolute;   word-wrap: break-word;   top: 50px;   left: 0;   font-size: 20px;   line-height: 30px; }   demo.tsx   import React, { useEffect, useState } from "react" import './demo3.less' const Demo = () => {     const [isFirst, setIsFirst] = useState(true)     const [opacityFirst, setOpacityFirst] = useState(1)     const [opacitySecond, setOpacitySecond] = useState(0)     const [topFirst, setTopFirst] = useState(200)     const [topSecond, setTopSecond] = useState(200)     const [width, setWidth] = useState(0)     const [transformFirst, setTransformFirst] = useState(0)     const [transformSecond, setTransformSecond] = useState(0)     const [transition, setTransition] = useState('opacity 3s linear')     const [stopInterval, setStopInterval] = useState(false)     let gradientTStyle = {         width,         top: topFirst,         transform: `translateY(${transformFirst}px)`,         transition,         opacity: opacityFirst     }     let gradientBStyle = {         width,         top: topSecond,         transform: `translateY(${transformSecond}px)`,         transition,         opacity: opacitySecond     }     let gradientTimer     const text = 'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeerrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrtttttttttttttttttttttttyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ'     const setPosition = () => {         const fatherWidth = document.getElementById('gradient_father')?.clientWidth         const textHeight = document.getElementById('gradient_text_top')?.clientHeight || document.getElementById('gradient_text_bottom')?.clientHeight         const fatherHeight = document.getElementById('gradient_father')?.clientHeight         fatherWidth && setWidth(fatherWidth)         setStopInterval(true)         if (fatherHeight && textHeight) {             if (isFirst) {                 setTopFirst(fatherHeight + fatherHeight)                 setTopSecond(fatherHeight + fatherHeight)             }             if (transformFirst < 0 && (Math.abs(transformFirst) > textHeight)) {                 setTransition('')                 setTransformFirst(0)                 setTransformSecond(0)                 setIsFirst(true)                 setTimeout(() => {                     setStopInterval(false)                 }, 100)
            } else {                 setTransition('opacity 3s linear')                 if (isFirst) {                     setTransformFirst(transformFirst - fatherHeight)                     setIsFirst(false)                 }                 if (opacitySecond === 0) {                     setTransformSecond(transformSecond - fatherHeight - fatherHeight)                 }                 if (opacityFirst === 0) {                     setTransformFirst(transformFirst - fatherHeight - fatherHeight)                 }                 setOpacityFirst(opacityFirst === 0 ? 1 : 0)                 setOpacitySecond(opacitySecond === 0 ? 1 : 0)                 setTimeout(() => {                     setStopInterval(false)                 }, 3000)             }         }

    }     useEffect(() => {         if (stopInterval) {             clearInterval('gradientTimer')         } else {             gradientTimer = setInterval(setPosition, 2000)         }         return () => {             clearInterval(gradientTimer)         }     }, [stopInterval])     return <div id="gradient_father">         <div id="gradient_text_top" style={gradientTStyle}>             {text}         </div>         <div id="gradient_text_bottom" style={gradientBStyle}>             {text}         </div>     </div> } export default Demo  

标签:文字,动画,滚动,text,fatherHeight,transform,setStopInterval,useState,const
From: https://www.cnblogs.com/zscxy/p/17958138

相关文章

  • loaders实现首页动画
     *   Loaders.css是一个性能出色的前端代码库,因为它完全没有用到一行js代码或者图片,仅仅利用纯CSS就实现多种样式的loading 动画,运行流畅丝滑,轻量小巧,性能优秀!css代码:<!DOCTYPEhtml5><head>  <linkhref="http://fonts.googleapis.com/css?family=Source+Sans+......
  • uniapp中如何实现路由跳转动画
    Laravel是一个流行的PHP框架,它具有出色的可测试性,可以帮助开发人员在更短的时间内编写可靠的代码。但是,即使使用了这个框架,也可能会出现测试覆盖率较低的情况。测试覆盖率是指代码中已由测试案例覆盖的部分比例。测试覆盖率越高,代码质量越高。在本文中,我们将分享几种技巧,帮助您提......
  • 滚动知识点
    functionpageChange(){//...页面滚动时,须要作的事情}window.addEventListener("scroll",pageChange,false);一、使用 window.addEventListener 和 document.addEventListener 来处理页面上的事件,区别仅仅在于:不一样事件模型上,处理的顺序不同。浏览器捕获,window......
  • Three.js——十五、Box3、相机动画、lookAt()视线方向、管道漫游案例、OrbitControls
    正投影相机正投影相机和透视相机的区别如果都以高处俯视去看整个场景,正投影相机就类似于2d的可视化的效果,透视相机就类似于人眼观察效果调整left,right,top,bottom范围大小如果你想整体预览全部立方体,就需要调整相机的渲染范围,比如设置上下左右的范围。使用场景:正投影可以......
  • css动画(详解)
    你是否惊讶于为啥别人的网站效果那么好,自己的只有简单的布局,和图片加持。没事,这篇博客将带你了解制作动画,让你的网页变得更加生动。动画的制作:制作动画,我们需要选择动画的类型,这里我采用的是使用keyframes类型的关键帧动画,定义一个动画@keyframes动画名称{关键帧的相应执行操......
  • Android多行横向来回自动滚动
    使用自定义RecyclerView实现,先看效果,能用得上的再继续往下看……上代码~自定义AutoRollRecyclerView,继承RecyclerView,重点只有这一个类classAutoRollRecyclerView@JvmOverloadsconstructor(context:Context,attrs:AttributeSet?=null):RecyclerView(context,a......
  • 解决IOS transform rotate后文字无法显示,backface-visibility导致@click事件失效
    问题一:IOStransformrotate后文字无法显示网上搜到可以用backface-visibility:hidden来解决,这样做文字是出来了,但是click事件无效了。问题二:backface-visibility导致@click事件失效在Vue中使用backface-visibility和@click可能会造成冲突,因为backface-visibility会影响元素背面的......
  • lottie 动画在 vue 中的使用
    前言最近我所负责的项目中,我采用了动画效果,并开始使用gif来实现。然而,在实践过程中,我发现gif格式的动画在git中出现了明显的锯齿感,这让我非常困扰。为了追求更完美的表现效果,我最终选择了lottie来实现我的动画需求。我深知动画效果的呈现对于用户体验至关重要,因此我非常认......
  • 安卓之动画使用场景以及优劣分析
    一、文章摘要自从安卓系统诞生以来,其强大的自定义能力和丰富的应用场景使得动画成为了重要的组成部分。安卓开发者们可以通过简单的API和控件实现各种各样的动画效果,不仅提高了用户体验,还增强了应用的视觉效果。本文将从使用场景和优劣分析两个方面,深入探讨安卓动画的应用。二、正......
  • 【Log4j2】Log4j2最佳实践:Log4j2配置超过7天压缩,超过3个月删除文件的滚动日志,分别定义
    目录Log4j2配置springboot多环境日志配置参考资料Log4j2配置如果你想要在控制台输出美化的日志信息,你可以使用Log4j2的ConsoleAppender和AnsiColorConverter来实现。下面是相应的配置示例:<Configurationstatus="WARN"><Properties><Propertyname="logPath">/path/t......