Merge pull request #149 from yuweizzz/fix

fix: no twitterIconElement raise error
This commit is contained in:
MeiK
2024-07-09 16:08:20 +08:00
committed by GitHub
2 changed files with 31 additions and 28 deletions

View File

@@ -204,19 +204,19 @@ window.onscroll = function (e) {
} }
}; };
var style = localStorage.getItem('data-color-mode'); var style = localStorage.getItem('data-color-mode')
iconElement = document.getElementById('github-icon'); githubIconElement = document.getElementById('github-icon')
twitterIconElement = document.getElementById('twitter-icon'); twitterIconElement = document.getElementById('twitter-icon')
if (style == 'light') { if (style == 'light') {
if (iconElement) iconElement.setAttribute('fill', '#24292e'); if (githubIconElement) githubIconElement.setAttribute('fill', '#24292e')
if (twitterIconElement) twitterIconElement.setAttribute("fill","black") if (twitterIconElement) twitterIconElement.setAttribute('fill', 'black')
} }
else { else {
if (iconElement) { if (githubIconElement) {
iconElement.removeAttribute('fill'); githubIconElement.removeAttribute('fill')
iconElement.setAttribute('class', 'octicon'); githubIconElement.setAttribute('class', 'octicon')
iconElement.setAttribute('color', '#f0f6fc'); githubIconElement.setAttribute('color', '#f0f6fc')
} }
if (twitterIconElement) twitterIconElement.setAttribute("fill","white") if (twitterIconElement) twitterIconElement.setAttribute('fill', 'white')
} }
</script> </script>

View File

@@ -1,22 +1,12 @@
function switchTheme() { function switchTheme() {
const currentStyle = currentTheme(); const currentStyle = currentTheme()
const iconElement = document.getElementById('github-icon');
if (currentStyle === 'light') { if (currentStyle === 'light') {
setTheme('dark'); setTheme('dark')
setIconTheme('dark') setIconTheme('dark')
if (iconElement) {
iconElement.setAttribute('class', 'octicon');
iconElement.setAttribute('color', '#f0f6fc');
}
} }
else { else {
setTheme('light'); setTheme('light')
setIconTheme('light') setIconTheme('light')
if (iconElement) {
iconElement.removeAttribute('color');
iconElement.removeAttribute('class');
}
} }
} }
@@ -29,7 +19,9 @@ function setTheme(style) {
} }
function setIconTheme(theme) { function setIconTheme(theme) {
twitterIconElement = document.getElementById('twitter-icon'); const twitterIconElement = document.getElementById('twitter-icon')
const githubIconElement = document.getElementById('github-icon')
if (twitterIconElement) {
if (theme === 'light') { if (theme === 'light') {
twitterIconElement.setAttribute("fill", "black") twitterIconElement.setAttribute("fill", "black")
} else if (theme === 'dark') { } else if (theme === 'dark') {
@@ -37,6 +29,17 @@ function setIconTheme(theme) {
} }
} }
if (githubIconElement) {
if (theme === 'light') {
githubIconElement.removeAttribute('color')
githubIconElement.removeAttribute('class')
} else if (theme === 'dark') {
githubIconElement.setAttribute('class', 'octicon')
githubIconElement.setAttribute('color', '#f0f6fc')
}
}
}
function currentTheme() { function currentTheme() {
const localStyle = localStorage.getItem('data-color-mode'); const localStyle = localStorage.getItem('data-color-mode');
const systemStyle = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'; const systemStyle = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';