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. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

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 class="container">
    <div class="col-xs-5 materialSelect inline empty">
        <ul class="select">
            <li data-selected="true">Choose something...</li>
            <li data-value="0">First option</li>
            <li data-value="1">Second option</li>
            <li data-value="2">Third option</li>
        </ul>
        <div class="message">Please select something</div>
    </div>
    <div class="col-xs-5 materialSelect inline empty">
        <ul class="select">
            <li data-selected="true">Choose something...</li>
            <li data-value="0">First option</li>
            <li data-value="1">Second option</li>
            <li data-value="2">Third option</li>
        </ul>
        <div class="message">Please select something</div>
    </div>
</div>
              
            
!

CSS

              
                $base: #303f9f;
$accent: #E91E63;
$error: #DD2C00;
body{
    min-width: calc(100vw - 20px);
    min-height: 100vh;
}
.container{
    margin-top: 150px;
}
.drop {
    display: block;
    position: absolute;
    background: #CCC;
    border-radius: 100%;
    transform: scale(0);
    pointer-events: none;
    width: 100%;
    height: 100%;
    
    &.animate {
        animation: drop 1s ease-out;
    }
}

@keyframes drop {
    100% {
        opacity: 0;
        transform: scale(2.5);
    }
}

.materialSelect{
    height: 70px;
    position: relative;
    text-align: center;
    margin-bottom: 10px;
    
    &.error {
        .select:not(.isOpen){
            border: 1px solid $error !important;
        }
        .message {
            display: block;
        }
    }
    &.inline{
        float: none !important;
        display: inline-block;
        z-index: 0;
        min-width: 100px;

        .select{
            transform: translate(0, 0);
            top: 0;
            left: 0;
            margin: 0;
            transition: all 0.2s !important;
            width: 100%;

            &.isOpen{
                transform: translate(0, -50%);
                top: 50%;
            }
        }
    }
    &:not(.inline){
        width: 100%;

        .select{
            width: 100%;
            margin-left: 50%;
            transform: translate(-50%, 0);
        }
    }
    .select {
        position: absolute;
        margin: 0;
        padding: 0;
        top: -1px;
        user-select: none;
        width: 250px;
        text-align: center;
        margin: 0px auto;
        z-index: 9999;
        height: 48px;
        overflow: hidden;
        border: 1px solid rgba(0, 0, 0, 0); 
        box-shadow: 0 0 0 0 rgba(0, 0, 0, 0);
        background-color: #fff;
        margin-left: 0;
        transform: none;
        
        &:after {
            border-left: 5px solid transparent;
            border-right: 5px solid transparent;
            border-top: 5px solid #bbbbbb;
            content: '';
            display: block;
            height: 0px;
            position: absolute;
            pointer-events: none;
            top: 19px;
            right: 10px;
            width: 0px;
        }
        li {
            cursor: pointer;
            font-size: 15px;
            list-style: none;
            line-height: 48px;
            padding: 0 48px 0 24px;
            position: relative;
            overflow: hidden;
            
            &[data-selected] {
                height: 48px;
            }
            &:not([data-selected]) {
                height: 0px;
                opacity: 0;
            }
        }
        &.isOpen {
            background-color: #fafafa;
            border-radius: 2px;
            box-shadow: 1px 2px 3px 1px rgba(0, 0, 0, 0.3);
            padding-bottom: 16px;
            top: -96px;
            height: 250px;
            z-index: 99999;
            
            &:after {
                display: none;
            }
            &:before {
                border-color: transparent !important;
            }
            li {
                height: 48px;
                opacity: 1;
                
                &[data-selected] {
                    color: $accent;
                }
                &:hover {
                    background-color: #eeeeee;
                }
                &:active {
                    background-color: #dbdbdb;
                }
            }
        }
        &:not(.isOpen) {
            &:hover {
                background-color: #f7f7f7;
                border-top: 1px solid #CDCDCD;
                border-bottom: 1px solid #CDCDCD;
            }
            &:active{
                box-shadow: 1px 2px 1px 0 rgba(0, 0, 0, 0.3);
            }
        }
        + .select:before {
            border-left: thin solid #c8c8c8;
            content: '';
            height: 32px;
            left: 0;
            position: absolute;
            top: 8px;
        }
    }
    .message{
        position: absolute;
        top: 50px;
        width: 100%;
        color: $error;
        display: none;
    }
}

