add: 目次をクリックしたときヘッダーにエフェクトをかける

This commit is contained in:
moris 2025-02-20 14:37:03 +09:00
parent f5fb89be3e
commit 134694eeb3
2 changed files with 44 additions and 3 deletions

View File

@ -229,4 +229,19 @@
}
}
}
& .marker {
text-decoration: line-through;
text-decoration-thickness: 1.125em;
animation-name: highlight;
animation-duration: .6s;
animation-fill-mode: forwards;
}
}
@keyframes highlight {
0% {text-decoration-color: #ffff0000;}
20% {text-decoration-color: #ffff00a0;}
100% {text-decoration-color: #ffff0040;}
}

View File

@ -29,16 +29,42 @@
}
window.addEventListener('message', toggleHide)
return ()=>{window.removeEventListener('message', toggleHide)}
})
onMount(()=>{
function setMarkerClass(id:string) {
let hash = document.querySelector("#blog .marker");
if (hash instanceof HTMLElement) {
hash.classList.remove("marker")
}
let element = document.querySelector(id);
if(element instanceof HTMLElement){
element.classList.add("marker")
}
}
if(location.hash) {
setMarkerClass(decodeURI(location.hash));
}
function hashChange(){
let urlHash = decodeURI(location.hash);
setMarkerClass(urlHash);
}
window.addEventListener('hashchange', hashChange)
return ()=>{window.removeEventListener('hashchange', hashChange)}
})
onMount(()=>{
document.getElementById('blog')?.addEventListener('click',(e:MouseEvent)=>{
if (e.target instanceof HTMLElement && e.target.matches('pre')) {
let code = e.target.textContent || '';
navigator.clipboard.writeText(code)
}
})
// リスナ解除
return ()=>{window.removeEventListener('message', toggleHide)}
})
</script>