This mixin lets CSS authors apply styles to all elder siblings of elements matching a CSS selector.
elder(selector, rule)
selector
is a CSS selectorrule
is one or more CSS declarations separated by semicolonsInput:
elder('.demo', 'background: lime;')
Output:
[data-elder-unique="0"] {
background: lime;
}
function elder(selector, rule) {
var tag = document.querySelectorAll(selector)
var style = ''
var count = 0
for (var i=0; i<tag.length; i++) {
var attr = selector.replace(/\W+/g, '')
var sibling = tag[i].parentNode.getElementsByTagName('*')
var tagIndex= [].indexOf.call(sibling, tag[i])
for (var j=0; j<sibling.length; j++) {
var siblingIndex = [].indexOf.call(sibling, sibling[j])
if (siblingIndex < tagIndex) {
sibling[j].setAttribute('data-elder-' + attr, count)
style += '\n[data-elder-' + attr + '="' + count + '"] {\n'
+ ' ' + rule + '\n'
+ '}\n'
count++
} else {
sibling[j].setAttribute('data-elder-' + attr, '')
}
}
}
return style
}