溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

angular倒計時組件如何使用

發(fā)布時間:2022-03-15 14:34:45 來源:億速云 閱讀:148 作者:iii 欄目:web開發(fā)

這篇文章主要介紹“angular倒計時組件如何使用”的相關(guān)知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強(qiáng),希望這篇“angular倒計時組件如何使用”文章能幫助大家解決問題。

組件countdown.html代碼

<div class="count-down"> <div class="title"> <h5> {{title}} </h5> </div> <div class="body"> <div class="content"> <div class="top"> {{hour}} </div> <div class="bottom"> 小時 </div> </div> <div class="content"> <div class="top"> {{minute}} </div> <div class="bottom"> 分鐘 </div> </div> <div class="content"> <div class="top"> {{second}} </div> <div class="bottom"> 秒 </div> </div> </div></div>

組件countdown.scss代碼

.count-down{ width:100%; height:100px; background: rgba(0,0,0,0.5); padding: 2px 0; .body{ margin-top: 8px; .content{ width:29%; float: left; margin: 0 2%; .top{ font-size: 20px;; line-height: 30px; color: black; background: white; border-bottom: 2px solid black; } .bottom{ font-size: 14px; line-height: 20px; background: grey; } } }}

組件countdown.component.ts代碼

import { Component, OnInit, Input, OnDestroy, AfterViewInit } from '@angular/core';@Component({ selector: 'roy-countdown', templateUrl: './countdown.component.html', styleUrls: ['./countdown.component.scss']})export class CountdownComponent implements AfterViewInit, OnDestroy { // 父組件傳遞截止日期 @Input() endDate: number; // 父組件傳遞標(biāo)題 @Input() title: string; // 小時差 private hour: number; // 分鐘差 private minute: number; // 秒數(shù)差 private second: number; // 時間差 private _diff: number; private get diff() { return this._diff; } private set diff(val) { this._diff = Math.floor(val / 1000); this.hour = Math.floor(this._diff / 3600); this.minute = Math.floor((this._diff % 3600) / 60); this.second = (this._diff % 3600) % 60; } // 定時器 private timer; // 每一秒更新時間差 ngAfterViewInit() { this.timer = setInterval(() => { this.diff = this.endDate - Date.now(); }, 1000); } // 銷毀組件時清除定時器 ngOnDestroy() { if (this.timer) { clearInterval(this.timer); } }}

使用方法demo.html

<roy-countdown title="距離考試還有:" [endDate]="endDate"></roy-countdown>

關(guān)于“angular倒計時組件如何使用”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識,可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會為大家更新不同的知識點。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI