Carousel
The Carousel (or Slider) component is a simple slider that allows users to navigate through a series of slides.
Add these before the </body> tag in your site/page settings
<script src="https://cdn.jsdelivr.net/npm/gsap@3.12.5/dist/gsap.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/socks-ui@{v}/dist/carousel.js"></script>
- Full keyboard navigation
- Autoplay with pause button
- Customizable threshold for dragging
- Customizable active slide class
- Customizable dragging class
- Customizable disabled button class
- Dots navigation
- No surprise CSS changes. Style it how you want and it will look exactly the same live.
- Wrapper
- Root
- Navigation
- Next Arrow
- Previous Arrow
- Pause Button (Optional - only for autoplay)
- Dots
- Dot (Rest will be generated)
Main Attributes:
s-carousel = wrapper - The wrapper element that holds the slides and the navigation elements. Each carousel wrapper will initialize a new carousel. All other elements need to be inside this wrapper.
s-carousel = root - The element that holds all the slides. The carousel works by moving this element left and right; not the individual slides. You can use flex or grid to create the gap between the slides.
s-carousel
= slide
- The individual slides.
s-carousel
= next
- The "Next slide" button
s-carousel
= prev
- The "Previous slide" button
s-carousel
= pause
- The "Pause autoplay" button.
s-carousel
= dot
- The dot that will be used for navigation. Will be used as a template to generate the other dots. You can add the active class to it to avoid losing the styles.
Options (add to wrapper element):
s-threshold
- Optional attribute for how much the user needs to drag to change slide (Accepted values: 0.01 - 1). Default: 0.3
s-active-class
- The active class added to the active slide. Default: s-active
s-dragging-class
- The class that will be added while the user is dragging. Default: s-dragging
s-disabled-class
- The class that will be added to buttons when the user reaches the first/last slide. Default: s-disabled
s-paused-class
- The class that will be added to the pause button when autoplay is paused. Default: s-paused
s-autoplay
- Activates autoplay. Set the value to a number to control the timing. Default: 5
(seconds).
Note: You need to include a pause button to make this feature accessible.
s-duration
- The duration of the slide animation. Default: 0.5
(seconds)
s-ease
- The easing of the slide animation. Default: power2.out
(see GSAP Easing)
s-loop
- Whether to go back to the first slide after the user reaches the end. Default: false
s-slides-to-scroll
- How many slides will be visible and will scroll when a navigation button is clicked.
Events
init
- Carousel initialized
destroy
- Carousel destroyed
change
- Slide changed
dragstart
- User started dragging
dragend
- User stopped dragging
// Get carousel instance
const carousel = socks.carousel['carousel-0']
// Subscribe to events
// carousel initialized
carousel.on('init', (detail) => {
console.log('Carousel initialized:', detail)
})
// slide changed
carousel.on('change', ({ currentIndex, previousIndex }) => {
console.log(`Slide changed from ${previousIndex} to ${currentIndex}`)
})
// drag started
carousel.on('dragstart', ({ startX, currentX }) => {
console.log('Started dragging at:', startX)
})
// drag ended
carousel.on('dragend', ({ dragDistance, currentIndex }) => {
console.log('Ended dragging, moved:', dragDistance)
})
// carousel destroyed
carousel.on('destroy', (detail) => {
console.log('Carousel destroyed:', detail)
})
// Unsubscribe from events
const onChangeHandler = ({ currentIndex }) => console.log('Current slide:', currentIndex)
carousel.on('change', onChangeHandler)
// Later...
carousel.off('change', onChangeHandler)
No custom events are exported for this component.
Methods
Navigation Methods
goToSlide(index, animate)
index
: The slide number to navigate to (zero-based)animate
: Whether to animate the transition (defaults to true)
goToNext()
- No parameters, moves to next slide
goToPrev()
- No parameters, moves to previous slide
Playback Control
startAutoplay()
- No parameters, begins auto-advancing slides
stopAutoplay()
- No parameters, stops auto-advancing slides
resetSlider()
- No parameters, resets carousel to initial state
State Management
enable()
- No parameters, enables all carousel functionality
disable()
- No parameters, disables all carousel functionality
destroy()
- No parameters, removes all event listeners and cleanup
Example usage
const carousel = window.socks.carousel['carousel-0']
carousel.goToNext() // next slide
No custom methods are exported for this component.
Use Cases
Sync a carousel with another one
If you want a second carousel to be synced with your first carousel, you can listen for changes on the first one and change based on the new slide index:
socks.carousel['carousel-0'].on('change', (e)=> {
socks.carousel['carousel-1'].goToSlide(e.currentSlide)
})
2-way sync
If you want the sync to happen both ways, we have to write a little bit more code to avoid infinite loops:
let isChanging = false
socks.carousel['carousel-0'].on('change', (e) => {
if (!isChanging) {
isChanging = true
socks.carousel['carousel-1'].goToSlide(e.currentIndex)
isChanging = false
}
})
socks.carousel['carousel-1'].on('change', (e) => {
if (!isChanging) {
isChanging = true
socks.carousel['carousel-0'].goToSlide(e.currentIndex)
isChanging = false
}
})
No special use cases added yet
{"type":"@webflow/XscpData","payload":{"nodes":[{"_id":"b4887368-df6c-4826-b13c-d5f0cba00b41","type":"Block","tag":"div","classes":["20c9e54d-eb64-cdf3-3740-db62ddf605fa"],"children":["e6485fbb-8015-0f6e-b8ae-6694cde27469","349afdc5-ba51-f658-f5b2-8034c799974e","12cb124b-8d65-61df-6a3b-7814e34ade85"],"data":{"text":false,"tag":"div","devlink":{"runtimeProps":{},"slot":""},"displayName":"","attr":{"id":""},"xattr":[{"name":"s-carousel","value":"wrapper"},{"name":"s-active-class","value":"cc-active"},{"name":"s-threshold","value":"0.2"},{"name":"s-autoplay","value":"4"},{"name":"s-duration","value":"0.6"},{"name":"s-ease","value":"expo.out"},{"name":"s-paused-class","value":"cc-paused"}],"search":{"exclude":false},"visibility":{"conditions":[]}}},{"_id":"e6485fbb-8015-0f6e-b8ae-6694cde27469","type":"Block","tag":"div","classes":["fb3f1b2e-88f4-b17a-5517-a33957406f50"],"children":["7fbe9c4f-2737-caf7-e624-940d9126e112","c2d923f1-81b5-7efe-8aec-0ca52401d871","954af422-e5fb-608c-21ec-53a42183096c"],"data":{"text":false,"tag":"div","devlink":{"runtimeProps":{},"slot":""},"displayName":"","attr":{"id":""},"xattr":[{"name":"s-carousel","value":"root"}],"search":{"exclude":false},"visibility":{"conditions":[]}}},{"_id":"7fbe9c4f-2737-caf7-e624-940d9126e112","type":"Block","tag":"div","classes":["46abd4df-7bb4-0ba1-63bd-c79a4af4c58b","d73a9728-12a3-a170-d6c4-3fac95e918f5"],"children":["7dcdccc5-a1f6-0027-dcaf-f18fd3fc2a9d"],"data":{"text":false,"tag":"div","devlink":{"runtimeProps":{},"slot":""},"displayName":"","attr":{"id":""},"xattr":[{"name":"s-carousel","value":"slide"}],"search":{"exclude":false},"visibility":{"conditions":[]}}},{"_id":"7dcdccc5-a1f6-0027-dcaf-f18fd3fc2a9d","type":"Block","tag":"div","classes":[],"children":["4aa67766-191b-fd10-9199-b9c1517b5dc1"],"data":{"text":true,"tag":"div","devlink":{"runtimeProps":{},"slot":""},"displayName":"","attr":{"id":""},"xattr":[],"search":{"exclude":false},"visibility":{"conditions":[]}}},{"_id":"4aa67766-191b-fd10-9199-b9c1517b5dc1","text":true,"v":"Slide 1"},{"_id":"c2d923f1-81b5-7efe-8aec-0ca52401d871","type":"Block","tag":"div","classes":["46abd4df-7bb4-0ba1-63bd-c79a4af4c58b"],"children":["371479b2-6798-f179-3102-256599700e60"],"data":{"text":false,"tag":"div","devlink":{"runtimeProps":{},"slot":""},"displayName":"","attr":{"id":""},"xattr":[{"name":"s-carousel","value":"slide"}],"search":{"exclude":false},"visibility":{"conditions":[]}}},{"_id":"371479b2-6798-f179-3102-256599700e60","type":"Block","tag":"div","classes":[],"children":["827391f6-5515-e332-1f0a-de5594cf8cef"],"data":{"text":true,"tag":"div","devlink":{"runtimeProps":{},"slot":""},"displayName":"","attr":{"id":""},"xattr":[],"search":{"exclude":false},"visibility":{"conditions":[]}}},{"_id":"827391f6-5515-e332-1f0a-de5594cf8cef","text":true,"v":"Slide 2"},{"_id":"954af422-e5fb-608c-21ec-53a42183096c","type":"Block","tag":"div","classes":["46abd4df-7bb4-0ba1-63bd-c79a4af4c58b"],"children":["a86a73da-a2ba-c05b-2676-2513449e9300"],"data":{"text":false,"tag":"div","devlink":{"runtimeProps":{},"slot":""},"displayName":"","attr":{"id":""},"xattr":[{"name":"s-carousel","value":"slide"}],"search":{"exclude":false},"visibility":{"conditions":[]}}},{"_id":"a86a73da-a2ba-c05b-2676-2513449e9300","type":"Block","tag":"div","classes":[],"children":["1b7aa3fb-0851-d6af-58a9-4dc47948954e"],"data":{"text":true,"tag":"div","devlink":{"runtimeProps":{},"slot":""},"displayName":"","attr":{"id":""},"xattr":[],"search":{"exclude":false},"visibility":{"conditions":[]}}},{"_id":"1b7aa3fb-0851-d6af-58a9-4dc47948954e","text":true,"v":"Slide 3"},{"_id":"349afdc5-ba51-f658-f5b2-8034c799974e","type":"Block","tag":"div","classes":["d6a5c462-70e9-ee0c-6dd0-d87e7e2cc9e7"],"children":["02e019bb-afd0-e4c2-6f07-8f8ae55b7c8a","08453eb9-0df1-deae-90ae-0dfb5803f517","6bfa7c07-6d1a-02ad-bfbd-49fe8d0d035d"],"data":{"text":false,"tag":"div","devlink":{"runtimeProps":{},"slot":""},"displayName":"","attr":{"id":""},"xattr":[],"search":{"exclude":false},"visibility":{"conditions":[]}}},{"_id":"02e019bb-afd0-e4c2-6f07-8f8ae55b7c8a","type":"Block","tag":"div","classes":["53efd51e-8f59-597d-e776-234589ff5f4a"],"children":["bf77c88a-b4b9-6802-3680-14ba0ec743d3"],"data":{"text":false,"tag":"div","devlink":{"runtimeProps":{},"slot":""},"displayName":"","attr":{"id":""},"xattr":[{"name":"s-carousel","value":"prev"}],"search":{"exclude":false},"visibility":{"conditions":[]}}},{"_id":"bf77c88a-b4b9-6802-3680-14ba0ec743d3","type":"HtmlEmbed","tag":"div","classes":[],"children":[],"v":"<svg width=\"24\" height=\"24\" viewBox=\"0 0 24 24\" fill=\"none\" xmlns=\"http://www.w3.org/2000/svg\">\n<path d=\"M15 18L9 12L15 6\" stroke=\"currentColor\" stroke-width=\"2.18182\" stroke-linecap=\"round\" stroke-linejoin=\"round\"/>\n</svg>","data":{"search":{"exclude":true},"embed":{"meta":{"html":"<svg width=\"24\" height=\"24\" viewBox=\"0 0 24 24\" fill=\"none\" xmlns=\"http://www.w3.org/2000/svg\">\n<path d=\"M15 18L9 12L15 6\" stroke=\"currentColor\" stroke-width=\"2.18182\" stroke-linecap=\"round\" stroke-linejoin=\"round\"/>\n</svg>","div":false,"script":false,"compilable":false,"iframe":false},"type":"html"},"insideRTE":false,"devlink":{"runtimeProps":{},"slot":""},"displayName":"","attr":{"id":""},"xattr":[],"visibility":{"conditions":[]}}},{"_id":"08453eb9-0df1-deae-90ae-0dfb5803f517","type":"Block","tag":"div","classes":["53efd51e-8f59-597d-e776-234589ff5f4a"],"children":["9877c368-3ef9-5f6f-eaae-f0e13e1a6d73"],"data":{"text":false,"tag":"div","devlink":{"runtimeProps":{},"slot":""},"displayName":"","attr":{"id":""},"xattr":[{"name":"s-carousel","value":"next"}],"search":{"exclude":false},"visibility":{"conditions":[]}}},{"_id":"9877c368-3ef9-5f6f-eaae-f0e13e1a6d73","type":"HtmlEmbed","tag":"div","classes":[],"children":[],"v":"<svg width=\"24\" height=\"24\" viewBox=\"0 0 24 24\" fill=\"none\" xmlns=\"http://www.w3.org/2000/svg\">\n<path d=\"M9 18L15 12L9 6\" stroke=\"currentColor\" stroke-width=\"2.18182\" stroke-linecap=\"round\" stroke-linejoin=\"round\"/>\n</svg>","data":{"search":{"exclude":true},"embed":{"meta":{"html":"<svg width=\"24\" height=\"24\" viewBox=\"0 0 24 24\" fill=\"none\" xmlns=\"http://www.w3.org/2000/svg\">\n<path d=\"M9 18L15 12L9 6\" stroke=\"currentColor\" stroke-width=\"2.18182\" stroke-linecap=\"round\" stroke-linejoin=\"round\"/>\n</svg>","div":false,"script":false,"compilable":false,"iframe":false},"type":"html"},"insideRTE":false,"devlink":{"runtimeProps":{},"slot":""},"displayName":"","attr":{"id":""},"xattr":[],"visibility":{"conditions":[]}}},{"_id":"6bfa7c07-6d1a-02ad-bfbd-49fe8d0d035d","type":"Block","tag":"div","classes":["53efd51e-8f59-597d-e776-234589ff5f4a"],"children":["e53e0c96-d916-2122-3e47-e0ad5c54deba"],"data":{"text":false,"tag":"div","devlink":{"runtimeProps":{},"slot":""},"displayName":"","attr":{"id":""},"xattr":[{"name":"s-carousel","value":"pause"}],"search":{"exclude":false},"visibility":{"conditions":[]}}},{"_id":"e53e0c96-d916-2122-3e47-e0ad5c54deba","type":"HtmlEmbed","tag":"div","classes":[],"children":[],"v":"<svg class=\"carousel_pause-icon\" height=\"24px\" id=\"Layer_1\" style=\"enable-background:new 0 0 512 512;\" version=\"1.1\" viewBox=\"0 0 512 512\" width=\"24px\" xml:space=\"preserve\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><g><path d=\"M224,435.8V76.1c0-6.7-5.4-12.1-12.2-12.1h-71.6c-6.8,0-12.2,5.4-12.2,12.1v359.7c0,6.7,5.4,12.2,12.2,12.2h71.6 C218.6,448,224,442.6,224,435.8z\"/><path d=\"M371.8,64h-71.6c-6.7,0-12.2,5.4-12.2,12.1v359.7c0,6.7,5.4,12.2,12.2,12.2h71.6c6.7,0,12.2-5.4,12.2-12.2V76.1 C384,69.4,378.6,64,371.8,64z\"/></g></svg>\n\n<svg class=\"carousel_play-icon\" enable-background=\"new 0 0 139 139\" height=\"24px\" id=\"Play\" version=\"1.1\" viewBox=\"0 0 139 139\" width=\"24px\" xml:space=\"preserve\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><path d=\"M117.037,61.441L36.333,14.846c-2.467-1.424-5.502-1.424-7.972,0c-2.463,1.423-3.982,4.056-3.982,6.903v93.188 c0,2.848,1.522,5.479,3.982,6.9c1.236,0.713,2.61,1.067,3.986,1.067c1.374,0,2.751-0.354,3.983-1.067l80.704-46.594 c2.466-1.422,3.984-4.054,3.984-6.9C121.023,65.497,119.502,62.866,117.037,61.441z\"/></svg>","data":{"search":{"exclude":true},"embed":{"meta":{"html":"<svg class=\"carousel_pause-icon\" height=\"24px\" id=\"Layer_1\" style=\"enable-background:new 0 0 512 512;\" version=\"1.1\" viewBox=\"0 0 512 512\" width=\"24px\" xml:space=\"preserve\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><g><path d=\"M224,435.8V76.1c0-6.7-5.4-12.1-12.2-12.1h-71.6c-6.8,0-12.2,5.4-12.2,12.1v359.7c0,6.7,5.4,12.2,12.2,12.2h71.6 C218.6,448,224,442.6,224,435.8z\"/><path d=\"M371.8,64h-71.6c-6.7,0-12.2,5.4-12.2,12.1v359.7c0,6.7,5.4,12.2,12.2,12.2h71.6c6.7,0,12.2-5.4,12.2-12.2V76.1 C384,69.4,378.6,64,371.8,64z\"/></g></svg>\n\n<svg class=\"carousel_play-icon\" enable-background=\"new 0 0 139 139\" height=\"24px\" id=\"Play\" version=\"1.1\" viewBox=\"0 0 139 139\" width=\"24px\" xml:space=\"preserve\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><path d=\"M117.037,61.441L36.333,14.846c-2.467-1.424-5.502-1.424-7.972,0c-2.463,1.423-3.982,4.056-3.982,6.903v93.188 c0,2.848,1.522,5.479,3.982,6.9c1.236,0.713,2.61,1.067,3.986,1.067c1.374,0,2.751-0.354,3.983-1.067l80.704-46.594 c2.466-1.422,3.984-4.054,3.984-6.9C121.023,65.497,119.502,62.866,117.037,61.441z\"/></svg>","div":false,"script":false,"compilable":false,"iframe":false},"type":"html"},"insideRTE":false,"devlink":{"runtimeProps":{},"slot":""},"displayName":"","attr":{"id":""},"xattr":[],"visibility":{"conditions":[]}}},{"_id":"12cb124b-8d65-61df-6a3b-7814e34ade85","type":"Block","tag":"div","classes":["349513df-2f6a-dd81-f7f6-bc47182f6ca4"],"children":["708fa8da-0318-963d-a84c-d9c9489d86f9"],"data":{"text":false,"tag":"div","devlink":{"runtimeProps":{},"slot":""},"displayName":"","attr":{"id":""},"xattr":[],"search":{"exclude":false},"visibility":{"conditions":[]}}},{"_id":"708fa8da-0318-963d-a84c-d9c9489d86f9","type":"Block","tag":"div","classes":["bfe1b9a2-2791-3ab5-74e6-7d593e767595","4c326d22-7de2-bb7e-0686-7d2f91019ae5"],"children":[],"data":{"text":false,"tag":"div","devlink":{"runtimeProps":{},"slot":""},"displayName":"","attr":{"id":""},"xattr":[{"name":"s-carousel","value":"dot"}],"search":{"exclude":false},"visibility":{"conditions":[]}}}],"styles":[{"_id":"4c326d22-7de2-bb7e-0686-7d2f91019ae5","fake":false,"type":"class","name":"cc-active","namespace":"","comb":"&","styleLess":"width: 40px;","variants":{},"children":[],"createdBy":"5de3f22e747d41cf4377aef1","origin":null,"selector":null},{"_id":"53efd51e-8f59-597d-e776-234589ff5f4a","fake":false,"type":"class","name":"slider_arrow","namespace":"","comb":"","styleLess":"display: flex; width: 2.5rem; height: 2.5rem; margin-top: -48px; justify-content: center; align-items: center; border-top-left-radius: 50%; border-top-right-radius: 50%; border-bottom-left-radius: 50%; border-bottom-right-radius: 50%; background-color: #00ff84; transition-property: background-color; transition-duration: 200ms; transition-timing-function: ease; color: hsla(0, 0.00%, 11.33%, 1.00); cursor: pointer;","variants":{},"children":["2ac81b08-73b9-1b77-2b0a-db7dc3d73b41","4bb77d6f-0db7-c974-e8f0-3e577b317984"],"createdBy":"5de3f22e747d41cf4377aef1","origin":null,"selector":null},{"_id":"bfe1b9a2-2791-3ab5-74e6-7d593e767595","fake":false,"type":"class","name":"slider_dot","namespace":"","comb":"","styleLess":"position: relative; overflow: hidden; width: 8px; height: 8px; border-top-left-radius: 999px; border-top-right-radius: 999px; border-bottom-left-radius: 999px; border-bottom-right-radius: 999px; background-color: hsla(157, 0.00%, 79.33%, 1.00); transition-property: width, background-color; transition-duration: 200ms, 200ms; transition-timing-function: ease, ease; cursor: pointer;","variants":{},"children":["4c326d22-7de2-bb7e-0686-7d2f91019ae5","37729aa3-904b-f33e-ca2b-a7db6246233d"],"createdBy":"5de3f22e747d41cf4377aef1","origin":null,"selector":null},{"_id":"46abd4df-7bb4-0ba1-63bd-c79a4af4c58b","fake":false,"type":"class","name":"slide","namespace":"","comb":"","styleLess":"display: flex; width: 100%; height: 400px; justify-content: center; align-items: center; flex-grow: 0; flex-shrink: 0; flex-basis: auto; background-color: hsla(169, 3.57%, 30.25%, 1.00); opacity: 0.5; transition-property: transform, opacity; transition-duration: 400ms, 400ms; transition-timing-function: ease, ease;","variants":{},"children":["d73a9728-12a3-a170-d6c4-3fac95e918f5","f99974ad-117a-3914-d51e-90964daad723","983a4d56-d1c1-cad9-1504-545b11af8ae8"],"createdBy":"5de3f22e747d41cf4377aef1","origin":null,"selector":null},{"_id":"d73a9728-12a3-a170-d6c4-3fac95e918f5","fake":false,"type":"class","name":"cc-active","namespace":"","comb":"&","styleLess":"opacity: 1; transform: scale(1);","variants":{},"children":[],"createdBy":"5de3f22e747d41cf4377aef1","origin":null,"selector":null},{"_id":"d6a5c462-70e9-ee0c-6dd0-d87e7e2cc9e7","fake":false,"type":"class","name":"slider_arrows","namespace":"","comb":"","styleLess":"position: absolute; left: 16px; bottom: 16px; display: flex; justify-content: space-between; align-items: center; grid-column-gap: 8px; grid-row-gap: 8px;","variants":{},"children":["77dcd48c-f64b-b1df-b37a-5730c7437c61"],"createdBy":"5de3f22e747d41cf4377aef1","origin":null,"selector":null},{"_id":"fb3f1b2e-88f4-b17a-5517-a33957406f50","fake":false,"type":"class","name":"slider","namespace":"","comb":"","styleLess":"display: flex; justify-content: flex-start; align-items: center; grid-column-gap: 20px; grid-row-gap: 20px;","variants":{},"children":["2cac8212-118d-78c5-4ec5-9bef16322449"],"createdBy":"5de3f22e747d41cf4377aef1","origin":null,"selector":null},{"_id":"349513df-2f6a-dd81-f7f6-bc47182f6ca4","fake":false,"type":"class","name":"slider_dots","namespace":"","comb":"","styleLess":"position: absolute; right: 16px; bottom: 16px; display: flex; grid-column-gap: 4px; grid-row-gap: 4px;","variants":{},"children":["c8dd2525-aeca-35e4-0790-e4d18389172e"],"createdBy":"5de3f22e747d41cf4377aef1","origin":null,"selector":null},{"_id":"20c9e54d-eb64-cdf3-3740-db62ddf605fa","fake":false,"type":"class","name":"slider-wrapper","namespace":"","comb":"","styleLess":"position: relative; overflow: hidden; margin-bottom: 2.5rem;","variants":{},"children":[],"createdBy":"5de3f22e747d41cf4377aef1","origin":null,"selector":null}],"assets":[],"ix1":[],"ix2":{"interactions":[],"events":[],"actionLists":[]}},"meta":{"droppedLinks":0,"dynBindRemovedCount":0,"dynListBindRemovedCount":0,"paginationRemovedCount":0,"universalBindingsRemovedCount":0,"unlinkedSymbolCount":0}}