微信小程序案例-03翻页时钟-3

news/2024/7/20 2:43:47 标签: 微信小程序, 小程序, 前端, css, javascript, html5

小程序>微信小程序实战系列

文章目录

  • 小程序>微信小程序实战系列
  • 前言
  • 动态翻页效果实现
    • clock.wxml
    • clock.wxss
    • clock.js
    • 运行效果
  • 总结

前言

本文继续完成最后一个部分“动态翻页效果”。

动态翻页效果实现

clock.wxml

<view class="container">
<view class="clock-container">
  <view class="flip-container">
    <block wx:for="{{timeArr}}" wx:for-item="unit" wx:for-index="unitIndex" wx:key="unitIndex">
      <view class="flip-items">
        <block wx:for="{{unit.max+1}}" wx:for-item="item" wx:for-index="index" wx:key="index">
          <view class="{{['item',(unit.current==index)?'current':'',(unit.current-1==index||index==unit.max&&unit.current==0)?'past':'']}}">
            <view class="up">
              <view class="inner">{{index}}</view>
              <view class="shadow"></view>
            </view>
            <view class="down">
              <view class="inner">{{index}}</view>
              <view class="shadow"></view>
            </view>
          </view>
        </block>
      </view>
    </block>
  </view>
</view>
</view>

clock.wxss

css">.flip-container {
  display: flex;
  justify-content: center;
  position: relative;
  padding: 0 20rpx;
}


.flip-items {
  position: relative;
  width: 90rpx;
  height: 145rpx;
  font-size: 128rpx;
  font-weight: bold;
  border-radius: 10rpx;
  border: 1rpx solid rgba(121, 121, 121, 0.384);
  box-shadow: 0 2rpx 18rpx rgba(0, 0, 0, 0.7);
}

.flip-container .flip-items:nth-of-type(2n+1) {
  margin-right: 12rpx;
}

.flip-container .flip-items:nth-of-type(2),
.flip-container .flip-items:nth-of-type(4) {
  margin-right: 36rpx;
}

.flip-container .flip-items:nth-of-type(2)::after,
.flip-container .flip-items:nth-of-type(4)::after,
.flip-container .flip-items:nth-of-type(2)::before,
.flip-container .flip-items:nth-of-type(4)::before {
  position: absolute;
  right: -18rpx;
  content: '';
  transform: translateX(50%);
  width: 25rpx;
  height: 25rpx;
  border-radius: 50%;
  box-shadow: rgba(0, 0, 0, 0.7) 1px 1px 5px;
  background-color: rgba(0, 0, 0, 0.801);
}

.flip-container .flip-items:nth-of-type(2)::before,
.flip-container .flip-items:nth-of-type(4)::before {
  top: 25%;
}

.flip-container .flip-items:nth-of-type(2)::after,
.flip-container .flip-items:nth-of-type(4)::after {
  bottom: 25%;
}


.item {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1;
}

.item::before {
  position: absolute;
  content: '';
  top: 75rpx;
  width: 100%;
  height: 5rpx;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 9;
}

.flip-container .flip-items .item .up,
.flip-container .flip-items .item .down {
  position: absolute;
  left: 0;
  right: 0;
  height: 50%;
  overflow: hidden;
}

/* 设置transform的原点 */
.up {
  transform-origin: 50% 100%;
  top: 0;
}

/* 设置transform的原点 */
.flip-container .flip-items .item .down {
  transform-origin: 50% 0%;
  bottom: 0;
}

.flip-container .flip-items .item .inner {
  position: absolute;
  width: 100%;
  height: 145rpx;
  color: #252525;
  left: 0;
  line-height: 145rpx;
  text-align: center;
  text-shadow: 0 2rpx 4rpx rgb(0, 0, 0);
  border-radius: 10rpx;
  background-color: #55e3e3;
}

.flip-container .flip-items .item .up .inner {
  top: 0;
}

.flip-container .flip-items .item .down .inner {
  bottom: 0;
}

.flip-container .flip-items .item .up .shadow {
  border-top-left-radius: 10rpx;
  border-top-right-radius: 10rpx;
}

.flip-container .flip-items .item .down .shadow {
  border-bottom-left-radius: 10rpx;
  border-bottom-right-radius: 10rpx;
}

.flip-container .flip-items .item.past {
  z-index: 3;
}

/*  current time keep top z-index:4 */
.flip-container .flip-items .item.current {
  animation: highter-level 0.5s 0.5s linear forwards;
  z-index: 2;
}

/* 前一秒的上半页,旋转 0~-90deg */
.flip-container .flip-items .item.past .up {
  animation: flip-past-up 0.5s linear both;
}

/* 当前秒的下半页,旋转90~0 */
.flip-container .flip-items .item.current .down {
  animation: flip-current-down 0.5s 0.5s linear both;
}

@keyframes flip-current-down {
  from {
    transform: rotateX(90deg);
  }

  to {
    transform: rotateX(0deg);
  }
}
@keyframes flip-past-up {
  from {
    transform: rotateX(0deg);
  }

  to {
    transform: rotateX(-90deg);
  }
}

@keyframes highter-level {
  from {
    z-index: 4;
  }

  to {
    z-index: 4;
  }
}

.flip-container .flip-items .item .shadow {
  position: absolute;
  width: 100%;
  height: 100%;
}

