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 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 class="timer__container">
    <div id="timer" class="timer"></div>
</div>
<div class="scores__container wrap clearfix">
    <div class="score__container">
        <p>Score: <span id="score" class="score"></span></p>
    </div>
    <div id="pb-container" class="pb__container">
        <p>Personal best: <span id="pb" class="score"></span></p>
    </div>
</div>
<div class="wrap">
    <h1 class="title">Name that colour!</h1>
    <p class="subtitle">Click the colour, <em>not</em> the word. <a href="https://codepen.io/andrewtanner1987/full/e259f8d33a896f23a4003f66c48bd251/" target="_blank">Full screen view</a> works best.</p>
    <button class="start" id="start">Go!</button>
    <p class="word" id="word"></p>
    <ul id="options" class="options__container"></ul>
    <div id="gameover-container" class="modal__container">
        <div class="modal">
            <p>Game over!</p>
            <button id="restart">Have another go?</button>
        </div>
    </div>
</div>
              
            
!

CSS

              
                @import "bourbon";

body {
    margin: 0;
}

// vars
$red: rgb(236, 111, 134);
$yellow: rgb(255, 221, 117);
$blue: rgb(69, 180, 231);
$green: rgb(178, 240, 104);
$pink: rgb(255, 192, 203);
$sm: 480px;
$md: 768px;
$lg: 1024px;
// mixins
@mixin bp($size) {
    @media screen and (min-width: $size) {
        @content;
    }
}


/* Generated by Font Squirrel (https://www.fontsquirrel.com) on May 15, 2016 */

@font-face {
    font-family: 'komika_axisregular';
    src: url('https://s3-us-west-2.amazonaws.com/s.cdpn.io/212141/komikax_-webfont.woff2') format('woff2'), url('https://s3-us-west-2.amazonaws.com/s.cdpn.io/212141/komikax_-webfont.woff') format('woff');
    font-weight: normal;
    font-style: normal;
}

body {
    background-color: #F9F9F9;
    font-family: 'komika_axisregular', Arial, sans-serif;
}

button {
    font-family: 'komika_axisregular';
}

.clearfix::after {
    content: "";
    display: table;
    clear: both;
}

.wrap {
    margin: 0 auto;
    width: 90%;
    max-width: 1200px;
}

.timer__container {
    height: 5px;
    .timer {
        height: 100%;
        background-color: $red;
    }
}

.scores__container {
    margin-top: 1em;
    > * {
        display: inline-block;
    }
    .score__container {
        float: left;
    }
    .pb__container {
        display: none;
        float: right;
    }
    p {
        color: #8A8A8A;
    }
    .score {
        font-size: 1em;
        @include bp($md) {
            font-size: 2em;
        }
    }
}

.title {
    margin: .5em 0;
    text-align: center;
    font-size: 2em;
    color: #4A4A4A;
    @include bp($md) {
        margin: .25em 0;
        font-size: 3em;
    }
}

.subtitle {
    margin-bottom: 2em;
    text-align: center;
    color: #4A4A4A;
    font-size: .75em;
    @include bp($md) {
        font-size: 1em;
    }
    em {
        padding-bottom: 3px;
        display: inline-block;
        border-bottom: 2px solid #4A4A4A;
    }
    a {
        color: $blue;
        text-decoration: none;
        border-bottom: 2px solid $blue;
    }
}

.start {
    display: block;
    margin: 0 auto;
    padding-bottom: 10px;
    width: 150px;
    height: 150px;
    border-radius: 100%;
    background-color: #EC6F86;
    box-shadow: 0 0 5px 5px lighten(#EC6F86, 10%);
    font-size: 3em;
    color: #F9F9F9;
    border: 0;
    cursor: pointer;
    animation: glow 1s infinite;
}

.word {
    font-size: 4em;
    text-align: center;
    @include bp($sm) {
        display: inline-block;
        width: 50%;
    }
    @include bp($md) {
        display: block;
        width: auto;
    }
}

.options__container {
    margin: 3em 0;
    @include bp($sm) {
        display: inline-block;
        margin: 0;
        width: 50%;
        float: right;
    }
    @include bp($md) {
        display: block;
        margin: 3em 0;
        width: auto;
        float: none;
    }
    li {
        margin-bottom: .5em;
        text-align: center;
        @inluce bp($sm) {
            margin-bottom: 0;
        }
    }
    button {
        position: relative;
        background-color: transparent;
        border: 0;
        font-size: 1em;
        color: #8A8A8A;
        cursor: pointer;
        @include bp($md) {
            font-size: 2em;
            line-height: 1.25em;
        }
    }
}

.modal__container {
    display: none;
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 1;
    background-color: rgba(0, 0, 0, .75);
    .modal {
        position: absolute;
        top: 50%;
        left: 50%;
        width: 90%;
        padding: 25px 0;
        @include transform(translate(-50%, -50%));
        background-color: #fff;
        border-radius: 5px;
        @include bp($sm) {
            width: 450px;
        }
    }
    p {
        color: $red;
        font-size: 2em;
        text-align: center;
        @include bp($md) {
            font-size: 3em;
        }
    }
    button {
        margin: 50px auto 0;
        padding: 1em;
        border: 3px solid $blue;
        border-radius: 10px;
        background-color: #fff;
        color: $blue;
        line-height: 1;
        cursor: pointer;
        transition: all .25s;
        @include bp($md) {
            font-size: 1.25em;
        }
        &:hover {
            background-color: $blue;
            color: #ffffff;
        }
    }
}

#restart {
    display: none;
}

