js+css实现翻页时钟效果

<!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>Document</title>
  <style>
  *{
    list-style-type: none;
    padding: 0;
    margin: 0;
  }
  .show {
    width: 800px;
    height: 300px;
    margin: 100px auto;
    position: relative;
    background-color: black;
    color: #fff;
  }
  .time {
    display: flex;
    flex: 1;
    font-size: 160px;
    text-align: center;
    line-height: 300px;
    padding: 0 20px;
    overflow: hidden;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
  }
  .beforeTime {
    z-index: 100;
  }
  .time li:nth-child(2n) {
    flex: 1;
  }
  .time li:nth-child(2n+1) {
    flex: 4;
    background-color: #3b3d3b;
    position: relative;
    height: 200px;
    line-height: 200px;
    margin: auto 20px;
    border-radius: 10px;
    box-shadow: 0 0 20px 3px white;
  }
  .time li:nth-child(2n+1) .upBox,
  .time li:nth-child(2n+1) .downBox {
    position: absolute;
    left: 0;
    right: 0;
    overflow: hidden;
  }
  .time li:nth-child(2n+1) .upBox {
    top: 0;
    bottom: 50%;
    box-sizing: border-box;
    border-bottom: solid 3px #3b3d3b;
    background-color: #3b3d3b;
    transform-origin: bottom;
  }
  .time li:nth-child(2n+1) .downBox {
    top: 50%;
    bottom: 0;
    line-height: 0;
    box-sizing: border-box;
    background-color: #3b3d3b;
    overflow: hidden;
    transform-origin: top;
  }
  .change{
    border-bottom: 2px solid white;
  }
  </style>
</head>
<body>
  <div class="show">
    <ul class="time">
      <li>
          <div class="upBox beforeTime"></div>
          <div class="downBox beforeTime"></div>
          <div class="upBox afterTime"></div>
          <div class="downBox afterTime"></div>
      </li>
      <li>:</li>
      <li>
        <div class="upBox beforeTime"></div>
          <div class="downBox beforeTime"></div>
          <div class="upBox afterTime"></div>
          <div class="downBox afterTime"></div>
      </li>
      <li>:</li>
      <li>
        <div class="upBox beforeTime"></div>
          <div class="downBox beforeTime"></div>
          <div class="upBox afterTime"></div>
          <div class="downBox afterTime"></div>
      </li>
  </ul>
</div>
</body>
<script>
  //格式化时间
const formatTime = (time)=>{
  if(time < 10) time = '0' + time
  return time
}
//翻转前面下面的盒子向上180deg
const rotateUp = (ele,time, n) => {
  //传入的为一开始翻转的元素,即前面下面的盒子,以及新时间,以及第几个li盒子
  let rotateDeg = 0;
  ele.style.zIndex=50;
  //这个是所有上面的盒子
  const allUpBox =  document.querySelector(`li:nth-child(${n})`).querySelectorAll('.upBox');
  //所有前面的盒子
  const beforeTime = document.querySelector(`li:nth-child(${n})`).querySelectorAll('.beforeTime');
  // 让上面后面的盒子先不可见,然后设置为270°
  allUpBox[1].style.display = 'none';
  allUpBox[1].transform = `rotateX(270deg)`;
  const animation = () => {
    rotateDeg+=3;
    ele.style.transform = `perspective(500px) rotateX(${rotateDeg}deg)`;
    if(rotateDeg == 90) {
      //让它更新为最近时间后隐藏
      ele.innerHTML = time
      ele.style.zIndex = -1;
      //让刚刚上面隐藏的盒子重新显示出来并且完成90°-180°的旋转
      allUpBox[1].style.display = 'block';
      allUpBox[0].style.zIndex = 1;
      rotateDown(allUpBox[1])
      allUpBox[1].style.zIndex = 1;
    }
    if(rotateDeg == 150) {
      beforeTime[0].innerHTML = time
    }
    if(rotateDeg < 180) {
      requestAnimationFrame(animation);
    }
  }
  animation()
} 

const rotateDown = (ele) => {
  let rotateDeg = 270;
  const animation = () => {
    rotateDeg+=3;
    ele.style.transform = `perspective(500px) rotateX(${rotateDeg}deg)`;
    if(rotateDeg < 360) {
      requestAnimationFrame(animation);
    }
  }
  animation()
} 
let time = new Date();
let oldHour = time.getHours();
var oldMinute = time.getMinutes();
var oldSecond = time.getSeconds();
document.querySelector('li:nth-child(1)').querySelectorAll('.beforeTime').forEach(ele=> ele.innerHTML = formatTime(oldHour));
document.querySelector(' li:nth-child(3)').querySelectorAll('.beforeTime').forEach(ele=> ele.innerHTML = formatTime(oldMinute));
document.querySelector(' li:nth-child(5)').querySelectorAll('.beforeTime').forEach(ele=> ele.innerHTML = formatTime(oldSecond));
const changeTime = ()=>{
    let time = new Date();
    let hour = time.getHours();
    let minute = time.getMinutes();
    let second = time.getSeconds();
    const setHourBox = document.querySelector('li:nth-child(1)').querySelectorAll('.afterTime');
    if(!setHourBox[0].innerHTML || setHourBox[0].innerHTML!=formatTime(hour)) {
      if(setHourBox[0].innerHTML)  rotateUp(document.querySelector('li:nth-child(1)').querySelectorAll('.beforeTime')[1], formatTime(hour), 1);
      setHourBox.forEach(ele=> ele.innerHTML = formatTime(hour));
    }
    const setMinuteBox = document.querySelector('li:nth-child(3)').querySelectorAll('.afterTime');
    if(!setMinuteBox[0].innerHTML || setMinuteBox[0].innerHTML != formatTime(minute)) {
      if(setMinuteBox[0].innerHTML)  rotateUp(document.querySelector('li:nth-child(3)').querySelectorAll('.beforeTime')[1], formatTime(minute), 3);
      setMinuteBox.forEach(ele=> ele.innerHTML = formatTime(minute));
    }
    const setSecondBox = document.querySelector('li:nth-child(5)').querySelectorAll('.afterTime')
    setSecondBox.forEach(ele=> ele.innerHTML = formatTime(second));
    rotateUp(document.querySelector('li:nth-child(5)').querySelectorAll('.beforeTime')[1], formatTime(second), 5)
   
    setTimeout(changeTime,1000);
}
changeTime();
</script>
</html>
  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值