国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

react native 文字輪播的實現示例

2019-11-19 13:24:27
字體:
來源:轉載
供稿:網友

本著我為人人,人人為我的精神,敲過的代碼就要分享出來!

項目需要做一個文字的輪播,開始想著是由下而上的滾動,但是還是寫的不是很好,就先退而求其次,通過透明度來實現文字的滾動了(也不能說是滾動了,是切換)。

為了貼上來還是寫了些注釋的,也就不一一的解釋了,很簡單的代碼,看注釋就好了。(我就是懶)

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要用的話改一下標簽就好了。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 高台县| 辽宁省| 呼玛县| 于田县| 山西省| 宁河县| 温宿县| 潞西市| 农安县| 德昌县| 中方县| 新沂市| 海淀区| 建水县| 莱阳市| 河西区| 巢湖市| 靖边县| 东至县| 夏邑县| 马龙县| 祁连县| 临潭县| 柳州市| 那曲县| 周至县| 巴青县| 郓城县| 汶上县| 怀集县| 广丰县| 肃宁县| 鹤庆县| 文水县| 金堂县| 廊坊市| 本溪| 甘谷县| 任丘市| 海南省| 南岸区|