js实现一键换肤效果

方法1
请添加图片描述

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>一键换肤</title>
  <style>
    :root {
      --color: white;
    }

    .card {
      width: 120px;
      height: 200px;
    }

    .controller {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 10px;
    }

    .btn {
      border: none;
      height: 30px;
      width: 100px;
      color: #eeeeee;
      background: linear-gradient(45deg, #ce7777, lightblue, #c19fc1, transparent);
      border-radius: 999px;
      box-shadow: 0 0 2px 2px #eeeeee;
      cursor: pointer;
    }

    .card {
      border-radius: 5px;
      box-shadow: 0 0 2px 2px rgb(126, 124, 124);
    }

    .card:nth-child(1) {
      background: black;
    }

    .card:nth-child(3) {
      background: white;
    }

    html, body {
      background: var(--color);
      opacity: 0.9;
    }
    body {
      height: 100vh;
    }
    *{
      margin: 0;
      padding: 0;
    }
  </style>
</head>

<body>

  <div class="controller">
    <div class="card"></div>
    <div><button class="btn" onclick="changeSkin()">一键换肤</button></div>
    <div class="card"></div>
  </div>

</body>

</html>

<script>
  function changeSkin() {
    let oldV = 'white';
    let newV = 'black';
    let nowColor = document.body.style.getPropertyValue('--color');
    if(nowColor == '' || nowColor == oldV) {
      document.body.style.setProperty('--color', newV)
    }else {
      document.body.style.setProperty('--color', oldV)
    }
  }
</script>

方法2

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>change skin</title>
  <style id="theme">
    :root {
      --bgColor: #f00;
    }

    .skin {
      background: var(--bgColor);
      width: 200px;
      height: 200px;
    }
  </style>
</head>

<body>
  <div class="skin"></div>
  <button type="button" onclick="changeSkin('black')">change theme</button>
  <script>
    changeSkin = (theme) => {
      console.log("function starts");
      document.getElementById("theme").innerHTML = `
        :root{--bgColor:${theme};}
        .skin {
          background: var(--bgColor);
          width: 200px;
          height: 200px;
        }
      `
    }
   
  </script>
</body>
</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值