CSSスタイルの差分を表示メモ

投稿者: ytyng 5年, 2ヶ月 前

CSSスタイルの差分を表示

<script>
var styleSnapshots = [];
var classNames = {};

var styleParams = [
'height',
'width',
'margin',
'padding',
'position',
'top',
'bottom',
'lineHeight',
];

function getStyleData(element){
var data = {};
var cs = window.getComputedStyle(element);
// console.log(cs);
for(var param of styleParams){
data[param] = cs[param];
}
return data;
}


function snapshot(snapshotIndex, path, element) {
if (styleSnapshots.length <= snapshotIndex) {
styleSnapshots[snapshotIndex] = {};
}
classNames[path] = element.getAttribute('class');

styleSnapshots[snapshotIndex][path] = getStyleData(element);
var children = element.children;
for (var i = 0; i < children.length; i++) {
var child = children[i];
var tagName = child.tagName;
var path2 = `${path}${tagName}[${i}]/`;

snapshot(snapshotIndex, path2, child);

}
}

function diffSnapshot(i0, i1) {
for (var path in styleSnapshots[i1]) {
if (!styleSnapshots[i0][path]) {
console.log(`${path} before undefined`);
continue;
}
if (!styleSnapshots[i1][path]) {
console.log(`${path} after undefined`);
continue;

}
var className = classNames[path];


for (var sp of styleParams) {


if (styleSnapshots[i0][path][sp] != styleSnapshots[i1][path][sp]) {
console.log(`${path} ${className} ${sp} ${styleSnapshots[i0][path][sp]} -> ${styleSnapshots[i1][path][sp]}`);
}
}
}

}

snapshot(0, '/', document.querySelector('.navlist'));

setTimeout(function () {
snapshot(1, '/', document.querySelector('.navlist'));

diffSnapshot(0, 1);

}, 3000);
</script>
現在未評価

コメント

アーカイブ

2024
2023
2022
2021
2020
2019
2018
2017
2016
2015
2014
2013
2012
2011