点击堆叠组件上的事件
P粉882357979
P粉882357979 2024-03-29 17:29:41
[CSS3讨论组]

我有两个相互堆叠的反应箭头函数组件(使用绝对定位),并且它们都有 onClick 属性。问题是,我想单击顶部的那个,并且两个 onClick 函数都会触发。有办法解决这个问题吗?

这是代码的简化版本:

const Card = ({...}) => {

    const styles = {
        optionsButton: {
            minWidth:0,
            minHeight: 0,
            padding: "2px",
            position: "absolute",
            color: "#808080",
            zIndex: 1,
            right: 5,
            top: 5,
            '&:hover':{
                backgroundColor: 'rgb(0, 0, 0, 0.1)'
            }
        },
    }

    const [hovering, setHovering] = useState(false)
    const [isCardExpanded, setIsCardExpanded] = useState(false)

    const expandCard = () => {
        setIsCardExpanded(true)
    }
    const closeCard = () => {
        setIsCardExpanded(false)
    }


    const mainPaperStyle = () => {
        let style = {
            padding: "10px",
            cursor: "pointer",
            position: "absolute",
            "&:hover": {
                filter: "brightness(97%)"
            }
        }

        //Extra code here modifying color of the style, no positioning modifications

        return style
    }

    const buttonAction = () => {
        console.log("Do action!")
    }

    return(
        <>
             setHovering(true)} onMouseLeave={() => setHovering(false)}>
                Lorem Ipsum

                {hovering &&
                    
                }
            
        
    )
}

下面的屏幕截图说明了为什么我希望将两个组件堆叠在一起:

  • 悬停之前:
  • 悬停后:

我希望当鼠标悬停在 Paper 组件顶部时出现一个按钮。问题是,当我单击按钮时, expandCardbuttonAction 都会触发。 (顺便说一句,我正在使用 Material UI)

P粉882357979
P粉882357979

全部回复(0)
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号