Skip to main content

Tedshd's Dev note

Website switch layout

Table of Contents

# Website switch layout

之前看過不少網站有切換 layout 的功能 於是就試著做做看

Demo

主要利用定義在 body 上的 class 去做不同 layout 的切換

Sample code

var node;
function node(selector) {
    return document.querySelector(selector);
}

// set style
var layoutList = 'layoutList',
    layoutGrid = 'layoutGrid',
    layoutFull = 'layoutFull',
    layoutCard = 'layoutCard';

function changeLayout(style) {
    // maybe make loading?
    node('body').setAttribute('class', style);
}

// bind click change style
node('#style_1').addEventListener('click', function () {
    changeLayout(layoutGrid);
});
node('#style_2').addEventListener('click', function () {
    changeLayout(layoutList);
});
node('#style_3').addEventListener('click', function () {
    changeLayout(layoutCard);
});
node('#style_4').addEventListener('click', function () {
    changeLayout(layoutFull);
});

/*
scss example

.layoutList {
    .bd {
        .con {
            color: #ff0000;
        }
    }
}
.layoutGrid {
    .bd {
        .con {
            color: #0088ff;
        }
    }
}
.layoutFull {
    .bd {
        .con {
            color: #ff8800;
        }
    }
}
.layoutCard {
    .bd {
        .con {
            color: #00ff00;
        }
    }
}
 */