【发布时间】:2020-11-10 19:15:31
【问题描述】:
我是一个新手,试图从移动优先开始构建投资组合页面。 我刚刚了解了集成偏好颜色方案媒体查询,但在添加之前,我想让用户选择在我的网页中的暗/亮模式之间切换。 我已经添加了一个 javascript 可以做到这一点。
我希望我的 logo 和 icons.svg 在我切换主题时也发生变化
你可以看看我链接的codepen。
您可以按照以下步骤测试问题:
- 进入菜单 --> 点击切换到灯光模式
- 徽标发生了变化,但是当我切换回深色模式时它不会变回来。
- 我也有同样的问题,当我点击徽标时,在浅色模式下,它会更改为默认的深色模式徽标。
我假设如果我知道如何让这个仅用于徽标,我也可以对其他图标使用相同的技术。
如何让它工作?
Check the codepen link to my problem 对于我的 codepen 中我的 svg 的长而令人不安的 base64 数据,我不知道还能做什么。
const toggleSwitch = document.querySelector('.theme-switch input[type="checkbox"]');
function switchTheme(e) {
if (e.target.checked) {
document.documentElement.setAttribute('data-theme', 'dark');
} else {
document.documentElement.setAttribute('data-theme', 'light');
}
}
toggleSwitch.addEventListener('change', switchTheme, false);
/*This is to store the light or dark mode preferences for future visits*/
function switchTheme(e) {
if (e.target.checked) {
document.documentElement.setAttribute('data-theme', 'dark');
localStorage.setItem('theme', 'dark'); //add this
} else {
document.documentElement.setAttribute('data-theme', 'light');
localStorage.setItem('theme', 'light'); //add this
}
}
/*to check for saved preferences on load of the site*/
const currentTheme = localStorage.getItem('theme') ? localStorage.getItem('theme') : null;
if (currentTheme) {
document.documentElement.setAttribute('data-theme', currentTheme);
if (currentTheme === 'dark') {
toggleSwitch.checked = true;
}
}
function changeImage() {
var image = document.getElementById('mylogo');
if (image.src.match("images/LOGO_SN_over_black.svg")) {
image.src = "images/LOGO_SN.svg";
} else {
image.src = "images/LOGO_SN.svg";
}
}
:root {
--background-color: #262626;
--mainfontcolor: #f7f7f2;
--hightlightcolor: #01BAEF;
}
[data-theme="dark"] {
--background-color: #f7f7f2;
--mainfontcolor: #262626;
--hightlightcolor: #01BAEF;
}
body {
background-color: var(--background-color);
font-family: 'Lato', sans-serif;
line-height: 1.5;
}
/*Navigation panel*/
.logo-header img {
position: absolute;
top: 1.2em;
left: 2em;
max-width: 3em;
opacity: 0.9;
z-index: 9999;
}
header {
background: var(--background-color);
height: 5em;
position: fixed;
top: 0;
-webkit-transition: top 0.2s ease-in-out;
-moz-transition: top 0.2s ease-in-out;
-o-transition: top 0.2s ease-in-out;
transition: top 0.2s ease-in-out;
width: calc(100vw);
z-index: 90;
}
/*menu bar styles start here*/
.menu-wrap {
position: fixed;
top: 0;
right: 0;
z-index: 99;
}
.menu-wrap .toggler {
position: absolute;
top: 2.5em;
right: 3.1em;
cursor: pointer;
opacity: 0;
width: 4.2em;
height: 2em;
z-index: 100;
}
.menu-wrap .hamburger {
position: absolute;
top: 1.8em;
right: 2em;
z-index: 99;
width: 4em;
height: 2em;
padding: 1em;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-align: center;
-moz-box-align: center;
-ms-flex-align: center;
-ms-box-align: center;
align-items: center;
-webkit-box-pack: center;
-moz-box-pack: center;
-ms-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
}
/*hamburger line */
.menu-wrap .hamburger>div {
position: relative;
-webkit-box-flex: 0;
-moz-box-flex: 0;
-ms-flex: none;
flex: none;
width: 100%;
height: 0.1em;
background-color: var(--mainfontcolor);
opacity: 0.9;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-align: center;
-moz-box-align: center;
-ms-box-align: center;
-ms-flex-align: center;
align-items: center;
-webkit-box-pack: center;
-moz-box-pack: center;
-ms-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
-webkit-transition: all 0.5s ease;
transition: all 0.5s ease;
}
.menu-wrap .hamburger>div::before,
.menu-wrap .hamburger>div::after {
content: '';
position: absolute;
z-index: 99;
top: -0.6em;
width: 100%;
height: 0.1em;
background-color: var(--mainfontcolor);
}
.menu-wrap .hamburger>div:after {
top: 0.6em;
}
/*toggle animate*/
.menu-wrap .toggler:checked+.hamburger>div {
-webkit-transform: rotate(135deg);
transform: rotate(135deg);
}
/* Turns Lines Into X */
.menu-wrap .toggler:checked+.hamburger>div:before,
.menu-wrap .toggler:checked+.hamburger>div:after {
top: 0;
-webkit-transform: rotate(90deg);
transform: rotate(90deg);
}
/* Rotate On Hover When Checked */
.menu-wrap .toggler:checked:hover+.hamburger>div {
-webkit-transform: rotate(225deg);
transform: rotate(225deg);
}
/* To Show the Menu */
.menu-wrap .toggler:checked~.menu {
visibility: visible;
}
.menu-wrap .toggler:checked~.menu>div {
-webkit-transform: scale(1);
transform: scale(1);
-webkit-transition-duration: 0.4s;
transition-duration: 0.4s;
}
.menu-wrap .toggler:checked~.menu>div>div {
opacity: 1;
-webkit-transition: opacity 0.3s ease 0.3s;
transition: opacity 0.3s ease 0.3s;
-webkit-transition: opacity 0.4s ease 0.3s;
-moz-transition: opacity 0.3s ease 0.3s;
-o-transition: opacity 0.3s ease 0.3s;
}
.menu-wrap .menu {
position: relative;
top: 0;
right: 0;
width: calc(100vw);
height: calc(100vh);
visibility: hidden;
overflow: hidden;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
-webkit-backface-visibility: hidden;
background-color: var(--background-color);
overflow-y: auto;
-webkit-overflow-scrolling: touch;
}
.menu-wrap .menu>div {
background-color: var(--background-color);
border-radius: 0%;
width: 100vw;
height: 100vh;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-flex: 0;
-ms-flex: none;
flex: none;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
-webkit-transform: scale(0);
transform: scale(0);
transition: all 0.4s ease;
-webkit-transition: all 0.4s ease;
-moz-transition: all 0.4s ease;
-o-transition: all 0.4s ease;
}
.menu-wrap .menu>div>div {
text-align: center;
max-width: calc(100vw);
max-height: calc(100vh);
opacity: 0;
transition: opacity 0.3s ease;
-webkit-transition: opacity 0.3s ease;
-moz-transition: opacity 0.3s ease;
-o-transition: opacity 0.3s ease;
padding-top: 1.5em;
}
.menu-wrap .menu>div>div>ul>li {
list-style: none;
color: var(--mainfontcolor);
font-size: 1.4em;
padding: 0.8em;
}
.menu-wrap .menu>div>div>ul>li>a {
color: var(--mainfontcolor);
opacity: 0.9;
text-decoration: none;
transition: color 0.3s ease;
-webkit-transition: color 0.3s ease;
-moz-transition: color 0.3s ease;
-o-transition: color 0.3s ease;
}
/*Main page content*/
.mainpage-container {
font-family: 'Lato', sans-serif;
color: var(--mainfontcolor);
margin-top: 5em;
text-align: center;
overflow: hidden;
max-width: 100vw;
}
.mainpage-section2 {
margin-top: 1em;
}
.content-image {
width: calc(100vw);
height: calc(95vh);
background-image: url('https://st.depositphotos.com/1695366/1398/v/450/depositphotos_13980191-stock-illustration-cartoon-woman-writing-new-years.jpg');
background-repeat: no-repeat;
background-position: right;
background-size: cover;
}
.mainpage-text {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
}
.mainpage-text h1 {
font-family: "Lato", sans-serif;
font-size: 2rem;
font-weight: 400;
color: var(--mainfontcolor);
text-align: center;
line-height: 1.5;
padding-top: 3em;
padding-bottom: 0.5em;
padding-left: 1.2rem;
padding-right: 1.2rem;
opacity: 0.9;
}
.mainpage-text h2 {
font-family: "Lato", sans-serif;
font-size: 1.1rem;
font-weight: 100;
color: var(--mainfontcolor);
opacity: 0.8;
text-align: center;
line-height: 1.5;
padding-left: 1.2rem;
padding-right: 1.2rem;
padding-top: 0.5em;
padding-bottom: 2.5em;
}
.mainpage-text p {
padding-left: 1.2rem;
padding-right: 1.2rem;
padding-top: 1.5em;
padding-bottom: 2em;
transition: color 0.3s ease;
}
.contact-link {
text-decoration: none;
font-family: "Lato", sans-serif;
font-size: 1rem;
font-weight: 100;
color: var(--mainfontcolor);
opacity: 0.8;
text-align: center;
line-height: 1.5;
position: relative;
}
.mainpage-text p :hover {
color: var(--hightlightcolor);
}
.contact-link::before {
border-top: 0.05em dotted var(--mainfontcolor);
content: '';
position: absolute;
right: 0;
top: 1.2em;
left: 0;
}
/*toggle to dark or light mode*/
.theme-switch-wrapper {
display: flex;
align-items: center;
}
.theme-switch-wrapper em {
margin-left: 0.5em;
margin-right: 0.5em;
}
.theme-switch-wrapper img {
width: 1em;
padding-top: 0.3em;
}
.theme-switch {
display: inline-block;
height: 1.5em;
position: relative;
width: 2.6em;
}
.theme-switch input {
display: none;
}
.slider {
background-color: #ccc;
bottom: 0;
cursor: pointer;
left: 0;
position: absolute;
right: 0;
top: 0;
transition: .4s;
}
.slider:before {
background-color: #fff;
bottom: 0.15em;
content: "";
height: 1.2em;
left: 0.16em;
position: absolute;
transition: .4s;
width: 1.2em;
}
input:checked+.slider {
background-color: #01BAEF;
}
input:checked+.slider:before {
transform: translateX(26px);
}
.slider.round {
border-radius: 34px;
}
.slider.round:before {
border-radius: 50%;
}
<body>
<header>
<div class="nav-container">
<div class="logo-header">
<a href="index.html"><img id="mylogo" src="image-of-logo-for darktheme"></a>
</div>
<div class="menu-wrap">
<input type="checkbox" class="toggler">
<div class="hamburger">
<div></div>
</div>
<div class="menu">
<div>
<div>
<ul>
<li><a href="about.html">About</a></li>
<li><a href="projects.html">Projects</a></li>
<li><a href="experience.html">Experience</a></li>
<li><a href="education.html">Education</a></li>
<li><a href="skills.html">Skills</a></li>
<li><a href="#">Resources</a></li>
<li><a href="contact.html">Contact</a></li>
<li>
<div class="theme-switch-wrapper">
<em><img src="moon-dark"></em>
<label class="theme-switch" for="checkbox">
<input type="checkbox" onclick="changeImage()" value="Change" id="checkbox" />
<div class="slider round"></div>
</label>
<em><img src="sun-light"></em>
</div>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</header>
<main>
<div class="mainpage-container">
<section class="mainpage-section1">
<div class="content-image"></div>
</section>
<section class="mainpage-section2">
<div class="mainpage-text">
<div>
<h1>I'M A <br>MATERIAL SCIENTIST<br> - PHYSICIST.</h1>
<h2>(With speciality in nanotechnology, materials science and a Ph.D. in physics.) </h2><br>
<h2>I develop efficient science communication strategies and create content for science blogs, magazines and, radio/tv shows.</h2>
<p><a class="contact-link" href="contact.html">Get in touch with me.</a></p>
</div>
</div>
</section>
</div>
</main>
</body>
【问题讨论】:
标签: javascript html css