Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="root"></div>
              
            
!

CSS

              
                $slide-count: 5;
$slide-width: 30rem;


html {
  box-sizing: border-box;
  font-size: 10px;
  @media (max-width: 425px) {
    font-size: 8px;
  }
}

body {
  font-size: 1.5rem;
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
}

*,
*:before,
*:after {
  -webkit-box-sizing: border-box;
  box-sizing: inherit;
}

ul {
  list-style-type: none;
  padding: 0;
}

a {
  text-decoration: none;
  color: inherit;
}

.carousel__wrap {
    margin-top: 10%;
    align-items: center;
    display: flex;
    flex: 1;
    justify-content: center;
    position: relative;
    width: 80%;
    left: 50%;
    transform: translateX(-50%);
}

.carousel__inner {
    height: 40rem;
    position: relative;
    width: calc(#{$slide-width * 3});
}

.carousel__container {
    height: 100%;
    overflow: hidden;
    position: relative;
    width: 100%;
}

.carousel__slide-list {
    height: 100%;
    left: 50%;
    list-style-type: none;
    margin: 0;
    padding: 0;
    position: absolute;
    transform: translateX(-50%);
    width: calc(#{($slide-count + 0.5) * $slide-width * 2});
}

.carousel__slide-item {
    display: inline-block;
    height: $slide-width;
    margin: 0;
    padding: 1rem;
    position: absolute;
    transition: all 0.3s;
    width: $slide-width;
}

.carousel__slide-item-img-link {
    // cursor: zoom-in;
    display: flex;
    height: 85%;
    // overflow: hidden;
    position: relative;
    width: 90%;
    margin-top:20px;

    img {
        height: 85%;
        object-fit: cover;
        transition: all 0.5s ease;
        width: 90%;
    }

    &:hover {
        &::after {
            opacity: 1;
        }

        img {
            transform: scale(1.2);
        }
    }
}

.carousel-slide-item__body {
    bottom: -2.5rem;
    height: 10%;
    position: absolute;

    h4 {
        margin: 0.7rem 0 0;
        text-transform: uppercase;
        font-size: 1.6rem;
    }

    p {
        font-size: 1.6rem;
        line-height: 1.3;
        margin: 0.7rem 0 0;
    }
}

.carousel__btn {
    align-items: center;
    background: 0;
    border: 0;
    cursor: pointer;
    display: flex;
    justify-content: center;
    position: absolute;
    top: 50%;
    transform: translateY(-50%);

    &--prev {
        left: -10rem;
    }

    &--next {
        right: -10rem;
    }
}

.carousel__btn-arrow {
    border: solid black;
    border-width: 0 0.4rem 0.4rem 0;
    height: 5rem;
    padding: 3px;
    width: 5rem;
    z-index: 10;

    &--left {
        transform: rotate(135deg);
    }

    &--right {
        transform: rotate(-45deg);
    }
}

.carousel__dots {
    display: inline-block;
    left: 50%;
    margin-top: 2rem;
    position: absolute;
    transform: translateX(-50%);

    .dot {
        background: #ccc;
        border: 0;
        border-radius: 50%;
        cursor: pointer;
        height: 2rem;
        margin: 0 0.3rem;
        outline: none;
        transform: scale(0.5);
        width: 2rem;

        &.active {
            background: black;
        }
    }
}

              
            
!

JS

              
                const _items = [
  {
        item: {
            name: 'OnePlus-Mirror-Black',
            price:' 45,999.00 Rs',
            image: 'https://www.itl.cat/pngfile/big/15-153953_how-to-find-the-best-wallpapers-for-android.jpg',
            category:'Mobile'
        }
    },
  {
        item: {
            name: 'Townsman Leather Watch',
            price:'5678.00 Rs',
            image: 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRwyiSsuTNAYnQNXeJSoPeTA_de8W9ykQDDmg&usqp=CAU',
            category:'Watch'
        }
    },
    {
        item: {
            name: 'Swiss-Luxury-Watch',
            price:'7595.00 Rs',
            image: 'https://cdn3.boutique.humbleandrich.com/wp-content/uploads/2018/06/09222629/Most-Respected-Swiss-Luxury-Watch-Brands.jpg',
            category:'Watch'
        }
    },
    {
        item: {
            name: 'Apple-iPhone-11-64GB',
            price:' 51,999.00 Rs',
            image: 'https://images.unsplash.com/photo-1595941069915-4ebc5197c14a?ixid=MXwxMjA3fDB8MHxzZWFyY2h8MTF8fHNtYXJ0JTIwcGhvbmV8ZW58MHx8MHw%3D&ixlib=rb-1.2.1&w=1000&q=80',
            category:'Mobile'
        }
    },
    {
         item: {
             name: 'Bovet-1822-Récital Astérium',
            price:'9867.00 Rs',
            image: 'https://2x1dks3q6aoj44bz1r1tr92f-wpengine.netdna-ssl.com/wp-content/uploads/2019/05/Bovet-1822-R%C3%A9cital-20-Ast%C3%A9rium-Watch.jpg',
            category:'Watch'
        }
    },
  
]

const length = _items.length
_items.push(..._items)
const slideWidth = 30;

const sleep = (ms = 0) => {
    return new Promise(resolve => setTimeout(resolve, ms))
}

const createItem = (position, idx, activeIdx) => {
    const item = {
        styles: {
            transform: `translateX(${position * slideWidth}rem)`
        },
        item: _items[idx].item
    }

    switch (position) {
        case length - 1:
        case length + 1:
            item.styles = { ...item.styles, filter: 'grayscale(1)' }
            break
        case length:
            item.styles = { ...item.styles  }
            break
        default:
            item.styles = { ...item.styles, opacity: 0 }
            break
    }

    return item
}

const getStyle = (position, idx, activeIdx) => {
    const item = {
        styles: { 
        },
    }

    switch (position) {
        case length - 1:
        case length + 1:
            item.styles = { ...item.styles }
            break
        case length:
            item.styles = { ...item.styles,  height: '110%', width:'100%', marginTop:0  }
            break
        default:
            item.styles = { ...item.styles }
            break
    }

    return item
}

const CarouselSlideItem = ({ pos, idx, activeIdx }) => {
    const item = createItem(pos, idx, activeIdx)
    const imgStyle = getStyle(pos, idx, activeIdx)

    return (
        <li className='carousel__slide-item' style={item.styles}>
            <div className='carousel__slide-item-img-link' style={imgStyle.styles}>
                <img src={item.item.image} alt={item.item.title} />
            </div>
            <div className='carousel-slide-item__body'>
                <h4>{item.item.name}</h4>
                <p><b>{item.item.price} </b></p>
                <p><b>{item.item.category}</b></p>
            </div>
        </li>
    )
}

const keys = Array.from(Array(_items.length).keys())

const Carousel = () => {
    const [items, setItems] = React.useState(keys)
    const [isTicking, setIsTicking] = React.useState(false)
    const [activeIdx, setActiveIdx] = React.useState(0)
    const bigLength = items.length

    const prevClick = (jump = 1) => {
        if (!isTicking) {
            setIsTicking(true)
            setItems(prev => {
                return prev.map((_, i) => prev[(i + jump) % bigLength])
            })
        }
    }

    const nextClick = (jump = 1) => {
        if (!isTicking) {
            setIsTicking(true)
            setItems(prev => {
                return prev.map(
                    (_, i) => prev[(i - jump + bigLength) % bigLength]
                )
            })
        }
    }

    const handleDotClick = idx => {
        if (idx < activeIdx) prevClick(activeIdx - idx)
        if (idx > activeIdx) nextClick(idx - activeIdx)
    }

    React.useEffect(() => {
        if (isTicking) sleep(300).then(() => setIsTicking(false))
    }, [isTicking])

    React.useEffect(() => {
        setActiveIdx((length - (items[0] % length)) % length) 
    }, [items])

    return (
        <div className='carousel__wrap'>
            <div className='carousel__inner'>
                <button
                    className='carousel__btn carousel__btn--prev'
                    onClick={() => prevClick()}>
                    <i className='carousel__btn-arrow carousel__btn-arrow--left' />
                </button>
                <div className='carousel__container'>
                    <ul className='carousel__slide-list'>
                        {items.map((pos, i) => (
                            <CarouselSlideItem
                                key={i}
                                idx={i}
                                pos={pos}
                                activeIdx={activeIdx}
                            />
                        ))}
                    </ul>
                </div>
                <button
                    className='carousel__btn carousel__btn--next'
                    onClick={() => nextClick()}>
                    <i className='carousel__btn-arrow carousel__btn-arrow--right' />
                </button>
                <div className='carousel__dots'>
                    {items.slice(0, length).map((pos, i) => (
                        <button
                            key={i}
                            onClick={() => handleDotClick(i)}
                            className={i === activeIdx ? 'dot active' : 'dot'}
                        />
                    ))}
                </div>
            </div>
        </div>
    )
}

ReactDOM.render(<Carousel />, document.getElementById("root"));

              
            
!
999px

Console