react native中内容占位效果

react native中内容占位效果

效果实例图

在这里插入图片描述

实例代码skeleton.jsx

import React, { useEffect, useRef } from "react";
import { Animated, StyleSheet, View } from "react-native";
import { pxToPd } from "../../../common/js/device";

const Skeleton = ({ }) => {
    const progressBarWidth = useRef(new Animated.Value(0)).current;

    const animationFun = () => {
        Animated.timing(progressBarWidth, {
            toValue: 1, // 目标值
            duration: 1000, // 动画持续时间(毫秒)
            useNativeDriver: false // 必须设置为 false,因为布局动画不能使用原生驱动
        }).start(({ finished }) => {
            if (finished) {
                progressBarWidth.setValue(0); // 重置为初始值
                animationFun(); // 重新触发动画
            }
        });;
    }

    const widthValue = progressBarWidth.interpolate({
        inputRange: [0, 1],
        outputRange: ['0%', '100%']
    });


    useEffect(() => {
        animationFun();

        return () => { }
    }, []);

    return (
        <>
            <View style={styles.skeleton}>
                <View style={styles.skeletonLogo}>
                    <Animated.View style={[styles.progress, { width: widthValue }]} />
                </View>
                <View style={styles.skeletonName}>
                    <Animated.View style={[styles.progress, { width: widthValue }]} />
                </View>
                <View style={styles.skeletonBtn}>
                    <Animated.View style={[styles.progress, { width: widthValue }]} />
                </View>
            </View>
        </>
    )
}

const styles = StyleSheet.create({
    skeleton: {
        width: '100%',
        height: pxToPd(200),
        position: "relative",
        backgroundColor: "#fff",
        marginBottom: pxToPd(16)
    },
    skeletonLogo: {
        height: pxToPd(155),
        width: pxToPd(155),
        backgroundColor: '#f2f2f2',
        borderRadius: pxToPd(5),
        overflow: 'hidden',
        position: 'absolute',
        left: '3.2%',
        top: pxToPd(22)
    },
    skeletonName: {
        height: pxToPd(60),
        width: pxToPd(319),
        backgroundColor: '#f2f2f2',
        overflow: 'hidden',
        position: 'absolute',
        left: '29%',
        top: pxToPd(70)
    },
    skeletonBtn: {
        height: pxToPd(64),
        width: pxToPd(152),
        backgroundColor: '#f2f2f2',
        borderRadius: pxToPd(32),
        overflow: 'hidden',
        position: 'absolute',
        right: '3.2%',
        top: pxToPd(68)
    },
    progress: {
        height: '100%',
        backgroundColor: 'rgba(255, 255, 255, 0.3)'
    }
})

export default Skeleton

相关推荐

  1. go 的 fmt

    2024-06-07 20:02:02       35 阅读
  2. Mybatis的使用的名称

    2024-06-07 20:02:02       36 阅读
  3. node-mysql符?的使用

    2024-06-07 20:02:02       10 阅读
  4. js符 ${} 使用

    2024-06-07 20:02:02       31 阅读
  5. 符哈哈哈哈

    2024-06-07 20:02:02       40 阅读
  6. 使用CSS样式化文本

    2024-06-07 20:02:02       24 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-07 20:02:02       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-07 20:02:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-07 20:02:02       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-07 20:02:02       20 阅读

热门阅读

  1. 【leetcode--统计优美子数组】

    2024-06-07 20:02:02       9 阅读
  2. 高级数据结构学习

    2024-06-07 20:02:02       8 阅读
  3. reshape用法 python:深入探索多维数组的重塑技巧

    2024-06-07 20:02:02       7 阅读
  4. 一篇高效处理数据可视化Python库,看这篇就够了

    2024-06-07 20:02:02       8 阅读
  5. gpt4free软件的 g4f gui 网页速度非常慢的问题解决

    2024-06-07 20:02:02       4 阅读
  6. 深度解析 VPN 工作原理:保护隐私的关键

    2024-06-07 20:02:02       10 阅读
  7. Podman:Linux下的无守护进程容器引擎

    2024-06-07 20:02:02       9 阅读
  8. NLP基础——语言模型(动手学深度学习)

    2024-06-07 20:02:02       9 阅读
  9. 【怀旧版】win10中从零开始创建vue2+ElementUI项目

    2024-06-07 20:02:02       13 阅读
  10. 【实用技巧】Unity的Transform组件实用技巧

    2024-06-07 20:02:02       11 阅读
  11. 每日一题:聊聊 Redis 过期键的删除策略

    2024-06-07 20:02:02       9 阅读
  12. 函数或变量 ‘tfrstft‘ 无法识别

    2024-06-07 20:02:02       10 阅读
  13. 新能源汽车企业的图纸防泄密解决方案

    2024-06-07 20:02:02       10 阅读
  14. 使用React Hooks有什么优势

    2024-06-07 20:02:02       9 阅读