【每日一练】207—黑暗模式效果的实现

每日一练1年前 (2023)更新 admin
1,442 0

以下是今天每日一练的最终效果:

【每日一练】207—黑暗模式效果的实现

HTML:

<!DOCTYPE html>
<html lang=”en”>
<head>
  <meta charset=”UTF-8″>
  <meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
  <title>【每日一练】第207练</title>
  <link rel=”stylesheet” href=”index.css”>
</head>
<body data-theme=”light”>
  <section>
    <div class=”container”>
      <h1>一日一句英语励志名言</h1>
      <p>成功者学习别人的经验,一般人学习自己的经验。
        Successful people learn from other people’s experience, the average person to learn their own experience.</p>
      <button>阅读更多</button>
    </div>
  </section>
  <div class=”theme-switcher”>
    <input type=”checkbox” id=”switcher”>
    <label for=switcher>switch</label>
  </div>
  <script src=”index.js”></script>
</body>
</html>

CSS:

*{
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}
body{
  font-family: ‘Montserrat’;
  color: var(–color-4);
}
body[data-theme=”light”]{
  –color-1:rgb(200, 220, 241);
  –color-2:white;
  –color-3: white;
  –color-4: rgb(80, 82, 110);
}
body[data-theme=”dark”]{
  –color-1:#1E1F26;
  –color-2:#292c33;
  –color-3: rgb(39, 40, 42);
  –color-4: rgb(186, 186, 202);
}
section{
  background-color: var(–color-1);
  min-height: 100vh;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}
.container{
  width: 90%;
  margin: 0 auto;
  background-color: var(–color-2);
  border-radius: 8px;
  padding: 20px;
  max-width: 500px;
}
h1{
  font-size: 30px;
  font-weight: 500;
  text-transform: uppercase;
}
p{
  margin-top: 10px;
  font-size: 16px;
  font-weight: 500;
  letter-spacing: 1px;
  line-height: 25px;
}
button{
  background-color: var(–color-4);
  padding: 10px 30px;
  border: none;
  font-size: 14px;
  text-transform: uppercase;
  color: var(–color-3);
  border-radius:4px ;
  margin-top: 20px;
  cursor: pointer;
}
.theme-switcher{
  position: absolute;
  right: 30px;
  top: 10px;
}
input{
  width: 0;
  height: 0;
  display: none;
  visibility: hidden;
}
label{
  cursor: pointer;
  display: block;
  text-indent: -9999px;
  height: 30px;
  width: 60px;
  border-radius: 50px;
  background-color: rgb(255, 255, 255);
  transition: .5s ease background-color;
}
label::after{
  position: absolute;
  content: ”;
  width: 20px;
  height: 20px;
  border-radius: 50px;
  top: 50%;
  left: 5px;
  transform: translateY( -50%);
  background-color: rgb(46, 42, 68);
  transition: .5s ease;
}
input:checked + label::after{
  /* left: calc(100% – 2.5px); */
  left: calc(100% – 25px);
  background-color: aliceblue;
}
input:checked + label{
  background-color: rgb(25, 26, 37);
  border: 2px solid whitesmoke;
}

JS:

const input = document.querySelector(‘.theme-switcher input’);
input.addEventListener(‘change’, (e) => {
  if (e.target.checked) {
    document.body.setAttribute(‘data-theme’, ‘dark’);
  } else {
    document.body.setAttribute(‘data-theme’, ‘light’);
  }
})

写在最后

 

以上就是每日一练的全部内容,希望今天的小练习对你有用,如果你觉得有帮助的话,请点赞我,关注我,并将它分享给你身边做开发的朋友,也许能够帮助到他。

我是杨小爱,我们明天见。

© 版权声明

相关文章

暂无评论

暂无评论...