如何使用CSS渐变突出显示模拟时钟上的25分钟?

问题描述 投票:1回答:1

我正在尝试实现倒数计时器,但要使用模拟时钟而不是数字时钟。我的目标是突出显示接下来的25分钟,作为从分钟时钟手柄开始的圆圈扇区。

这是我到目前为止的内容:https://codepen.io/seifjo/pen/ExVQLOd

这是上述密码笔中的相关代码:

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: #68a4ff;
}

.clock {
  width: 350px;
  height: 350px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #bcd0e7;
  border: 20px solid #fff;
  border-radius: 50%;
  background-size: cover;
  box-shadow: inset 0 0 30px rgba(0, 0, 0, .2), 0 20px 20px rgba(0, 0, 0, .2), 0 0 0 4px rgba(255, 255, 255, 1);
}

.clock {
  background-image: url(clock.png), linear-gradient(330deg, transparent 50%, #fff 50%), linear-gradient(540deg, #fff 50%, transparent 50%);
}

.clock::before {
  content: '';
  position: absolute;
  width: 15px;
  height: 15px;
  background: #848484;
  border: 2px solid #fff;
  border-radius: 50%;
  z-index: 10000;
}

.clock .hour,
.clock .min,
.clock .sec {
  position: absolute;
}

.clock .hour,
.hr {
  width: 160px;
  height: 160px;
}

.clock .min,
.mn {
  width: 190px;
  height: 190px;
}

.clock .sec,
.sc {
  width: 230px;
  height: 230px;
}

.hr,
.mn,
.sc {
  display: flex;
  justify-content: center;
  position: absolute;
  border-radius: 50%;
}

.hr::before {
  content: '';
  position: absolute;
  width: 8px;
  height: 80px;
  background: #848484;
  z-index: 10;
  border-radius: 6px 6px 0 0;
}

.mn:before {
  content: '';
  position: absolute;
  width: 4px;
  height: 90px;
  background: #d6d6d6;
  z-index: 11;
  border-radius: 6px 6px 0 0;
}

.sc:before {
  content: '';
  position: absolute;
  width: 2px;
  height: 150px;
  background: #ff6767;
  z-index: 12;
  border-radius: 6px 6px 0 0;
}
<div class="clock">
  <div class="mask"></div>
  <div class="hour">
    <div class="hr" id="hr">

    </div>
  </div>
  <div class="min">
    <div class="mn" id="mn">

    </div>
  </div>
  <div class="sec">
    <div class="sc" id="sc">

    </div>
  </div>
</div>
<script>
  const deg = 6;
  const hr = document.querySelector("#hr");
  const mn = document.querySelector("#mn");
  const sc = document.querySelector("#sc");
  setInterval(() => {
    let day = new Date();
    let hh = day.getHours() * 30;
    let mm = day.getMinutes() * deg;
    let ss = day.getSeconds() * deg;

    hr.style.transform = `rotateZ(${(hh)+(mm/12)}deg)`;
    mn.style.transform = `rotateZ(${mm}deg)`;
    sc.style.transform = `rotateZ(${ss}deg)`;
  }, 1000);
</script>

我设法从3点开始突出显示接下来的25分钟。但是我无法弄清楚分钟和度之间的关系。最终,这将是一个React应用,我将通过当前分钟作为道具。

javascript html css geometry linear-gradients
1个回答
0
投票

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: #68a4ff;
}

.clock {
  width: 350px;
  height: 350px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #bcd0e7;
  border: 20px solid #fff;
  border-radius: 50%;
  background-size: cover;
  box-shadow: inset 0 0 30px rgba(0, 0, 0, .2), 0 20px 20px rgba(0, 0, 0, .2), 0 0 0 4px rgba(255, 255, 255, 1);
}

.clock::before {
  content: '';
  position: absolute;
  width: 15px;
  height: 15px;
  background: #848484;
  border: 2px solid #fff;
  border-radius: 50%;
  z-index: 10000;
}

.clock .hour,
.clock .min,
.clock .sec {
  position: absolute;
}

.clock .hour,
.hr {
  width: 160px;
  height: 160px;
}

.clock .min,
.mn {
  width: 190px;
  height: 190px;
}

.clock .sec,
.sc {
  width: 230px;
  height: 230px;
}

.hr,
.mn,
.sc {
  display: flex;
  justify-content: center;
  position: absolute;
  border-radius: 50%;
}

.hr::before {
  content: '';
  position: absolute;
  width: 8px;
  height: 80px;
  background: #848484;
  z-index: 10;
  border-radius: 6px 6px 0 0;
}

.mn:before {
  content: '';
  position: absolute;
  width: 4px;
  height: 90px;
  background: #d6d6d6;
  z-index: 11;
  border-radius: 6px 6px 0 0;
}

.sc:before {
  content: '';
  position: absolute;
  width: 2px;
  height: 150px;
  background: #ff6767;
  z-index: 12;
  border-radius: 6px 6px 0 0;
}
<div class="clock">
  <div class="mask"></div>
  <div class="hour">
    <div class="hr" id="hr">

    </div>
  </div>
  <div class="min">
    <div class="mn" id="mn">

    </div>
  </div>
  <div class="sec">
    <div class="sc" id="sc">

    </div>
  </div>
</div>
<script>
  const deg = 6;
  const hr = document.querySelector("#hr");
  const mn = document.querySelector("#mn");
  const sc = document.querySelector("#sc");
  setInterval(() => {
    let day = new Date();
    let hh = day.getHours() * 30;
    let mm = day.getMinutes() * deg;
    let ss = day.getSeconds() * deg;

    hr.style.transform = `rotateZ(${(hh)+(mm/12)}deg)`;
    mn.style.transform = `rotateZ(${mm}deg)`;
    sc.style.transform = `rotateZ(${ss}deg)`;
  }, 1000);

  let lauchTime = new Date();
  let clock = document.querySelector('.clock');
  let startDeg = ((lauchTime.getMinutes() * deg) + 360 - 90) % 360;
  let highlightPeriod = 25;
  let endDeg = startDeg + (highlightPeriod * deg);
  //alert(startDeg);      
  clock.style.backgroundImage = `linear-gradient(${startDeg}deg, transparent 50%,  #fff 50%), linear-gradient(${endDeg}deg,  #fff 50%, transparent 50%)`;
</script>
© www.soinside.com 2019 - 2024. All rights reserved.