cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

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.

            
              <div class="site-wrap">
    <h1>Block Slider</h1>
    Repsitory: <a href="https://github.com/rbrtsmith/BlockSlider">https://github.com/rbrtsmith/BlockSlider</a>
    <p>Reduce the width of the viewport to see the # of visible items reduce!</p>
</div>
<div class="site-wrap">
    <div id="block-slider">
        <div class="block-slider js-block-slider__wrap">
            <div class="block-slider__items js-block-slider__items">
                <div class="block-slider__item">
                    <div class="block-slider__image">
                        Image-1
                    </div>
                </div>
                <div class="block-slider__item">
                    <div class="block-slider__image">
                        Image-2
                    </div>
                </div>
                <div class="block-slider__item">
                    <div class="block-slider__image">
                        Image-3
                    </div>
                </div>
                <div class="block-slider__item">
                    <div class="block-slider__image">
                        Image-4
                    </div>
                </div>
                <div class="block-slider__item">
                    <div class="block-slider__image">
                        Image-5
                    </div>
                </div>
                <div class="block-slider__item">
                    <div class="block-slider__image">
                        Image-6
                    </div>
                </div>
                <div class="block-slider__item">
                    <div class="block-slider__image">
                        Image-7
                    </div>
                </div>
                <div class="block-slider__item">
                    <div class="block-slider__image">
                        Image-8
                    </div>
                </div>
                <div class="block-slider__item">
                    <div class="block-slider__image">
                        Image-9
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
            
          
!
            
              $spacing: .5em;
$brand-color: #45d296;

@function number-visible-items($num) {
  @return #{(100 / $num) + '%'};
}

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

a {
    color: $brand-color;
}

.site-wrap {
  max-width: 1000px;
  margin: 0 auto;
  padding: ($spacing * 2) $spacing;
}

