当前位置: 移动技术网 > IT编程>开发语言>JavaScript > react native 文字轮播的实现示例

react native 文字轮播的实现示例

2018年08月10日  | 移动技术网IT编程  | 我要评论
本着我为人人,人人为我的精神,敲过的代码就要分享出来! 项目需要做一个文字的轮播,开始想着是由下而上的滚动,但是还是写的不是很好,就先退而求其次,通过透明度来实现文字的滚

本着我为人人,人人为我的精神,敲过的代码就要分享出来!

项目需要做一个文字的轮播,开始想着是由下而上的滚动,但是还是写的不是很好,就先退而求其次,通过透明度来实现文字的滚动了(也不能说是滚动了,是切换)。

为了贴上来还是写了些注释的,也就不一一的解释了,很简单的代码,看注释就好了。(我就是懒)

import react, { component } from "react"
import { view, text, touchableopacity } from "react-native"

export default class textlantern extends component {
  constructor(props) {
    super(props)
    this.state = {
      nowtext: "", // 显示的文本
      opacity: 1, // 透明度
      index: 0, // 下标
      list: [], // 滚动的列表
    }
  }

  componentwillmount() {
    const { list } = this.props
    this.setstate({
      nowtext: list[0].title, // 插入显示的文本
      list, // 滚动的列表
    })
    this.onstart() // 启动计时器 a
  }

  onstart = () => {
    const { intervals = 5000 } = this.props // intervals 可为参数非必传
    this.timer = setinterval(() => {
      this.ondisappear() // 间隔intervals毫秒启动计时器b
    }, intervals)
  };

  ondisappear = () => {
    this.timer1 = setinterval(() => {
      const { opacity, index, list } = this.state
      if (opacity === 0) {
        // 当透明度为0时候开始显示在一个文本
        if (index + 2 > list.length) {
          // 当前显示的文本为最后一个时 重头再来
          this.setstate({
            nowtext: list[0].title,
            index: 0,
          })
        } else {
          this.setstate({
            nowtext: list[index + 1].title, // 下一个文本
            index: index + 1,
          })
        }
        this.onappear() // 显示
        clearinterval(this.timer1)
        return
      }
      this.setstate({
        opacity: parsefloat(math.abs(opacity - 0.04).tofixed(2)),
      })
    }, 20)
  };

  onappear = () => {
    this.timer2 = setinterval(() => {
      const { opacity } = this.state
      if (opacity === 1) {
        clearinterval(this.timer2)
        return
      }
      this.setstate({
        opacity: parsefloat(math.abs(opacity + 0.04).tofixed(2)),
      })
    }, 20)
  };

  render() {
    const { nowtext, opacity, list, index } = this.state
    return (
      <view style={{ borderwidth: 1, margin: 10, padding: 5, bordercolor: "#dddddd" }}>
        <touchableopacity activeopacity={0.75} onpress={() => {console.log(list[index].address)}}>
          <view style={{ width: "80%" }}>
            <text
              style={{
                opacity,
                fontsize: 14,
              }}
            >
              {nowtext}
            </text>
          </view>
        </touchableopacity>
      </view>
    )
  }
}

引用:

const tprops = {
      list: [
        { id: 1, title: "不是这件事很难,而是每件事都难", address: 1 },
        { id: 2, title: "稳食姐,犯法啊", address: 2 },
        { id: 3, title: "夜半诉心声,何须太高清", address: 3 },
        { id: 4, title: "失恋唱情歌,即是漏煤气关窗", address: 4 },
      ],
    }

...

<textlantern {...tprops} />

点击跳转说一下我的做法:

通过当前的 index 来拿出对应的address进行跳转。

react要用的话改一下标签就好了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网