@keyframes glow {
    50% {
        box-shadow: 0 0 10px 10px lighten(#EC6F86, 10%);
    }
}
              
            
!

JS

              
                "use strict";

var wg = function(difficulty) {
    var names = ['Red',
            'Yellow',
            'Blue',
            'Green',
            'Purple'
        ],

        colours = ['rgb(236, 111, 134)',
            'rgb(255, 221, 117)',
            'rgb(69, 180, 231)',
            'rgb(178, 240, 104)',
            'rgb(167, 111, 236)'
        ],

        timeout,
        timer;

    var domCache = {
        startGame: document.getElementById('start'),
        wordContainer: document.getElementById('word'),
        optionsContainer: document.getElementById('options'),
        scoreContainer: document.getElementById('score'),
        timer: document.getElementById('timer'),
        gameoverContainer: document.getElementById('gameover-container'),
        restart: document.getElementById('restart'),
        pbContainer: document.getElementById('pb-container'),
        pb: document.getElementById('pb')
    };

    var methods = {
        pbCookie: function() {
            var cookieValue = document.cookie.replace(/(?:(?:^|.*;\s*)wg-pb\s*\=\s*([^;]*).*$)|^.*$/, "$1");
            if (!cookieValue) {
                domCache.pb.textContent = 0;
            } else {
                domCache.pbContainer.style.display = 'block';
                domCache.pb.textContent = cookieValue;
            }
                
            return cookieValue;
        },

        rand: function() {
            var i = (Math.round(Math.random() * (names.length - 1)));

            return i;
        },

        pick: function(ar) {
            var picked = ar[methods.rand()];

            return picked;
        },

        printWord: function() {
            domCache.wordContainer.textContent = methods.pick(names);
            domCache.wordContainer.style.color = methods.pick(colours);
        },

        timeout: function() {
            clearTimeout(timeout);

            timeout = setTimeout(function() {
                methods.gameOver();
            }, difficulty);
        },

        timer: function() {
            clearInterval(timer);
            var w = 100

            timer = window.setInterval(function() {
                domCache.timer.style.width = ((w - 1) - 1) + '%';
                w--;
            }, (difficulty / 100));
        },

        printOptions: function() {
            for (var i = 0; i < names.length; i++) {
                domCache.optionsContainer.innerHTML += '<li><button class="option" data-colour="' + colours[i] + '">' + names[i] + '</button></li>';
            }
        },

        getOptions: function() {
            var options = document.querySelectorAll('.option');

            return options;
        },

        clickListeners: function() {
            for (var i = 0; i < names.length; i++) {
                methods.getOptions()[i].addEventListener('click', function(e) {
                    methods.answerAttempt(e);
                });
            };
        },

        answerAttempt: function(e) {
            var attempt = e.target.getAttribute('data-colour');

            if (attempt != domCache.wordContainer.style.color) {
                methods.gameOver();
            } else {
                methods.updateScore();
                methods.printWord();
                methods.timer();
                methods.timeout();
            }
        },

        gameOver: function() {
            domCache.gameoverContainer.style.display = 'block';
            domCache.restart.style.display = 'block';
            methods.restart();
        },

        restart: function() {
            domCache.restart.addEventListener('click', function() {
                domCache.gameoverContainer.style.display = 'none';
                domCache.restart.style.display = 'none';
                methods.updateScore(0);
                methods.printWord();
                methods.timer();
                methods.timeout();
            });
        },

        initScore: function() {
            domCache.scoreContainer.textContent = 0;
        },

        updateScore: function(sc) {
            if (sc == false) {
                domCache.scoreContainer.textContent = 0;
            } else {
                var currentScore = parseInt(domCache.scoreContainer.textContent);
                var newScore = currentScore + 1;

                domCache.scoreContainer.textContent = newScore;

                if (newScore > methods.pbCookie()) {
                    document.cookie = 'wg-pb=' + newScore;
                }

                domCache.pb.textContent = methods.pbCookie();
            }
        },

        startGame: function() {
            domCache.startGame.addEventListener('click', function() {
                methods.printWord();
                methods.printOptions();
                methods.timer();
                methods.timeout();
                methods.clickListeners();
                domCache.startGame.style.display = 'none';
            });
        }
    };

    var init = function() {
        methods.initScore();
        methods.pbCookie();
        methods.startGame();
    };

    return {
        init: init()
    }
}

wg(1750);
              
            
!
999px

Console