.block-slider {
  line-height: 100px;
  
  &__items {
    margin: 0 -$spacing;
    
    &::after {
      display: table;
      clear: both;
      content: " ";
    }
  }
  
  &__item {
    width: number-visible-items(2);
    float: left;
    padding: 0 $spacing;
    @media(min-width: 500px) {
      width: number-visible-items(3);
    }
    
    @media(min-width: 700px) {
      width: number-visible-items(4);
    }
    
    @media(min-width: 800px) {
      width: number-visible-items(5);
    }
    
    @media(min-width: 900px) {
      width: number-visible-items(6);
    }
    @media(min-width: 1000px) {
      width: number-visible-items(7);
    }
  }
  
  &__image {
    padding: 0 $spacing;
    background: $brand-color;
    color: white;
    text-align: center;
  }
}
            
          
!
            
              function BlockSlider(collection, {
    sInterval = 2000,
    sTransition = 400,
    wrapClass = '.js-block-slider__wrap',
    itemsClass = '.js-block-slider__items'
}) {
    'use strict';
    const safariFixDelay = /constructor/i.test(window.HTMLElement) ? 500 : 0,
        sliders = [],
        lgStart = 'Block Slider: ';
    let resizeTimer;

    function testTiming(timing) {
        if (typeof timing !== 'number') {
            console.warn(`${lgStart}Supplied timing: '${timing}' is not an integer`);
            return false;
        }
        return true;
    }
    if (! testTiming(sInterval)) {
        sInterval = 2000;
    }
    if (! testTiming(sTransition)) {
        sTransition = 400;
    }
    
    try {
        // can collection be found ?
        if (!collection || !collection.length && !collection.querySelector) {
            throw new Error(`${lgStart}Supplied slider(s) could not be found.`);
        }
    }
    catch(e) {
        console.warn(e.message);
        return;
    }

    const calcTransiton = () => sTransition / 1000;

    function cache(slider) {
        let wrap,
            items,
            duration,
            sliderName = slider.id ? '#' + slider.id : '.' + slider.className,
            msg2 = ' could not be found as a direct descendant of ';
        try {
            wrap = slider.querySelector(wrapClass);
            if (!wrap) {
                throw new Error(`${lgStart}'${wrapClass}'${msg2}'${sliderName}.'`);
            } 
            else {
                items = wrap.querySelector(itemsClass);
                if (!items) {
                    throw new Error(`${lgStart}'${itemsClass}'${msg2}'${wrapClass}.'`);
                } 
                else {
                    items = [].slice.call(items.children, 0);                    
                }
            }
        } 
        catch(e) {
            console.warn(e.message);
            return false;
        }
        duration = calcTransiton();
        slider.setAttribute('style', 'overflow: hidden; opacity: 0');
        wrap.setAttribute('style', `-webkit-transition: -webkit-transform ${duration}s;transition: transform ${duration}s;`);
        sliders.push({
            slider,
            wrap,
            items,
            slidePosition: 0,
            itemsPerSlide: 0,
            itemWidth: 0,
            wrapWidth: 0,
            sliderWidth: 0
        });
    }
    if (collection.length) {
        [].forEach.call(collection, cache);
    } else {
        cache(collection);
    }

    const hasReachedEnd = s => -(s.slidePosition + 1) > s.wrapWidth - s.sliderWidth;

    function slide() {
        sliders.forEach(s => {
            const wrap = s.wrap,
                str = `translate3d(${s.slidePosition}px, 0, 0)`;
            wrap.style.webkitTransform = str;
            wrap.style.msTransform = str;
            wrap.style.transform = str;
            s.slidePosition -= s.itemWidth;
            if (hasReachedEnd(s)) {
                s.slidePosition = 0;
            }
        });
    }

    function calcAndSetWidths() {
        function removeInlineWidths(s) {
            // Temporarily hide the slider
            s.slider.style.opacity = 0;
            // Remove widths for wrap and all items.
            s.wrap.style.removeProperty('width');
            s.items.forEach((item, i, arr) => {
                item.style.removeProperty('width');
            });
        }

        const calcItemWidth = s => s.items[0].offsetWidth;

        const calcItemsPerSlide = s => Math.round(s.sliderWidth / s.itemWidth);

        function setWidths(s) {
            // loop through all items and set their width in pixels
            // to the calculated width.
            s.items.forEach((item) => {
                item.style.width = s.itemWidth + 'px';
            });
            // Set wrap width equal to total width of items.
            s.wrapWidth = s.itemWidth * s.items.length;
            s.wrap.style.width = s.wrapWidth + 'px';
            s.sliderWidth = s.slider.offsetWidth;
        }

        sliders.forEach(s => {
            removeInlineWidths(s);
            // create a slight delay for Safari being rubbishly slow at layout calcs.
            setTimeout(() => {
                // get the default CSS item width in pixels.
                s.itemWidth = calcItemWidth(s);
                setWidths(s);
                s.itemsPerSlide = calcItemsPerSlide(s);
                s.slidePosition = 0;
                setTimeout(() => {
                    // fade slider back in.
                    s.slider.style.opacity = 1;
                    slide();
                }, safariFixDelay);
            }, safariFixDelay);
        });
    }

    calcAndSetWidths();

    window.addEventListener('resize', () => {
        // call setWidths once the screen has finished resizing.
        clearTimeout(resizeTimer);
        resizeTimer = setTimeout(calcAndSetWidths, 250);
    });

    setInterval(() => {
        if (requestAnimationFrame) {
            requestAnimationFrame(() => {
                slide(sliders)
            });            
        } else {
            slide(sliders); 
        }
    }, sInterval);
}

// if jQuery is present, create a plugin.
if (window.jQuery) {
    (($) => {
        $.fn.BlockSlider = function(opts) {
            BlockSlider(this, opts);
            return this;
        };
    })(window.jQuery);
}


BlockSlider(document.getElementById('block-slider'), {
    sInterval: 2400,
    sTransition: 700,
});
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console