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

              
                <ul>
    <li>1</li>
    <li>2</li>
    <li>3</li>
    <li>4</li>
    <li>5</li>
    <li>6</li>
    <li>7</li>
    <li>8</li>
    <li>9</li>
</ul>
<hr>
<button id="shift">シフト</button>
<button id="shuffle">シャッフル</button>
<button id="reset">リセット</button>
              
            
!

CSS

              
                ul {
    display: flex;
    flex-wrap: wrap;
    margin: 0;
    padding: 0;
    width: 330px;
}

li {
    list-style: none;
    border: 1px solid #888;
    margin: 4px;
    width: 100px;
    height: 100px;
    line-height: 100px;
    user-select: none;
    text-align: center;
    transition: all .5s;
}

button {
    width: 6rem;
}

              
            
!

JS

              
                function flip(elem, callback) {

    // 要素の移動前の位置をマップに保存
    const map = new Map();
    for (const el of elem.querySelectorAll('li')) {
        const r = el.getBoundingClientRect();
        map.set(el, r);
    }

    // コールバックで要素を移動させる
    callback();

    // 移動後の位置を確定させる
    requestAnimationFrame(() => {
        for (const [el, r1] of map) {

            // 移動後の要素の位置を得る
            const r2 = el.getBoundingClientRect();

            // 要素の移動距離を反転して transform に適用する
            const x = r1.left - r2.left;
            const y = r1.top - r2.top;
            el.style.transitionProperty = 'none';
            el.style.transform = `translate(${x}px,${y}px)`;

            requestAnimationFrame(() => {
                // 要素の transform を解除して本来の位置に戻す
                el.style.transitionProperty = '';
                el.style.transform = '';
            });
        }
    });
}

document.querySelector('#shift').addEventListener('click', () => {
    const elem = document.querySelector('ul');
    flip(elem, () => {
        // 終端の要素を先頭に移動
        const arr = Array.from(elem.querySelectorAll('li'));
        arr.unshift(arr.pop());
        elem.append(...arr);
    });
});

document.querySelector('#shuffle').addEventListener('click', () => {
    const elem = document.querySelector('ul');
    flip(elem, () => {
        // シャッフル
        const arr = Array.from(elem.querySelectorAll('li'));
        for (let i = arr.length - 1; i > 0; i--) {
            const r = Math.floor(Math.random() * (i + 1));
            [ arr[i], arr[r] ] = [ arr[r], arr[i] ];
        }
        elem.append(...arr);
    });
});

document.querySelector('#reset').addEventListener('click', () => {
    const elem = document.querySelector('ul');
    flip(elem, () => {
        // 順番に整列
        const arr = Array.from(elem.querySelectorAll('li')).sort((a, b) => {
            if (+a.textContent < +b.textContent) {
                return -1;
            } else if (+a.textContent < +b.textContent) {
                return +1;
            } else {
                return 0;
            }
        });
        elem.append(...arr);
    });
});

              
            
!
999px

Console