微信小程序实战-02翻页时钟-2

news/2024/7/20 2:46:53 标签: 微信小程序, 小程序

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

文章目录

  • 小程序>微信小程序实战系列
  • 前言
  • 计时功能实现
    • clock.wxml
    • clock.wxss
    • clock.js
  • 运行效果
  • 总结

前言

接着《小程序>微信小程序实战-01翻页时钟-1》,继续完成“6个页面的静态渲染和计时”功能。

计时功能实现

clock.wxml

clock.wxml中 新增了wx:for(基础知识),用来现实六个“页面”;“item”相当于一个较大的盒子“包裹”着“flip_item”及其后代组件。“item”用来渲染时钟的四个“黑点”,flip_item用来渲染“页轴”。

<!--pages/clock/clock.wxml-->

<view class="container">
  <view class="clock_container">
    <block wx:for="{{timeArr}}" wx:for-index="timeIndex" wx:for-item="timeItem" wx:key="timeIndex">
      <view class="item">
        <view class="flip_item">
          <view class="up">
            <view class="number">{{timeItem}}</view>
          </view>
          <view class="down">
            <view class="number">{{timeItem}}</view>
          </view>
        </view>
      </view>
    </block>
  </view>
</view>

clock.wxss

CSS中,::before::after都是创建一个伪元素(pseudo-element);::before为匹配选中的元素的第一个子元素;::after为已选中元素的最后一个子元素。通常会配合content属性来为该伪元素添加装饰内容。这个伪元素默认是行内元素。

CSS中,:nth-of-type() 创建一个伪类(pseudo-class),基于同类型元素(组件名称)的兄弟元素中的位置来匹配元素。

每段样式的作用在代码中都做了注释。

/* pages/clock/clock.wxss */
.clock_container{
  display: flex;
}

/* 设置item的样式,固定宽高 */
.item {
  position: relative;
  width: 90rpx;
  height: 155rpx;
  border:1rpx solid rgba(121, 121, 121, 0.384);
  box-shadow: 0 4rpx 18rpx rgba(0,0,0,0.9);
  border-radius: 10rpx;
  margin-right: 12rpx;
  background-color: #55e3e3;
}

.flip_item{
  position: relative;
  width: 100%;
  height: 100%;
  box-shadow: 0 4rpx 18rpx rgba(0,0,0,0.9);
}

/* 第2、4页增加右边距 */
.item:nth-of-type(4),
.item:nth-of-type(2){
  margin-right: 48rpx;
}

/* 第2、4页增点 “黑点” */
.item:nth-of-type(4)::before,
.item:nth-of-type(4)::after,
.item:nth-of-type(2)::before,
.item:nth-of-type(2)::after{
  position: absolute;
  content:'';
  width: 25rpx;
  height: 25rpx;
  background-color: rgba(0,0,0,0.8);
  border-radius: 50%;
  left: 105rpx;
}

/* 增加 上“黑点”边距 */
.item:nth-of-type(4)::before,
.item:nth-of-type(2)::before{
  top: 30rpx;
}

/* 增加 下“黑点”边距 */
.item:nth-of-type(4)::after,
.item:nth-of-type(2)::after{
  bottom: 30rpx;
}

/* 时钟的单个数字 */
.number{
  position: absolute;
  /* border: 1px solid red; 调试用 */
  width: 100%;
  height: 155rpx;
  color: #252525;
  text-align: center;
  text-shadow: 0 2rpx 4rpx rgb(0, 0, 0);
  font-size: 118rpx;
  font-weight: bold;
}

/* 页轴 */
.flip_item::before{
  position: absolute;
  content: '';
  top: 75rpx;
  width: 100%;
  height: 5rpx;
  background-color: rgba(0, 0, 0, 0.5);
}

/*  掩盖“down”的上半部分 */
.down{
  position: absolute;
  width: 100%;
  height: 50%;
  overflow: hidden;
  bottom: 0;
}
.down .number{
  bottom: 0;
}

/* 掩盖“up”的下半部分 */
.up{
  position: absolute;
  width: 100%;
  height: 50%;
  overflow: hidden;
}

clock.js