/* show 渐变 */
.flip-container .flip-items .item.past .up .shadow {
  background: linear-gradient(rgba(0, 0, 0, 0.1) 0%, #000 100%);
  animation: show 0.5s linear both;
}

/* show 渐变 */
.flip-container .flip-items .item.past .down .shadow {
  background: linear-gradient(#000 0%, rgba(0, 0, 0, 0.1) 100%);
  animation: show 0.5s linear both;
}

/* hide 渐变 */
 .flip-container .flip-items .item.current .up .shadow {
  background: linear-gradient(rgba(0, 0, 0, 0.1) 0%, #000 100%);
  animation: hide 0.5s 0.3s linear both;
}

.flip-container .flip-items .item.current .down .shadow {
  background: linear-gradient(#000 0%, rgba(0, 0, 0, 0.1) 100%);
  animation: hide 0.5s 0.3s linear both;
}

@keyframes show {
  from {
    opacity: 0;
  }

  to {
    opacity: 1;
  }
}

@keyframes hide {
  from {
    opacity: 1;
  }

  to {
    opacity: 0;
  }
}

clock.js


Page({
  data: {

  },

  onLoad: function () {
   this.setTimeRunner()
  },

  setTimeRunner: function () {
    this.timeRunner = setInterval(() => {
      this.getTimeStr();
      this.getTimeArr();
    }, 1000);
  },

   getTimeStr: function () {
    let that = this;
    var time = new Date();
    var hour = ('0' + time.getHours()).slice(-2);
    var minute = ('0' + time.getMinutes()).slice(-2);
    var second = ('0' + time.getSeconds()).slice(-2);
    var timeStr = (hour + minute + second).split('');
    that.setData({timeStr:timeStr})
  },

    getTimeArr: function () {
      var timeArr = this.data.timeStr.map(function (element, index) {
        var max;
        if (index & 1 == 1) { 
          max = 9;
        } else if (index == 0) { 
          max = 2;
        } else if (index == 2) { 
          max = 5;
        } else if (index == 4) { 
          max = 5;
        }
        var current = Number(element)
        return {
          max: max,
          current: current
        };
      })
      this.setData({timeArr:timeArr})
    },

    beforeDestroy:function () {
      clearInterval(this.timeRunner);
    },
  onUnload: function () {
    this.beforeDestroy()
  }
})

运行效果

点击翻页时钟运行效果

总结

本项目的综合性较强,对wxss、wxml、js的综合运用需要较为熟悉,是一个非常好的练手小项目。


http://www.niftyadmin.cn/n/5320063.html

相关文章

C++学习笔记(三十):c++ 多个返回值

本节主要介绍c函数多个返回值的实现方式。 返回结构体 通过返回一个结构体来返回多个值&#xff0c;该实现方式代码的可读性高&#xff0c;便于维护。同时在栈上创建结构体&#xff0c;函数也拥有比较好的性能。示例代码如下&#xff1a; #include <iostream>struct Test…

实现LCM在docker之间的通信

目录 1.docker容器互联 新建网络 连接容器 2.设置环境变量 3.在两个docker之间实现通信 1.docker容器互联 新建网络 $ docker network create -d bridge test-net 连接容器 运行一个容器并连接到新建的 test-net 网络: $ docker run -itd --name lcm_1 --network tes…

L1-023 输出GPLT(Java)

给定一个长度不超过10000的、仅由英文字母构成的字符串。请将字符重新调整顺序&#xff0c;按GPLTGPLT....这样的顺序输出&#xff0c;并忽略其它字符。当然&#xff0c;四种字符&#xff08;不区分大小写&#xff09;的个数不一定是一样多的&#xff0c;若某种字符已经输出完&…

揭秘HTTP协议:深入了解互联网通信的核心!

文章目录 HTTPHTTP的消息结构HTTP 常用请求方法HTTP 状态码 HTTP HTTP 是超文本传输协议&#xff0c;HTTP是缩写&#xff0c;全称是 HyperText Transfer Protocol 超文本指的是 HTML、css、JavaScript和图片等&#xff0c;HTTP的出现就是为方便接收和发布超HTML页面&#xff0c…

很容易导致错误的两种空格:普通空格和不换行空格

不换行空格&#xff08;Non-Breaking Space&#xff0c;又称硬空格&#xff0c;ASCII 160&#xff09;和&#xff08;普通空格&#xff0c;ASCII 32&#xff09;都是用于表示空白的字符&#xff0c;但它们有一些关键的区别&#xff1a; 显示效果&#xff1a; ASCII 32&#xff…

【PaperReading】2. MM-VID

Category Content 论文题目 MM-VID: Advancing Video Understanding with GPT-4V(ision) 作者 Kevin Lin, Faisal Ahmed, Linjie Li, Chung-Ching Lin, Ehsan Azarnasab, Zhengyuan Yang, Jianfeng Wang, Lin Liang, Zicheng Liu, Yumao Lu, Ce Liu, Lijuan Wang (Microso…

【docker】Docker Compose 使用介绍

一、什么是Docker Compose Docker Compose是一个用于定义和运行多个Docker容器的工具。它允许您使用YAML文件来配置应用程序的服务、网络和卷等方面&#xff0c;并通过单个命令即可快速启动和停止整个应用程序的多个容器。 Docker Compose的主要作用如下&#xff1a; 管理多个…

数据类型、数据类型转换(Java)

一、数据类型的分类 1. byte&#xff1a;1字节&#xff0c;-128~127 2. short&#xff1a;2字节&#xff0c;-32768~32767 3. int&#xff1a;4字节 默认整型 4. long&#xff1a;8字节 注意&#xff1a;随便写一个整型字面量会默认是整型的&#xff0c;所以我们在写一个…