有一个在图像上有三重悬停效果的代码笔:Codepen

当我试图用MUI接受的MUI和MUI图像在React Typescript上将其重建为样式化组件时,我的样式={…}中出现错误,无法在我的React应用程序中工作.有人能帮忙吗?我在const样式中声明了css,并将其内联apply.

App.tsx

import React from 'react';
import { Box } from '@mui/material';
import Image from 'mui-image';

const styles = {
   body:{
       background: "#3d4552",
       fontFamily: "arial",
       fontSize: "12px",
       color: "#fff",
   },
   
   img: {
       border: "1px solid #d2d2d2",
       padding: "3px",
       boxShadow: "0 0 15px rgba(0,0,0,.1)",
   },
   
   picContainer: {
       maxWidth: "210px",
       maxHeight: "210px",
       margin: "50px",
   },
   
   pic: {
       background: "#fff",
       position: "absolute",
       transition: "all 0.2s ease",
       backfaceVisibility:"hidden",
   },
   
   "pix:nth:child(1)": {
       zIndex: 3,
   },
   
   "pic:nth-child(2)": {
       zIndex: 1,
   },
   
   "pic:nth-child(3)": {
       zIndex: 2,
   },
   
   "picContainer:hover .pic:nth-child(1)": {
       transform: "rotate(15deg)",
       transition: "all 0.5s ease",
   },
   
   "picContainer:hover .pic:nth-child(2)": {
       transform: "rotate(7deg)",
       transition: "all 0.10s ease",
   },
   
   "picContainer:hover .pic:nth-child(3)": {
       transform: "rotate(-5deg)",
   },
   
   picCaption: {
       background: "#82a3d4",
       padding: "10px 15px",
       color: "#fff",
       fontSize: "12px",
       position: "relative",
       zIndex: "10",
       top: "90px",
       left: "200px",
       borderRadius: "3px",
       transition: "all 0.5s ease",
       opacity: 0,
   },
   
   "picCaption:hover": {
       background: "#607fae",
   },
   "picContainer:hover .pic-caption": {
       left:"230px",
       opacity: 1,
   },
   
};

function Hover() {

   return (
<Box style={styles.picContainer}>
   <Image src="https://via.placeholder.com/150" style={styles.pic}  />
   <Image src="https://via.placeholder.com/150" style={styles.pic} />
   <Image src="https://via.placeholder.com/150" style={styles.pic} />
</Box>
   );
}

export { Hover };

The error:

(property) MuiImageProps.style?: React.CSSProperties | undefined

Type '{ background: string; position: string; transition: string; backfaceVisibility: string; }' is not assignable to type 'Properties<string | number, string & {}>'.
  Types of property 'backfaceVisibility' are incompatible.
    Type 'string' is not assignable to type 'BackfaceVisibility | undefined'.ts(2322)

index.d.ts(28, 5): The expected type comes from property 'style' which is declared here on type 'IntrinsicAttributes & MuiImageProps & { children?: ReactNode; }'

推荐答案

首先,我通过删除未使用或错误的样式(如picCaptionpix:nth:child(1))缩小了styles对象.自nth-child is potentially unsafe以来,我还将所有出现的:nth-child()替换为:nth-of-type().

对于该解决方案,只有两个相关的样式对象,picContainer将应用于mui <Image/>包装器的外部<Box/>img.pic类可以在样式对象内引用.我们可以利用嵌套来避免重复,类似于scss,这在这里也适用.

const styles = {
  picContainer: {
    maxWidth: "210px",
    maxHeight: "210px",
    margin: "50px",

    "&:hover": {
      ".pic:nth-of-type(1)": {
        transform: "rotate(15deg)",
        transition: "all 0.5s ease",
      },
      ".pic:nth-of-type(2)": {
        transform: "rotate(7deg)",
        transition: "all 0.10s ease",
      },
      ".pic:nth-of-type(3)": {
        transform: "rotate(-5deg)",
      },
    },
    ".pic": {
      position: "absolute",
      transition: "all 0.2s ease",
      backfaceVisibility: "hidden",

      "&:nth-of-type(2)": {
        zIndex: 1,
      },
      "&:nth-of-type(3)": {
        zIndex: 2,
      },
    },
  },
  img: {
    backgroundColor: "#fff",
    border: "1px solid #d2d2d2",
    padding: "3px",
    boxShadow: "0 0 15px rgba(0,0,0,.1)",
  },
};

自从我第一次使用mui-image中的<Image/>后,我发现这很棘手.图像被包装在一个带有自己样式的MUI图像类中.这里需要使用wrapperClassName和自定义widthheight来避免图像被扩展和拉伸.

// Hover.tsx

  return (
    <Box sx={styles.picContainer}>
      <Image
        wrapperClassName="pic"
        width={150}
        height={150}
        src="https://via.placeholder.com/150"
        style={styles.img}
      />
      <Image
        wrapperClassName="pic"
        width={150}
        height={150}
        src="https://via.placeholder.com/150"
        style={styles.img}
      />
      <Image
        wrapperClassName="pic"
        width={150}
        height={150}
        src="https://via.placeholder.com/150"
        style={styles.img}
      />
    </Box>
  );

结果与问题中提供的代码笔相同:

enter image description here

Javascript相关问答推荐

如何避免使用ajax在Vue 3合成API中重定向

如何为GrapesJS模板编辑器创建自定义撤销/重复按钮?

Vue:ref不会创建react 性属性

类型脚本中只有字符串或数字键而不是符号键的对象

防止用户在selectizeInput中取消 Select 选项

浮动Div的淡出模糊效果

使用Java脚本根据按下的按钮更改S文本

查找最长的子序列-无法重置数组

如何在Vue 3中创建自定义 Select 组件,并将选项作为HTML而不是props 传递?

我正在建立一个基于文本的游戏在react ,我是从JS转换.我怎样才能使变量变呢?

在Java中寻找三次Bezier曲线上的点及其Angular

Webpack在导入前混淆文件名

在FAQ Accodion应用程序中使用React useState时出现问题

在SHINY R中的嵌套模块中,不能使用Java代码

Phaser3 preFX addGlow不支持zoom

将相关数据组合到两个不同的数组中

输入的值的类型脚本array.排序()

Reaction useState和useLoaderData的组合使用引发无限循环错误

在高位图中显示每个y轴系列的多个值

将延迟加载的模块转换为Eager 加载的模块