// pages/clock/clock.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    timeArr:[]
  },

  /**
   * 获取时间数组
   */
  getTimeArr: function(){
    let tempArr = []
    let str = ""
    let now = new Date()
    // 获取小时
    let hours = now.getHours()
    // console.log("hours", hours)
    str = hours.toString()
    if (str.length === 1){
      tempArr[0] = '0'
      tempArr[1] = str[1]
    }else{
      tempArr[0] = str[0]
      tempArr[1] = str[1]
    }
    // 获取分钟
    let minutes = now.getMinutes()
    // console.log("minutes", minutes)
    str = minutes.toString()
    if (str === '0'){
      tempArr[2] = '0'
      tempArr[3] = '0'
    }else if (str.length === 1){
      tempArr[2] = '0'
      tempArr[3] = str[0]
    }else{
      tempArr[2] = str[0]
      tempArr[3] = str[1]
    }
    // 获取秒数
    let seconds = now.getSeconds()
    // console.log("seconds", seconds)
    str = seconds.toString()
    if (str === '0'){
      tempArr[4] = '0'
      tempArr[5] = '0'
    }else if (str.length === 1){
      tempArr[4] = '0'
      tempArr[5] = str[0]
    }else{
      tempArr[4] = str[0]
      tempArr[5] = str[1]
    }
    this.setData({timeArr:tempArr})
    // console.log("timeArr:", this.data.timeArr)
  },

  /**
   * 设置定一个定时器, 每秒更新TimeArr
   */
  timeRunner: function(){
    this.timer = setInterval(()=>{ //设置定时器
      this.getTimeArr()
    }, 1000)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getTimeArr()
    this.timeRunner()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    clearInterval(this.timer);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})

运行效果

请添加图片描述

说明:本文样式代码中的nth-of-type只能在WebView渲染模式下正常显示;在Skyline模式下,由于不支持“nth-of-type”,因此“小黑点”渲染不出来,后续Skyline是否支持“nth-of-type”可能只有天知道了!

请添加图片描述

总结

今天完成了三分之二的“翻页时钟”,下一篇博文将记录最后一个部分“动态翻页效果”。


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

相关文章

杨中科 ASP.NET Core 中的依赖注入的使用

ASP.NET CORE中服务注入的地方 1、在ASP.NET Core项目中一般不需要自己创建ServiceCollection、IServiceProvider。在Program.cs的builder.Build()之前向builderServices中注入 2、在Controller中可以通过构造方法注入服 务。 3、演示 新建一个calculator类 注入 新建TestC…

java基础 - 01 java集合框架概述以及Iterable接口和Collection简单介绍

最近在开发过程中&#xff0c;发现自己对java集合的了解已经忘得差不多了&#xff0c;作为开发者&#xff0c;这可不是一件好事哈&#xff0c;之前开始学习java基础的时候&#xff0c;学过一段时间的java集合&#xff0c;但是现在到了工作岗位上的时候&#xff0c;发现自己用到…

阿里云RDMA通信库XRDMA论文详解

RDMA(remote direct memory access)即远端直接内存访问&#xff0c;是一种高性能网络通信技术&#xff0c;具有高带宽、低延迟、无CPU消耗等优点。RDMA相比TCP在性能方面有明显的优势&#xff0c;但在编程复杂度上RDMA verbs却比TCP socket复杂一个数量级。 开源社区和各大云厂…

开发React应用的多语言支持最佳实践

前言 VoerkaI18n是一款非常优秀的全新的开源国际化多语言解决方案&#xff0c;主要特性包括&#xff1a; 全面工程化解决方案&#xff0c;提供初始化、提取文本、自动翻译、编译等工具链支持。符合直觉&#xff0c;不需要手动定义文本Key映射。强大的插值变量格式化器机制&am…

MinIO (五) .net core实现分片上传

开发环境 Win11 vs2022 appsettings.json添加配置项 //minIO配置"MinIO": {//服务器IP"Endpoint": "192.168.xx.xx:9090",//账号"AccessKey": "3xR7i4zs1vLnxxxxxxxx",//密码"SecretKey": "P6bAnyzJm47Ub…

GSP算法在数据挖掘中的应用

文章目录 一&#xff1a;基本概念介绍二&#xff1a;从一个样例入手三 论文中定义的一些细节四&#xff1a;GSP算法五.算法六 源代码及数据集等总结七. 参考文章 一&#xff1a;基本概念介绍 序列模式挖掘&#xff1a;指挖掘相对时间或其他模式出现频率高的模式 序列模式挖掘…

Python面试题(基础篇)

题目001: 在Python中如何实现单例模式。 点评&#xff1a;单例模式是指让一个类只能创建出唯一的实例&#xff0c;这个题目在面试中出现的频率极高&#xff0c;因为它考察的不仅仅是单例模式&#xff0c;更是对Python语言到底掌握到何种程度&#xff0c;建议大家用装饰器和元类…

uniapp自定义顶部导航并解决打包成apk后getMenuButtonBoundingClientRect方法失效问题

需求&#xff1a;要在app上的顶部导航提示哪里添加一些东西进去&#xff0c;用uniapp自带的肯定不行啊&#xff0c;所以自定义了所有的页面的顶部导航&#xff0c;之后自定义后用手机调试发现 uni.getMenuButtonBoundingClientRect()这个方法的top获取不到....网上找了很多种方…