实现公告文字轮播效果

<template>
  <div class="announcementList">
    <el-row :gutter="20">
      <el-col :span="6" class="lable">
        <el-tag type="danger" size="small " effect="dark">公告</el-tag>
      </el-col>
      <el-col :span="12" v-if="1==0">
        <div class="displayContent">暂无公告</div>
      </el-col>
      <el-col :span="6">
          <div class="textBox">
            <transition name="slide">
              <p class="text" :key="text.id">{{text.val}}</p>
            </transition>
          </div>

      </el-col>
    </el-row>
  </div>
</template>

<script>
  import baseModule from '../baseModule'

  export default {
    name: "announcementList",
    computed: {
      text() {
        return {
          id: this.number,
          val: this.textArr[this.number]
        }
      }
    },
    mounted() {
      this.startMove()
    },
    methods: {
      startMove() {
        // eslint-disable-next-line
        let timer = setTimeout(() => {
          if (this.number === 2) {
            this.number = 0;
          } else {
            this.number += 1;
          }
          this.startMove();
        }, 2000); // 滚动不需要停顿则将2000改成动画持续时间
      }

    },
    data() {
      return {
        textArr: [
          '1 第一条公告',
          '2 第二条公告第二条公告',
          '3 第三条公告第三条公告第三条公告'
        ],
        number: 0
      }
    },

  }
</script>

<style scoped lang="less">
  .announcementList {
    margin: 0;
    padding: 0;
    font-size: 14px;

    .lable {
      margin:5px 0px 7px 10px;
    }

    .displayContent {
      color: #9f9f9f;
      text-align: center;
    }

    .textBox {
      width: 100%;
      height: 35px;
      margin: 0 auto;
      margin-top: 3px;
      overflow: hidden;
      position: relative;
      text-align: center;
    }

    .text {
      width: 100%;
      position: absolute;
      bottom: 0;
    }

    .slide-enter-active, .slide-leave-active {
      transition: all 0.5s linear;
    }
    .slide-enter {
      transform: translateY(20px) scale(1);
      opacity: 1;
    }
    .slide-leave-to {
      transform: translateY(-20px) scale(0.8);
      opacity: 0;
    }
  }
</style>
Flutter 文字轮播图(通常称为文字滚动公告或文本滑动视图)是一种在 Flutter 应用程序中展示动态变化的文字信息的组件。它通过循环滚动显示一系列预设的文字字符串,常用于广告、新闻标题或其他需要频繁更新信息的场景。在 Flutter 中,你可以使用 `SlidingUpPanel` 或者自定义一个 `StatefulWidget` 结合动画来实现这个效果。例如,可以使用 `AnimatedContainer` 和 `Positioned` 来控制文字的滚动。 以下是一个简单的例子: ```dart import 'package:flutter/material.dart'; class TextCarousel extends StatefulWidget { final List<String> texts; // 需要滚动的文字列表 const TextCarousel({Key? key, required this.texts}) : super(key: key); @override _TextCarouselState createState() => _TextCarouselState(); } class _TextCarouselState extends State<TextCarousel> { int _currentIndex = 0; Duration _animationDuration = Duration(milliseconds: 500); // 滑动速度 void _scrollToNext() { setState(() { if (_currentIndex < widget.texts.length - 1) { _currentIndex++; } else { _currentIndex = 0; } }); } @override Widget build(BuildContext context) { return SlidingUpPanel( panel: Container( child: SingleChildScrollView( padding: EdgeInsets.all(8), child: Column( mainAxisAlignment: MainAxisAlignment.start, children: [ SizedBox(height: kToolbarHeight), ...widget.texts.map((text) { return Padding( padding: EdgeInsets.symmetric(horizontal: 16), child: Text(text, style: TextStyle(fontSize: 16)), ); }).toList(), ], ), ), ), dismissible: false, position: PanelPosition.bottom, height: 100, behavior: SlidingUpPanelBehavior.canSlideVertically, clipBehavior: Clip.none, animationDuration: _animationDuration, onPanelDismissed: () {}, onPanelClosing: (double progress) {}, builder: (context, isClosed) { return IconButton( icon: isClosed ? Icon(Icons.arrow_downward) : Icon(Icons.close), onPressed: isClosed ? () {} : _scrollToNext, tooltip: isClosed ? "展开" : "关闭", ); }, ); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值