Displaying Differences in CSS Styles: A Memo

css
2019-02-10 15:23 (6 years ago) ytyng

Displaying CSS Style Differences

<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>
Currently unrated
The author runs the application development company Cyberneura.
We look forward to discussing your development needs.

Archive

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