3
import { Box, Circle, Float } from "@chakra-ui/react"
export const FloatBasic = () => (
<Box position="relative" w="80px" h="80px" bg="bg.emphasized">
<Float>
<Circle size="5" bg="red" color="white">
3
</Circle>
</Float>
</Box>
)
用法
浮动组件需要一个父元素,并应用position: relative
样式。
import { Box, Float } from "@chakra-ui/react"
<Box position="relative">
<Float>
<div />
</Float>
</Box>
示例
位置
使用 placement
属性将元素定位在容器边缘。
底部-末尾
3
底部-开始
3
顶部-末尾
3
顶部-开始
3
底部-居中
3
顶部-居中
3
中间-居中
3
中间-末尾
3
中间-开始
3
import { Box, Circle, Float, HStack, Stack } from "@chakra-ui/react"
export const FloatWithPlacements = () => (
<HStack gap="14" wrap="wrap">
{placements.map((placement) => (
<Stack key={placement} gap="3">
<p>{placement}</p>
<Box position="relative" width="80px" height="80px" bg="bg.emphasized">
<Float placement={placement}>
<Circle size="5" bg="red" color="white">
3
</Circle>
</Float>
</Box>
</Stack>
))}
</HStack>
)
const placements = [
"bottom-end",
"bottom-start",
"top-end",
"top-start",
"bottom-center",
"top-center",
"middle-center",
"middle-end",
"middle-start",
] as const
X轴偏移
使用 offsetX
属性沿 X 轴偏移元素。
3
import { Box, Circle, Float } from "@chakra-ui/react"
export const FloatWithOffsetX = () => (
<Box position="relative" w="80px" h="80px" bg="bg.emphasized">
<Float offsetX="-4">
<Circle size="5" bg="red" color="white">
3
</Circle>
</Float>
</Box>
)
Y轴偏移
使用 offsetY
属性沿 Y 轴偏移元素。
3
import { Box, Circle, Float } from "@chakra-ui/react"
export const FloatWithOffsetY = () => (
<Box position="relative" w="80px" h="80px" bg="bg.emphasized">
<Float offsetY="-4">
<Circle size="5" bg="red" color="white">
3
</Circle>
</Float>
</Box>
)
偏移
使用 offset
属性同时沿 X 和 Y 轴偏移元素。
3
import { Box, Circle, Float } from "@chakra-ui/react"
export const FloatWithOffset = () => (
<Box position="relative" w="80px" h="80px" bg="bg.emphasized">
<Float offset="4">
<Circle size="5" bg="red" color="white">
3
</Circle>
</Float>
</Box>
)
头像
这是一个将 Float
组件与 Avatar
组件组合使用的示例。
新
import { Avatar, Badge, Box, Float } from "@chakra-ui/react"
const Demo = () => {
return (
<Box display="inline-block" pos="relative">
<Avatar.Root size="lg" shape="rounded">
<Avatar.Image src="https://bit.ly/dan-abramov" />
<Avatar.Fallback />
</Avatar.Root>
<Float placement="bottom-end">
<Badge size="sm" variant="solid" colorPalette="teal">
New
</Badge>
</Float>
</Box>
)
}
属性
属性 | 默认值 | 类型 |
---|---|---|
placement | 'top-end' | ConditionalValue< | 'bottom-end' | 'bottom-start' | 'top-end' | 'top-start' | 'bottom-center' | 'top-center' | 'middle-center' | 'middle-end' | 'middle-start' > 指示器的位置 |
offsetX | SystemStyleObject['left'] 指示器沿 X 轴的偏移量 | |
offsetY | SystemStyleObject['top'] 指示器沿 Y 轴的偏移量 | |
offset | SystemStyleObject['top'] 指示器沿 X 轴和 Y 轴的偏移量 |