Hide anchors until mouseover

This commit is contained in:
Rico Sta. Cruz 2018-07-09 10:27:10 +08:00
parent 8af730ebb3
commit a66ea66c80
No known key found for this signature in database
GPG Key ID: CAAD38AE2962619A
4 changed files with 15 additions and 6 deletions

View File

@ -5,7 +5,7 @@ const DEFAULTS = {
// select elements to put anchor on // select elements to put anchor on
rule: 'h2[id]', rule: 'h2[id]',
// class name for anchor // class name for anchor
cls: 'local-anchor', className: 'local-anchor anchor',
// text of anchor // text of anchor
text: '#', text: '#',
// append before or after innerText? // append before or after innerText?
@ -18,11 +18,11 @@ const DEFAULTS = {
onmount('[data-js-anchors]', function () { onmount('[data-js-anchors]', function () {
const data = JSON.parse(this.getAttribute('data-js-anchors') || '{}') const data = JSON.parse(this.getAttribute('data-js-anchors') || '{}')
const rules = Array.isArray(data) const rules = Array.isArray(data)
? (data.length ? data : [DEFAULTS]) ? (data.length ? data : [DEFAULTS])
: [Object.assign({}, DEFAULTS, data)] : [Object.assign({}, DEFAULTS, data)]
for (const { rule, cls, text, shouldAppend } of rules) { for (const { rule, className, text, shouldAppend } of rules) {
for (const el of this.querySelectorAll(rule)) { for (const el of this.querySelectorAll(rule)) {
if (!el.hasAttribute('id')) { if (!el.hasAttribute('id')) {
continue continue
@ -31,7 +31,7 @@ onmount('[data-js-anchors]', function () {
const id = el.getAttribute('id') const id = el.getAttribute('id')
const anchor = document.createElement('a') const anchor = document.createElement('a')
anchor.setAttribute('href', `#${id}`) anchor.setAttribute('href', `#${id}`)
anchor.setAttribute('class', cls) anchor.setAttribute('class', className)
anchor.innerText = String(text || DEFAULTS.text) anchor.innerText = String(text || DEFAULTS.text)
if (shouldAppend) { if (shouldAppend) {

View File

@ -13,4 +13,13 @@
margin-top: 48px; margin-top: 48px;
} }
} }
/* Hide anchors until mouseover */
& > .anchor {
opacity: 0;
}
&:hover > .anchor {
opacity: 1;
}
} }

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long