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

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

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>