Carousel blocks
Block's Carousel
Introduction
Slider Layout, like Flex Layout, is a flexible way to create a new block from other blocks using children. It allows sliders to be created from other blocks, such as info-cards and even flex-layout for example.
With the Slider Layout, it is possible to create a carousel not only with images but with any content you want. For this example, we will use the Slider Layout to turn a set of info-cards into a slider.
Slider Layout
Looking at the documentation, we see that you can use an array of blocks like children, just like in Flex Layout.
Below is an example of implementing a slider-layout with two info-cards:
"slider-layout#home": {
"children": ["info-card#1", "info-card#2"],
"props": {
"autoplay": {
"timeout": 5000,
"stopOnHover": false
}
}
},
"info-card#1": {
"props": {
"imageUrl": "https://images.unsplash.com/photo-1524185962737-ea7c028a12cd?ixlib=rb-1.2.1&auto=format&fit=crop&w=1350&q=80",
"isFullModeStyle": true,
"headline": "Black Friday",
"callToActionText": "Subscribe",
"textPosition": "center"
}
},
"info-card#2": {
"props": {
"imageUrl": "https://images.unsplash.com/photo-1524185962737-ea7c028a12cd?ixlib=rb-1.2.1&auto=format&fit=crop&w=1350&q=80",
"isFullModeStyle": true,
"headline": "Black Friday",
"callToActionText": "Subscribe",
"textPosition": "center"
}
}
Activity
In this activity, we will create a brand slider for our website:
-
In the
home.jsoncfile, declare theslider-layout#homeblock to thestore.hometemplate. -
Create a file called
slider-layout.jsoncinside the/store/blocksfolder; -
In this file, based on the code above, replace the
info-carddeclared as children ofslider-layout#homeand add 6 image componentsimageas children. Use the formatimage# brand1,image#brand2(...)image#brand6to declare the components; -
State a specific
srcprop for each definedimage#brand. Use the URLs below for each one:https://appliancetheme.vteximg.com.br/arquivos/flatflat-brand-logo-square1.pnghttps://appliancetheme.vteximg.com.br/arquivos/flatflat-brand-logo-square2.pnghttps://appliancetheme.vteximg.com.br/arquivos/flatflat-brand-logo-square3.pnghttps://appliancetheme.vteximg.com.br/arquivos/flatflat-brand-logo-square4.pnghttps://appliancetheme.vteximg.com.br/arquivos/flatflat-brand-logo-square5.pnghttps://appliancetheme.vteximg.com.br/arquivos/flatflat-brand-logo-square6.png
-
Finally, you must use the
autoplayproperty in theslider-layout# homeblock. Make slide happen automatically every 7 seconds and stop when the user hovers overslide .
Note: Remember to access the Slider Layout and Image documentations if you have any questions during the activity.
Any questions?
See the answersheet for this step or check our office hours on the VTEX Developers channel.
Help us make this content better!
VTEX IO courses are open source. If you see something wrong, you can open a pull request!
Make a contribution
Updated 6 months ago
