Skip to content

Commit

Permalink
ensure element exists before access it (#356)
Browse files Browse the repository at this point in the history
* ensure element exists before access it

* use optional chaining
  • Loading branch information
yshrsmz authored Aug 3, 2022
1 parent aa6f113 commit 1ba7d8d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion assets/js/main.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion assets/js/partials/dark-mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ const currentTheme = () => sessionStorage.getItem('theme')
function setMode(theme) {
document.documentElement.setAttribute('data-theme', theme)
sessionStorage.setItem('theme', theme)
document.getElementById('theme-toggle').innerHTML = themeButton[theme]
const toggle = document.getElementById('theme-toggle')
if (toggle) {
toggle.innerHTML = themeButton[theme]
}
}

function themeToggle() {
Expand Down
7 changes: 5 additions & 2 deletions assets/js/partials/navbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ document.addEventListener("DOMContentLoaded", function (event) {
const menu = document.querySelector('nav ul');

['click', 'touch'].forEach(function (e) {
pull.addEventListener(e, function () {
pull?.addEventListener(e, function () {
menu.classList.toggle('hide')
}, false);
});
Expand All @@ -18,6 +18,9 @@ document.addEventListener("DOMContentLoaded", function (event) {
*/
window.addEventListener('scroll', function () {
const offset = -(window.scrollY || window.pageYOffset || document.body.scrollTop) / 3;
document.getElementById("main").style.backgroundPosition = '100% ' + (offset - 50) + 'px' + ', 0%, center top';
const main = document.getElementById('main');
if (main) {
main.style.backgroundPosition = '100% ' + (offset - 50) + 'px' + ', 0%, center top';
}
});
});

0 comments on commit 1ba7d8d

Please sign in to comment.