@media only screen and (min-width: 1008px){
    .materialSelect{
        .select{
            transition: background 0.2s ease, border 0.2s ease, top 0.2s ease, height 0.2s ease, box-shadow 0.2s ease;
            
            li{
                transition: height 0.2s linear, opacity 0.2s ease, margin 0.2s linear;
            }
        }
    }
}
              
            
!

JS

              
                $(function(){
    var closeSelectTimeout;

    function hideMaterialList(parent){
        parent.css({
            'overflow': 'hidden'
        }).removeClass('isOpen');
        clearTimeout(closeSelectTimeout);
        closeSelectTimeout = setTimeout(function(){
            parent.parent().css({
                'z-index': 0  
            });
        }, 200);
    }
    $(document.body).on('mousedown', '.materialBtn, .select li', function(event){
        if(parseFloat($(this).css('opacity')) > 0 && $(document).width() >= 1008){
            var maxWidthHeight = Math.max($(this).width(), $(this).height());
            if($(this).find("b.drop").length == 0 || $(this).find("b.drop").css('opacity') != 1) {
                // .drop opacity is 1 when it's hidden...css animations
                drop = $('<b class="drop" style="width:'+ maxWidthHeight +'px;height:'+ maxWidthHeight +'px;"></b>').prependTo(this);
            }
            else{
                $(this).find("b.drop").each(function(){
                    if($(this).css('opacity') == 1){
                        drop = $(this).removeClass("animate");
                        return;
                    }
                })
            }
            x = event.pageX - drop.width()/2 - $(this).offset().left;
            y = event.pageY - drop.height()/2 - $(this).offset().top;
            drop.css({
                top: y,
                left: x
            }).addClass("animate");
        }
    });
    $(document.body).on('dragstart', '.materialBtn, .select li', function(e){
        e.preventDefault();
    })

    var selectTimeout;
    $(document.body).on('click', '.select li', function() {
        var parent = $(this).parent();
        parent.children('li').removeAttr('data-selected');
        $(this).attr('data-selected', 'true');
        clearTimeout(selectTimeout);
        if(parent.hasClass('isOpen')){
            if(parent.parent().hasClass('required')){
                if(parent.children('[data-selected]').attr('data-value')){
                    parent.parents('.materialSelect').removeClass('error empty');
                }
                else{
                    parent.parents('.materialSelect').addClass('error empty');
                }
            }
            hideMaterialList($('.select'));
        }
        else{
            var pos = Math.max(($('li[data-selected]', parent).index() - 2) * 48, 0);
            parent.addClass('isOpen');
            parent.parent().css('z-index', '999');
            if($(document).width() >= 1008){
                var i = 1;
                selectTimeout = setInterval(function(){
                    i++;
                    parent.scrollTo(pos, 50);
                    if(i == 2){
                        parent.css('overflow', 'auto');
                    }
                    if(i >= 4){
                        clearTimeout(selectTimeout);
                    }
                }, 100);
            }
            else{
                parent.css('overflow', 'auto').scrollTo(pos, 0);
            }
        }
    });

    $('.materialInput input').on('change input verify', function(){
        if($(this).attr('required') == 'true'){
            if($(this).val().trim().length){
                $(this).parent().removeClass('error empty');
            }
            else{
                $(this).parent().addClass('error empty');
                $(this).val('');
            }
        }
        else{
            if($(this).val().trim().length){
                $(this).parent().removeClass('empty');
            }
            else{
                $(this).parent().addClass('empty');
            }
        }
    });

    $(document.body).on('click', function(e) {
        var clicked;
        if($(e.target).hasClass('materialSelect')){
            clicked = $(e.target).find('.select').first();
        }
        else if($(e.target).hasClass('select')){
            clicked = $(e.target);
        }
        else if($(e.target).parent().hasClass('select')){
            clicked = $(e.target).parent();
        }

        if($(e.target).hasClass('materialSelect') || $(e.target).hasClass('select') || $(e.target).parent().hasClass('select')){
            hideMaterialList($('.select').not(clicked));
        }
        else{
            if($('.select').hasClass('isOpen')){
                hideMaterialList($('.select'));
            }
        }
    });
    hideMaterialList($('.select'));
})
              
            
!
999px

Console