Home Web Front-end JS Tutorial HTML+JS implements a clock with rolling numbers

HTML+JS implements a clock with rolling numbers

Apr 13, 2018 pm 03:31 PM
javascript clock

This time I will bring you HTML JS to implement a rolling digital clock. What are the precautions for implementing a rolling digital clock in HTML JS? The following is a practical case, let's take a look.

The following is the entire code of this rolling clock animation:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />

<title>CSS3+JS滚动数字时钟代码-脚本之家</title>

<style>

body{text-align: center;background-color: #0e141b;color: rgba(224, 230, 235, 0.89);font-family: 'Roboto Condensed', sans-serif;font-weight: 300;overflow: hidden;}

.column,.colon{display: inline-block;vertical-align: top;font-size: 86px;line-height: 86px;}

.column{-webkit-transition: -webkit-transform 300ms;transition: -webkit-transform 300ms;transition: transform 300ms;transition: transform 300ms, -webkit-transform 300ms;}

.colon{-webkit-transition: -webkit-transform 300ms;transition: -webkit-transform 300ms;transition: transform 300ms;transition: transform 300ms, -webkit-transform 300ms;-webkit-transform: translateY(calc(50vh - 43px));transform: translateY(calc(50vh - 43px));}

.colon:after{content: ':';}

.num{-webkit-transition: opacity 500ms, text-shadow 100ms;transition: opacity 500ms, text-shadow 100ms;opacity: 0.025;}

.num.visible{opacity: 1.0;text-shadow: 1px 1px 0px #336699;}

.num.close{opacity: 0.35;}

.num.far{opacity: 0.15;}

.num.distant{opacity: 0.1;}

</style>

</head>

<body>

<p class="column">

 <p class="num">0</p>

 <p class="num">1</p>

 <p class="num">2</p>

</p>

<p class="column">

 <p class="num">0</p>

 <p class="num">1</p>

 <p class="num">2</p>

 <p class="num">3</p>

 <p class="num">4</p>

 <p class="num">5</p>

 <p class="num">6</p>

 <p class="num">7</p>

 <p class="num">8</p>

 <p class="num">9</p>

</p>

<p class="colon"></p>

<p class="column">

 <p class="num">0</p>

 <p class="num">1</p>

 <p class="num">2</p>

 <p class="num">3</p>

 <p class="num">4</p>

 <p class="num">5</p>

</p>

<p class="column">

 <p class="num">0</p>

 <p class="num">1</p>

 <p class="num">2</p>

 <p class="num">3</p>

 <p class="num">4</p>

 <p class="num">5</p>

 <p class="num">6</p>

 <p class="num">7</p>

 <p class="num">8</p>

 <p class="num">9</p>

</p>

<p class="colon"></p>

<p class="column">

 <p class="num">0</p>

 <p class="num">1</p>

 <p class="num">2</p>

 <p class="num">3</p>

 <p class="num">4</p>

 <p class="num">5</p>

</p>

<p class="column">

 <p class="num">0</p>

 <p class="num">1</p>

 <p class="num">2</p>

 <p class="num">3</p>

 <p class="num">4</p>

 <p class="num">5</p>

 <p class="num">6</p>

 <p class="num">7</p>

 <p class="num">8</p>

 <p class="num">9</p>

</p>

<script>

'use strict';

var size = 86;

var columns = Array.from(document.getElementsByClassName('column'));

var d = undefined,

 c = undefined;

var classList = ['visible''close''far''far''distant''distant'];

var use24HourClock = true;

function padClock(p, n) {

    return p + ('0' + n).slice(-2);

}

function getClock() {

    d = new Date();

    return [use24HourClock ? d.getHours() : d.getHours() % 12 || 12, d.getMinutes(), d.getSeconds()].reduce(padClock, '');

}

function getClass(n, i2) {

    return classList.find(function (class_, classIndex) {

        return i2 - classIndex === n || i2 + classIndex === n;

    }) || '';

}

var loop = setInterval(function () {

    c = getClock();

    columns.forEach(function (ele, i) {

        var n = +c[i];

        var offset = -n * size;

        ele.style.transform = 'translateY(calc(50vh + ' + offset + 'px - ' + size / 2 + 'px))';

        Array.from(ele.children).forEach(function (ele2, i2) {

            ele2.className = 'num ' + getClass(n, i2);

        });

    });

}, 200 + Math.E * 10);

</script>

</body>

</html>

Copy after login
Note: The top one is the CSS style content, where you can adjust the color, font, etc.

1

2

3

4

5

6

7

8

9

10

11

12

<style>

body{text-align: center;background-color: #0e141b;color: rgba(224, 230, 235, 0.89);font-family: 'Roboto Condensed', sans-serif;font-weight: 300;overflow: hidden;}

.column,.colon{display: inline-block;vertical-align: top;font-size: 86px;line-height: 86px;}

.column{-webkit-transition: -webkit-transform 300ms;transition: -webkit-transform 300ms;transition: transform 300ms;transition: transform 300ms, -webkit-transform 300ms;}

.colon{-webkit-transition: -webkit-transform 300ms;transition: -webkit-transform 300ms;transition: transform 300ms;transition: transform 300ms, -webkit-transform 300ms;-webkit-transform: translateY(calc(50vh - 43px));transform: translateY(calc(50vh - 43px));}

.colon:after{content: ':';}

.num{-webkit-transition: opacity 500ms, text-shadow 100ms;transition: opacity 500ms, text-shadow 100ms;opacity: 0.025;}

.num.visible{opacity: 1.0;text-shadow: 1px 1px 0px #336699;}

.num.close{opacity: 0.35;}

.num.far{opacity: 0.15;}

.num.distant{opacity: 0.1;}

</style>

Copy after login
JS code is mainly used to control the scrolling effect of animation, and CSS is used to control the size of numbers and other content.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to make the image float in the center in JS

JS implements label scrolling switching

JS implements mouse following special effects

The above is the detailed content of HTML+JS implements a clock with rolling numbers. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

Clock app missing in iPhone: How to fix it Clock app missing in iPhone: How to fix it May 03, 2024 pm 09:19 PM

Is the clock app missing from your phone? The date and time will still appear on your iPhone's status bar. However, without the Clock app, you won’t be able to use world clock, stopwatch, alarm clock, and many other features. Therefore, fixing missing clock app should be at the top of your to-do list. These solutions can help you resolve this issue. Fix 1 – Place the Clock App If you mistakenly removed the Clock app from your home screen, you can put the Clock app back in its place. Step 1 – Unlock your iPhone and start swiping to the left until you reach the App Library page. Step 2 – Next, search for “clock” in the search box. Step 3 – When you see “Clock” below in the search results, press and hold it and

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

See all articles