我正在尝试为当前正在使用Angular创建的应用程序创建模拟时钟。
我需要根据当前时间在时钟上旋转秒/分钟/小时处理程序,但是我不知道如何更新每个处理程序的style.transform
属性。
到目前为止,这是我的代码:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-clock',
templateUrl: './clock.component.html',
styleUrls: ['./clock.component.css']
})
export class ClockComponent implements OnInit {
constructor() { }
ngOnInit() { }
}
body {
margin: 0;
}
.wrapper {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
width: 100%;
font-family: Verdana, Geneva, Tahoma, sans-serif;
}
.clock {
width: 150px;
height: 150px;
background-color: #20b7af;
border: 12px solid white;
box-shadow: 0 0 30px rgb(202, 202, 202);
border-radius: 50%;
position: relative;
}
#sec-hand {
width: 1px;
height: 50%;
background-color: rgb(255, 255, 255);
transform-origin: 50% 80%;
position: absolute;
top: 10%;
left: 50%;
}
#min-hand {
width: 5px;
height: 40%;
background-color: rgb(255, 255, 255);
transform-origin: 50% 80%;
position: absolute;
top: 18%;
left: calc(50% - 1px);
}
#hr-hand {
width: 3px;
height: 25%;
background-color: rgb(255, 255, 255);
transform-origin: 50% 80%;
position: absolute;
top: 30%;
left: calc(50% + -2px);
}
.num {
height: 100%;
position: absolute;
left: calc(50% - 0.5em);
font-size: 10px;
}
.num div {
width: 1em;
line-height: 2em;
color: rgb(255, 255, 255);
text-align: center;
vertical-align: middle;
}
.num1 {
transform: rotate(30deg);
}
.num1 div {
transform: rotate(-30deg);
}
.num2 {
transform: rotate(60deg);
}
.num2 div {
transform: rotate(-60deg);
}
.num3 {
transform: rotate(90deg);
}
.num3 div {
transform: rotate(-90deg);
}
.num4 {
transform: rotate(120deg);
}
.num4 div {
transform: rotate(-120deg);
}
.num5 {
transform: rotate(150deg);
}
.num5 div {
transform: rotate(-150deg);
}
.num6 {
transform: rotate(180deg);
}
.num6 div {
transform: rotate(-180deg);
}
.num7 {
transform: rotate(210deg);
}
.num7 div {
transform: rotate(-210deg);
}
.num8 {
transform: rotate(240deg);
}
.num8 div {
transform: rotate(-240deg);
}
.num9 {
transform: rotate(270deg);
}
.num9 div {
transform: rotate(-270deg);
}
.num10 {
transform: rotate(300deg);
}
.num10 div {
transform: rotate(-300deg);
}
.num11 {
transform: rotate(330deg);
}
.num11 div {
transform: rotate(-330deg);
}
.num12 {
transform: rotate(0deg);
}
.num12 div {
transform: rotate(-0deg);
}
.as-console-wrapper {
display: none !important;
}
<div class="wrapper">
<div class="clock" #clock>
<div class="num num1">
<div>1</div>
</div>
<div class="num num2">
<div>2</div>
</div>
<div class="num num3">
<div>3</div>
</div>
<div class="num num4">
<div>4</div>
</div>
<div class="num num5">
<div>5</div>
</div>
<div class="num num6">
<div>6</div>
</div>
<div class="num num7">
<div>7</div>
</div>
<div class="num num8">
<div>8</div>
</div>
<div class="num num9">
<div>9</div>
</div>
<div class="num num10">
<div>10</div>
</div>
<div class="num num11">
<div>11</div>
</div>
<div class="num num12">
<div>12</div>
</div>
<div id="hr-hand"></div>
<div id="min-hand"></div>
<div id="sec-hand"></div>
</div>
</div>
您可以在sec-hand
等不同元素上使用模板变量,然后在您的组件中获取对它的引用:
<div id="sec-hand" #secHand></div>
然后在组件中使用@ViewChild
获取参考:
@ViewChild('secHand') el: ElementRef;
现在您可以访问DOM元素(请参见Stackblitz中的工作示例)。该示例仅添加了一个类,因此您需要检查docs以完成转换所需的内容,例如setStyle
。
import { Component, AfterViewInit, ViewChild, Renderer2, ElementRef } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent implements AfterViewInit {
@ViewChild('secHand') el: ElementRef;
constructor(private renderer: Renderer2) {}
ngAfterViewInit() {
this.renderer.addClass(this.el.nativeElement, 'example');
}
}
您需要在三只手中加refs
,因此在您的HTML模板中:
refs
然后使用<div id="hr-hand" #hrHand></div>
<div id="min-hand" #minHand></div>
<div id="sec-hand" #secHand></div>
在组件中声明并初始化它们,并使用它们直接更新您需要的@ViewChild
属性,或者甚至更好地使用@ViewChild
渲染器来更新它们,这使您的应用程序更安全地抵抗XSS攻击:
transform: rotate(...)
另一种选择可能是在它们上添加Renderer2
并在组件上每只手的角度添加3个属性。