search
HomeWeb Front-endHTML Tutorialcss3-hand-in-hand transform small clock_html/css_WEB-ITnose

Learning css3ing and transfomr, I suddenly thought of using this to make a small clock. Let’s get started:

  1. Prepare the preliminary work, put the clock dial, hour, minute and second hands, real-time time label The approximate appearance is done, the effect is as shown:

html code is as follows:

<div class="main"> <div id="timeLabel"></div> <div id="hour"></div> <div id="minute"></div> <div id="second"></div> </div>

css code is as follows:

 1  <style>  2  * {  3  margin: 0;  4  padding: 0;  5 }  6  .main {  7  position: relative;  8  margin: 100px auto;  9  width: 300px; 10  height: 300px; 11  border-radius: 300px; 12  border: 1px solid #000; 13  box-shadow:2px 5px; 14 } 15  #timeLabel { 16  position: absolute; 17  background-color:pink; 18  width:100px; 19  height:30px; 20  left:100px; 21  top:180px; 22 } 23 24  #hour { 25  width: 100px; 26  height: 10px; 27  background-color: red; 28  position:absolute; 29  left:150px; 30  top:145px; 31 } 32  #minute { 33  width:120px; 34  height:8px; 35  background-color:blue; 36  position:absolute; 37  left:150px; 38  top:146px; 39 } 40  #second { 41  width: 140px; 42  height: 4px; 43  background-color: green; 44  position: absolute; 45  left: 150px; 46  top: 148px; 47 } 48  </style>

 2. Initialize the default time and dial scale, the effect is as follows:

Changed css code:

 <style>        * {            margin: 0;            padding: 0;        }        .main {            position: relative;            margin: 100px auto;            width: 300px;            height: 300px;            border-radius: 300px;            border: 1px solid #000;            box-shadow: 2px 5px #808080;        }        #timeLabel {            position: absolute;            background-color: pink;            width: 80px;            height: 25px;            left: 110px;            top: 180px;            color: #fff;            line-height: 25px;            text-align: center;        }        #hour {            width: 100px;            height: 10px;            background-color: red;            position: absolute;            left: 150px;            top: 145px;            transform-origin: 0 50%;        }        #minute {            width: 120px;            height: 8px;            background-color: blue;            position: absolute;            left: 150px;            top: 146px;            transform-origin: 0 50%;        }        #second {            width: 140px;            height: 4px;            background-color: green;            position: absolute;            left: 150px;            top: 148px;            transform-origin: 0 50%;        }        .hourPointer, .minuterPointer, .secondPointer {            position: absolute;            transform-origin: 0 50%;        }        .hourPointer {            height: 10px;            width: 12px;            left: 150px;            top: 145px;            background-color: #f00;            z-index:3;        }        .minuterPointer {            height: 8px;            width: 10px;            left: 150px;            top: 146px;            background-color: #b6ff00;            z-index: 2;        }        .secondPointer {            height: 6px;            width: 8px;            left: 150px;            top: 147px;            background-color: #fa8;            z-index: 1;        }    </style>

Initialization js code:

window.onload = function () {            initClock();                  }        var timer = null;        function $(id) {            return document.getElementById(id)        }        function CreateKeDu(pElement, className, deg, translateWidth) {            var Pointer = document.createElement("div");            Pointer.className = className            Pointer.style.transform = "rotate(" + deg + "deg)   translate(" + translateWidth + "px)";            pElement.appendChild(Pointer);        }        function initClock() {            var main = $("biaopan");            var timeLabel = $("timeLabel");            var hour = $("hour");            var minute = $("minute");            var second = $("second");            var now = new Date();            var nowHour = now.getHours();            var nowMinute = now.getMinutes();            var nowSecond = now.getSeconds();            //初始化timeLabel            timeLabel.innerHTML = nowHour + ":" + nowMinute + ":" + nowSecond;            //初始化表盘            for (var index = 0; index < 4; index++) {                CreateKeDu(main, "hourPointer", index * 90, 138);            }            for (var index = 0; index < 12; index++) {                CreateKeDu(main, "minuterPointer",index*30, 140);            }            for (var index = 0; index < 60; index++) {                CreateKeDu(main, "secondPointer", index * 6, 142);            }            //初始化时分秒针            second.style.transform = "rotate(" + (nowSecond * 6 - 90) + "deg)";            minute.style.transform = "rotate(" + (nowMinute * 6 + 1 / 10 * nowSecond - 90) + "deg)";            hour.style.transform = "rotate(" + (nowHour * 30 + 1 / 2 * nowMinute + 1 / 120 * nowSecond - 90) + "deg)";        }

3. Add timer:

The js code is as follows:

 //定时器        function startMove() {            clearInterval(timer);            timer = setInterval(function () {                var now = new Date();                var nowSecond = now.getSeconds();                var nowMinute = now.getMinutes();                var nowHour = now.getHours();                second.style.transform = "rotate(" + (nowSecond * 6 - 90) + "deg)";                minute.style.transform = "rotate(" + (nowMinute * 6 + 1 / 10 * nowSecond - 90) + "deg)";                hour.style.transform = "rotate(" + (nowHour * 30 + 1 / 2 * nowMinute + 1 / 120 * nowSecond - 90) + "deg)";                timeLabel.innerHTML = nowHour + ":" + nowMinute + ":" + nowSecond;            }, 1000);        }

 4. Use OOP method to change:

The modified js code is as follows:

function Clock() {            //定义属性            this.main = this.$("biaopan");            this.timeLabel = this.$("timeLabel");            this.hour = this.$("hour");            this.minute = this.$("minute");            this.second = this.$("second");            this.nowHour = null;            this.nowMinute = null;            this.nowSecond = null;            this.timer = null;            var _this = this;            //初始化函数            var init = function () {                _this.getNowTime();                _this.initClock();                _this.InterVal();            }            init();        }        Clock.prototype.$ = function (id) {            return document.getElementById(id)        }        Clock.prototype.CreateKeDu = function (className, deg, translateWidth) {            var Pointer = document.createElement("div");            Pointer.className = className            Pointer.style.transform = "rotate(" + deg + "deg)   translate(" + translateWidth + "px)";            this.main.appendChild(Pointer);        }        Clock.prototype.getNowTime = function () {            var now = new Date();            this.nowHour = now.getHours();            this.nowMinute = now.getMinutes();            this.nowSecond = now.getSeconds();        }        Clock.prototype.setPosition = function () {            this.second.style.transform = "rotate(" + (this.nowSecond * 6 - 90) + "deg)";            this.minute.style.transform = "rotate(" + (this.nowMinute * 6 + 1 / 10 * this.nowSecond - 90) + "deg)";            this.hour.style.transform = "rotate(" + (this.nowHour * 30 + 1 / 2 * this.nowMinute + 1 / 120 * this.nowSecond - 90) + "deg)";        }        Clock.prototype.initClock = function () {            //初始化timeLabel            this.timeLabel.innerHTML = this.nowHour + ":" + this.nowMinute + ":" + this.nowSecond;            //初始化表盘            for (var index = 0; index < 4; index++) {                this.CreateKeDu("hourPointer", index * 90, 138);            }            for (var index = 0; index < 12; index++) {                this.CreateKeDu("minuterPointer", index * 30, 140);            }            for (var index = 0; index < 60; index++) {                this.CreateKeDu("secondPointer", index * 6, 142);            }            this.setPosition();        }        Clock.prototype.InterVal = function () {            clearInterval(this.timer);            var _this = this;            this.timer = setInterval(function () {                _this.getNowTime();                _this.second.style.transform = "rotate(" + (_this.nowSecond * 6 - 90) + "deg)";                _this.minute.style.transform = "rotate(" + (_this.nowMinute * 6 + 1 / 10 * _this.nowSecond - 90) + "deg)";                _this.hour.style.transform = "rotate(" + (_this.nowHour * 30 + 1 / 2 * _this.nowMinute + 1 / 120 * _this.nowSecond - 90) + "deg)";                _this.timeLabel.innerHTML = _this.nowHour + ":" + _this.nowMinute + ":" + _this.nowSecond;            }, 1000);        }

 

The final call is as follows:

window.onload = function () {            new Clock();        }

Finally Page code:

                <script>        function Clock() {            //定义属性            this.main = this.$(&quot;biaopan&quot;);            this.timeLabel = this.$(&quot;timeLabel&quot;);            this.hour = this.$(&quot;hour&quot;);            this.minute = this.$(&quot;minute&quot;);            this.second = this.$(&quot;second&quot;);            this.nowHour = null;            this.nowMinute = null;            this.nowSecond = null;            this.timer = null;            var _this = this;            //初始化函数            var init = function () {                _this.getNowTime();                _this.initClock();                _this.InterVal();            }            init();        }        Clock.prototype.$ = function (id) {            return document.getElementById(id)        }        Clock.prototype.CreateKeDu = function (className, deg, translateWidth) {            var Pointer = document.createElement(&quot;div&quot;);            Pointer.className = className            Pointer.style.transform = &quot;rotate(&quot; + deg + &quot;deg)   translate(&quot; + translateWidth + &quot;px)&quot;;            this.main.appendChild(Pointer);        }        Clock.prototype.getNowTime = function () {            var now = new Date();            this.nowHour = now.getHours();            this.nowMinute = now.getMinutes();            this.nowSecond = now.getSeconds();        }        Clock.prototype.setPosition = function () {            this.second.style.transform = &quot;rotate(&quot; + (this.nowSecond * 6 - 90) + &quot;deg)&quot;;            this.minute.style.transform = &quot;rotate(&quot; + (this.nowMinute * 6 + 1 / 10 * this.nowSecond - 90) + &quot;deg)&quot;;            this.hour.style.transform = &quot;rotate(&quot; + (this.nowHour * 30 + 1 / 2 * this.nowMinute + 1 / 120 * this.nowSecond - 90) + &quot;deg)&quot;;        }        Clock.prototype.initClock = function () {            //初始化timeLabel            this.timeLabel.innerHTML = this.nowHour + &quot;:&quot; + this.nowMinute + &quot;:&quot; + this.nowSecond;            //初始化表盘            for (var index = 0; index &lt; 4; index++) {                this.CreateKeDu(&quot;hourPointer&quot;, index * 90, 138);            }            for (var index = 0; index &lt; 12; index++) {                this.CreateKeDu(&quot;minuterPointer&quot;, index * 30, 140);            }            for (var index = 0; index &lt; 60; index++) {                this.CreateKeDu(&quot;secondPointer&quot;, index * 6, 142);            }            this.setPosition();        }        Clock.prototype.InterVal = function () {            clearInterval(this.timer);            var _this = this;            this.timer = setInterval(function () {                _this.getNowTime();                _this.second.style.transform = &quot;rotate(&quot; + (_this.nowSecond * 6 - 90) + &quot;deg)&quot;;                _this.minute.style.transform = &quot;rotate(&quot; + (_this.nowMinute * 6 + 1 / 10 * _this.nowSecond - 90) + &quot;deg)&quot;;                _this.hour.style.transform = &quot;rotate(&quot; + (_this.nowHour * 30 + 1 / 2 * _this.nowMinute + 1 / 120 * _this.nowSecond - 90) + &quot;deg)&quot;;                _this.timeLabel.innerHTML = _this.nowHour + &quot;:&quot; + _this.nowMinute + &quot;:&quot; + _this.nowSecond;            }, 1000);        }        window.onload = function () {            new Clock();        }    </script>    


Summary: In this example, the rotation effect of rotate and the displacement effect of translate are used in the transform attribute of css3. Regarding the use of transform, please refer to http://www.w3school.com.cn/cssref/pr_transform.asp

Statement
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
From Text to Websites: The Power of HTMLFrom Text to Websites: The Power of HTMLApr 13, 2025 am 12:07 AM

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

Understanding HTML, CSS, and JavaScript: A Beginner's GuideUnderstanding HTML, CSS, and JavaScript: A Beginner's GuideApr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The Role of HTML: Structuring Web ContentThe Role of HTML: Structuring Web ContentApr 11, 2025 am 12:12 AM

The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.

HTML and Code: A Closer Look at the TerminologyHTML and Code: A Closer Look at the TerminologyApr 10, 2025 am 09:28 AM

HTMLisaspecifictypeofcodefocusedonstructuringwebcontent,while"code"broadlyincludeslanguageslikeJavaScriptandPythonforfunctionality.1)HTMLdefineswebpagestructureusingtags.2)"Code"encompassesawiderrangeoflanguagesforlogicandinteract

HTML, CSS, and JavaScript: Essential Tools for Web DevelopersHTML, CSS, and JavaScript: Essential Tools for Web DevelopersApr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

The Roles of HTML, CSS, and JavaScript: Core ResponsibilitiesThe Roles of HTML, CSS, and JavaScript: Core ResponsibilitiesApr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

Is HTML easy to learn for beginners?Is HTML easy to learn for beginners?Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

What is an example of a starting tag in HTML?What is an example of a starting tag in HTML?Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

See all articles

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 agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft