轮播
使用 Embla 构建的具有运动和滑动功能的轮播。
关于
¥About
轮播组件是使用 Embla Carousel 库构建的。
¥The carousel component is built using the Embla Carousel library.
安装
¥Installation
pnpm dlx shadcn@latest add carousel
用法
¥Usage
import {
Carousel,
CarouselContent,
CarouselItem,
CarouselNext,
CarouselPrevious,
} from "@/components/ui/carousel"
<Carousel>
<CarouselContent>
<CarouselItem>...</CarouselItem>
<CarouselItem>...</CarouselItem>
<CarouselItem>...</CarouselItem>
</CarouselContent>
<CarouselPrevious />
<CarouselNext />
</Carousel>
示例
¥Examples
大小
¥Sizes
要设置项目的大小,你可以在 <CarouselItem />
上使用 basis
实用程序类。
¥To set the size of the items, you can use the basis
utility class on the <CarouselItem />
.
// 33% of the carousel width.
<Carousel>
<CarouselContent>
<CarouselItem className="basis-1/3">...</CarouselItem>
<CarouselItem className="basis-1/3">...</CarouselItem>
<CarouselItem className="basis-1/3">...</CarouselItem>
</CarouselContent>
</Carousel>
// 50% on small screens and 33% on larger screens.
<Carousel>
<CarouselContent>
<CarouselItem className="md:basis-1/2 lg:basis-1/3">...</CarouselItem>
<CarouselItem className="md:basis-1/2 lg:basis-1/3">...</CarouselItem>
<CarouselItem className="md:basis-1/2 lg:basis-1/3">...</CarouselItem>
</CarouselContent>
</Carousel>
间距
¥Spacing
要设置项目之间的间距,我们在 <CarouselItem />
上使用 pl-[VALUE]
实用程序,在 <CarouselContent />
上使用负 -ml-[VALUE]
。
¥To set the spacing between the items, we use a pl-[VALUE]
utility on the <CarouselItem />
and a negative -ml-[VALUE]
on the <CarouselContent />
.
原因:我尝试在 <CarouselContent />
上使用 gap
属性或 grid
布局,但需要大量的数学和脑力劳动才能获得正确的间距。我发现 pl-[VALUE]
和 -ml-[VALUE]
实用程序更容易使用。
¥Why: I tried to use the gap
property or a grid
layout on the <CarouselContent />
but it required a lot of math and mental effort to get the
spacing right. I found pl-[VALUE]
and -ml-[VALUE]
utilities much easier to
use.
如果需要,你可以随时在自己的项目中进行调整。
¥You can always adjust this in your own project if you need to.
<Carousel>
<CarouselContent className="-ml-4">
<CarouselItem className="pl-4">...</CarouselItem>
<CarouselItem className="pl-4">...</CarouselItem>
<CarouselItem className="pl-4">...</CarouselItem>
</CarouselContent>
</Carousel>
<Carousel>
<CarouselContent className="-ml-2 md:-ml-4">
<CarouselItem className="pl-2 md:pl-4">...</CarouselItem>
<CarouselItem className="pl-2 md:pl-4">...</CarouselItem>
<CarouselItem className="pl-2 md:pl-4">...</CarouselItem>
</CarouselContent>
</Carousel>
方向
¥Orientation
使用 orientation
属性设置轮播的方向。
¥Use the orientation
prop to set the orientation of the carousel.
<Carousel orientation="vertical | horizontal">
<CarouselContent>
<CarouselItem>...</CarouselItem>
<CarouselItem>...</CarouselItem>
<CarouselItem>...</CarouselItem>
</CarouselContent>
</Carousel>
选项
¥Options
你可以使用 opts
属性将选项传递给轮播。有关更多信息,请参阅 Embla Carousel 文档。
¥You can pass options to the carousel using the opts
prop. See the Embla Carousel docs for more information.
<Carousel
opts={{
align: "start",
loop: true,
}}
>
<CarouselContent>
<CarouselItem>...</CarouselItem>
<CarouselItem>...</CarouselItem>
<CarouselItem>...</CarouselItem>
</CarouselContent>
</Carousel>
API
使用状态和 setApi
属性获取轮播 API 的实例。
¥Use a state and the setApi
props to get an instance of the carousel API.
import { type CarouselApi } from "@/components/ui/carousel"
export function Example() {
const [api, setApi] = React.useState<CarouselApi>()
const [current, setCurrent] = React.useState(0)
const [count, setCount] = React.useState(0)
React.useEffect(() => {
if (!api) {
return
}
setCount(api.scrollSnapList().length)
setCurrent(api.selectedScrollSnap() + 1)
api.on("select", () => {
setCurrent(api.selectedScrollSnap() + 1)
})
}, [api])
return (
<Carousel setApi={setApi}>
<CarouselContent>
<CarouselItem>...</CarouselItem>
<CarouselItem>...</CarouselItem>
<CarouselItem>...</CarouselItem>
</CarouselContent>
</Carousel>
)
}
事件
¥Events
你可以使用 setApi
中的 api 实例监听事件。
¥You can listen to events using the api instance from setApi
.
import { type CarouselApi } from "@/components/ui/carousel"
export function Example() {
const [api, setApi] = React.useState<CarouselApi>()
React.useEffect(() => {
if (!api) {
return
}
api.on("select", () => {
// Do something on select.
})
}, [api])
return (
<Carousel setApi={setApi}>
<CarouselContent>
<CarouselItem>...</CarouselItem>
<CarouselItem>...</CarouselItem>
<CarouselItem>...</CarouselItem>
</CarouselContent>
</Carousel>
)
}
有关使用事件的更多信息,请参阅 Embla Carousel 文档。
¥See the Embla Carousel docs for more information on using events.
插件
¥Plugins
你可以使用 plugins
属性将插件添加到轮播。
¥You can use the plugins
prop to add plugins to the carousel.
import Autoplay from "embla-carousel-autoplay"
export function Example() {
return (
<Carousel
plugins={[
Autoplay({
delay: 2000,
}),
]}
>
// ...
</Carousel>
)
}
有关使用插件的更多信息,请参阅 Embla Carousel 文档。
¥See the Embla Carousel docs for more information on using plugins.