React 实战选择互动特效小功能

javascript 复制代码
import React, { useState } from 'react';
import { useDispatch } from 'react-redux';
import { setSwitchPage, updateSelectedEffect } from '../store/selectionSlice';
import { ArrowLeftOutlined } from '@ant-design/icons';
import { Button, Typography, Image, Row, Col, Card } from 'antd';

export interface MainProps {}

const { Title } = Typography;

const InteractEffectPannel: React.FC<MainProps> = () => {
    const dispatch = useDispatch();
    const effects = [
        { id: 1, src: require('../assets/pika/pika1.jpg'), label: '皮卡1' },
        { id: 2, src: require('../assets/pika/pika2.jpg'), label: '皮卡2' },
        { id: 3, src: require('../assets/pika/pika3.jpg'), label: '皮卡3' },
        { id: 4, src: require('../assets/pika/pika4.jpg'), label: '皮卡4' },
        { id: 5, src: require('../assets/pika/pika5.jpg'), label: '皮卡5' },
        { id: 6, src: require('../assets/pika/pika6.jpg'), label: '皮卡6' },
        { id: 7, src: require('../assets/pika/pika7.jpg'), label: '皮卡7' },
        { id: 8, src: require('../assets/pika/pika8.jpg'), label: '皮卡8' },
    ];
    const effectsGif = [
        { id: 1, src: require('../assets/pika/pika1.jpg'), label: '皮卡1' },
        { id: 2, src: require('../assets/pika/pika2.jpg'), label: '皮卡2' },
        { id: 3, src: require('../assets/pika/pika3.jpg'), label: '皮卡3' },
        { id: 4, src: require('../assets/pika/pika4.jpg'), label: '皮卡4' },
        { id: 5, src: require('../assets/pika/pika5.jpg'), label: '皮卡5' },
        { id: 6, src: require('../assets/pika/pika6.jpg'), label: '皮卡6' },
        { id: 7, src: require('../assets/pika/pika7.jpg'), label: '皮卡7' },
        { id: 8, src: require('../assets/pika/pika8.jpg'), label: '皮卡8' },
    ];

    const [hoveredIndex, setHoveredIndex] = useState<number | null>(null);
    const [hoverPosition, setHoverPosition] = useState<{ top: number; left: number } | null>(null);
    const [selectedEffect, setSelectedEffect] = useState<{src: string, label: string} | null>(null);
    const [selectedIndex, setSelectedIndex] = useState<number | null>(null);

    const handleMouseEnter = (event: any, index: number) => {
        const rect = event.target.getBoundingClientRect();
        let left = rect.left - 25;
        const viewWidth = 700;
        console.log('left:',left)
        if (left < 0) {
            left = 10;
        }

        setHoveredIndex(index);
        setHoverPosition({ top: rect.top + rect.height + 10, left: left });
    };

    const handleEffectSelect = (effect: {src: string, label: string}, index: number) => {
        setSelectedEffect({src: effect.src, label: effect.label});
        setSelectedIndex(index);
    };

    const handleConfirm = () => {
        if (selectedEffect) {
            dispatch(updateSelectedEffect(selectedEffect));
        }
    };

    return (
        <Card style={{ width: 720, height: 700, backgroundColor: '#2E3137', borderRadius: 12 }}>
            <Row align="middle" justify="space-between" style={{ padding: '10px 10px', borderBottom: '1px solid rgba(255, 255, 255, 0.1)' }}>
                <Button
                    type="link"
                    icon={<ArrowLeftOutlined style={{ color: '#ffffff' }} />}
                    onClick={() => dispatch(setSwitchPage(false))}
                />
                <Title level={4} style={{ color: '#FFFFFF' }}>选择互动特效</Title>
                <Button type="link" style={{ color: '#FFD736' }} onClick={handleConfirm}>确定</Button>
            </Row>
            <Row gutter={[24, 24]} style={{ padding: '20px', overflowY: 'auto', maxHeight: '700px' }}>
                {effects.map((effect, index) => (
                    <Col key={effect.id} xs={12} sm={6} md={6}>
                        <Card
                            hoverable
                            bordered={false}
                            onMouseEnter={(event) => handleMouseEnter(event, index)}
                            onMouseLeave={() => setHoveredIndex(null)}
                            onClick={() => handleEffectSelect(effect, index)}
                            style={{
                                display: 'flex',
                                flexDirection: 'column',
                                justifyContent: 'center',
                                alignItems: 'center',
                                width: 140,
                                height: 140,
                                position: 'relative',
                                boxSizing: 'border-box',
                                textAlign: 'center',
                                backgroundColor: 'rgba(0,0,0,0.5)',
                                padding: 0 // 确保没有额外的内边距
                            }}
                        >
                            <div style={{
                                display: 'flex',
                                justifyContent: 'center',
                                alignItems: 'center',
                                height: '100%'
                            }}>
                                <Image preview={false} src={effect.src} width={80} height={80} />
                            </div>
                            <Title level={5} style={{ color: 'rgba(255, 255, 255, 0.7)', fontSize: '12px', marginTop: '-7px' }}>{effect.label}</Title>
                            {(hoveredIndex === index || selectedIndex === index) && (
                                <div style={{
                                    position: 'absolute',
                                    top: '-5px',
                                    left: '-5px',
                                    right: '-5px',
                                    bottom: '-5px',
                                    border: '2px solid yellow',
                                    borderRadius: '10px',
                                    boxSizing: 'border-box'
                                }} />
                            )}
                        </Card>
                    </Col>
                ))}
            </Row>
            {hoveredIndex !== null && hoverPosition && (
                <div
                    style={{
                        position: 'absolute',
                        top: hoverPosition.top,
                        left: hoverPosition.left,
                        width: '180px',
                        height: '180px',
                        zIndex: 1000,
                        borderRadius: '10px',
                        boxShadow: '0 0 20px rgba(0, 0, 0, 0.4)',
                        backgroundColor: '#2E3137',
                        overflow: 'hidden',
                        paddingTop:5,
                    }}
                >
                    <Image preview={false} src={effectsGif[hoveredIndex].src} width={170} height={170} />
                </div>
            )}
        </Card>
    );
};

export default InteractEffectPannel;
相关推荐
奔跑的web.1 分钟前
UniApp 路由导航守
前端·javascript·uni-app
EchoEcho5 分钟前
记录overflow:hidden和scrollIntoView导致的页面问题
前端·css
Cache技术分享7 分钟前
318. Java Stream API - 深入理解 Java Stream 的中间 Collector —— mapping、filtering 和 fla
前端·后端
liyang_ii9 分钟前
createAsyncThunk
react.js
竟未曾年少轻狂10 分钟前
Vue3 生命周期钩子
前端·javascript·vue.js·前端框架·生命周期
TT哇15 分钟前
【实习】数字营销系统 银行经理端(interact_bank)前端 Vue 移动端页面的 UI 重构与优化
java·前端·vue.js·ui
蓝帆傲亦16 分钟前
Web前端跨浏览器兼容性完全指南:构建无缝用户体验的最佳实践
前端
晴殇i20 分钟前
【前端缓存】localStorage 是同步还是异步的?为什么?
前端·面试
不一样的少年_22 分钟前
Chrome 插件实战:如何实现“杀不死”的可靠数据上报?
前端·javascript·监控
深度涌现22 分钟前
DNS详解——域名是如何解析的
前端