Angular6封装LED时钟数字组件
一、运行截图
截图1:

截图2:

二、代码
html代码:
<div class="time" >
<ng-container #container> </ng-container>
</div> <ng-template #child_elem>
<div class="digit minutes">
<div class="segment "></div>
<div class="segment "></div>
<div class="segment "></div>
<div class="segment "></div>
<div class="segment "></div>
<div class="segment "></div>
<div class="segment "></div>
</div>
</ng-template> <ng-template #point_elem>
<div class="separator minutes"></div>
</ng-template>
css代码:
.time {
height: 40px;
position: absolute;
top:;
left: 28%;
width: 100px;
margin-left: 0px;
text-align: center;
z-index:;
margin-top: -7px;
}
.time .digit {
width: 16px;
height: 44px;
position: relative;
display: inline-block;
margin-top: 2px;
}
.digit .segment {
background:#11c4fb;
border-radius:2px;
position:absolute;
opacity:0.1;
transition:opacity 0.2s;
-webkit-transition:opacity 0.2s;
-ms-transition:opacity 0.2s;
-moz-transition:opacity 0.2s;
-o-transition:opacity 0.2s;
}
.digit .segment.on, .separator {
opacity:;
background:#11c4fb;
box-shadow:0 0 30px rgba(0,255,0,0.1);
transition:opacity 0s;
-webkit-transition:opacity 0s;
-ms-transition:opacity 0s;
-moz-transition:opacity 0s;
-o-transition:opacity 0s;
}
.time .separator {
width: 4px;
height: 4px;
background: #11c4fb;
border-radius: 50%;
display: inline-block;
position: relative;
bottom: 10px;
}
.digit .segment:nth-child(1) {
top: 10px;
left: 4px;
right: 4px;
height: 2px;
background: #11c4fb;
}
.digit .segment:nth-child(2) {
top: 12px;
right: 1px;
width: 2px;
height: 75px;
height: calc(71% - 21px);
background: #11c4fb;
}
.digit .segment:nth-child(3) {
bottom: 10px;
right: 1px;
width: 2px;
height: 72px;
height: calc(71% - 21px);
background: #11c4fb;
}
.digit .segment:nth-child(4) {
bottom: 8px;
right: 4px;
height: 2px;
left: 4px;
background: #11c4fb;
}
.digit .segment:nth-child(5) {
bottom: 12px;
left: 2px;
width: 2px;
height: 75px;
height: calc(70% - 24px);
background: #11c4fb;
}
.digit .segment:nth-child(6) {
top: 14px;
left: 2px;
width: 2px;
height: 75px;
height: calc(70% - 24px);
background: #11c4fb;
}
.digit .segment:nth-child(7) {
bottom: 95px;
bottom: calc(71% - 11px);
right: 4px;
left: 4px;
height: 2px;
background: #11c4fb;
}
ts代码:
import { Component, OnInit, Input, ViewChild, ViewContainerRef, TemplateRef, ElementRef, QueryList, SimpleChanges } from '@angular/core';
@Component({
selector: 'app-led-clockfont',
templateUrl: './led-clockfont.component.html',
styleUrls: ['./led-clockfont.component.css']
})
export class LedClockfontComponent implements OnInit {
@ViewChild("container", {read: ViewContainerRef}) container_elem: ViewContainerRef;
@ViewChild("child_elem") tpl_elem: TemplateRef<any>;
@ViewChild("point_elem") tpl_point_elem: TemplateRef<any>;
@Input() fontValue:number = 0;
//数值模型数组,0,1,2,3,4,5,6,7,8,9
private digitSegments = [
[1, 2, 3, 4, 5, 6],
[2, 3],
[1, 2, 7, 5, 4],
[1, 2, 7, 3, 4],
[6, 7, 2, 3],
[1, 6, 7, 3, 4],
[1, 6, 5, 4, 3, 7],
[1, 2, 3],
[1, 2, 3, 4, 5, 6, 7],
[1, 2, 7, 3, 6, 4]
]
constructor(private el:ElementRef) {
}
ngOnInit() {
}
ngOnChanges(changes: SimpleChanges): void {
if(parseFloat(this.fontValue.toString())){
this.createDomContainer();
}else{
//Do-nothing
}
// this.createDomContainer();
}
createDomContainer(){
//根据当前数值长度处理DOM容器
if(0 !== this.fontValue.toString().length){
//清空容器内视图
this.container_elem.clear();
//处理容器内视图
let view = null;
let point_index = this.fontValue.toString().indexOf('.');
for(let i=0;i<this.fontValue.toString().length;i++){
if((-1 != point_index) && (i == point_index)){
view = this.tpl_point_elem.createEmbeddedView(null);
this.container_elem.insert(view);
}else{
view = this.tpl_elem.createEmbeddedView(null);
this.container_elem.insert(view);
}
}
this.typeConversionst();
}else{
//Do-nothing
}
}
//截取数字,类型转换
typeConversionst(){
let _minutes = this.el.nativeElement.querySelectorAll('.minutes');
//字符串格式
let fontValue_string = this.fontValue.toString();
//转化成number类型,调用设置数值方法,设置数值
if(0 != _minutes.length){
for(let i=0;i<_minutes.length;i++){
this.setNumber(_minutes[i], parseInt(fontValue_string.slice(i,i+1)), 1);
}
}
}
setNumber(digit, number, on){
let segments = digit.querySelectorAll('.segment');
let current = parseInt(digit.getAttribute('data-value'));
if(!isNaN(number)){
//处理数字
this.digitSegments[number].forEach(function(digitSegment, index) {
setTimeout(function() {
segments[digitSegment - 1].classList.add('on');
}, index * 1)
});
digit.setAttribute('data-value', number);
}else{
//Do-nothing
}
}
}
三、使用
图一使用:
<p *ngIf="selfFontStyle == 'clockStyle' && value != '--'" style="height: 140px;line-height: 120px;text-align: right;font-size: 40px;color: #13b4eb;position: relative;float: right;right: 130px;">
<app-led-clockfont [fontValue]="value" *ngIf="selfFontStyle == 'clockStyle'"></app-led-clockfont>
<!-- value = 40.72 -->
</p>
图二使用:
<app-led-clockfont [fontValue]="mapComponentConfig.length"></app-led-clockfont>
<!-- mapComponentConfig.length = 397 -->
四、说明
利用Angular6创建一个led-clockfont组件,目录结构如下图:

css代码详见第二步 css代码,html代码详见第二步 html代码,ts代码详见第二步 ts代码。
使用部分详见第三步
Angular6封装LED时钟数字组件的更多相关文章
- C#进阶系列——一步一步封装自己的HtmlHelper组件:BootstrapHelper(三:附源码)
前言:之前的两篇封装了一些基础的表单组件,这篇继续来封装几个基于bootstrap的其他组件.和上篇不同的是,这篇的有几个组件需要某些js文件的支持. 本文原创地址:http://www.cnblog ...
- 原生JS面向对象思想封装轮播图组件
原生JS面向对象思想封装轮播图组件 在前端页面开发过程中,页面中的轮播图特效很常见,因此我就想封装一个自己的原生JS的轮播图组件.有了这个需求就开始着手准备了,代码当然是以简洁为目标,轮播图的各个功能 ...
- C#进阶系列——一步一步封装自己的HtmlHelper组件:BootstrapHelper
前言:之前学习过很多的Bootstrap组件,博主就在脑海里构思:是否可以封装一套自己Bootstrap组件库呢.再加上看到MVC的Razor语法里面直接通过后台方法输出前端控件的方式,于是打算仿照H ...
- JS组件系列——封装自己的JS组件,你也可以
前言:之前分享了那么多bootstrap组件的使用经验,这篇博主打算研究下JS组件的扩展和封装,我们来感受下JQuery为我们提供$.Extend的神奇,看看我们怎么自定义自己的组件,比如我们想扩展一 ...
- JS组件系列——分享自己封装的Bootstrap树形组件:jqTree
前言:之前的一篇介绍了下如何封装自己的组件,这篇再次来体验下自己封装组件的乐趣.看过博主博客的园友应该记得之前分享过一篇树形菜单的使用JS组件系列——Bootstrap 树控件使用经验分享,这篇里面第 ...
- JS组件系列——封装自己的JS组件
前言:之前分享了那么多bootstrap组件的使用经验,这篇博主打算研究下JS组件的扩展和封装,我们来感受下JQuery为我们提供$.Extend的神奇,看看我们怎么自定义自己的组件,比如我们想扩展一 ...
- 从零开始教你封装自己的vue组件
组件(component)是vue.js最强大的功能之一,它可以实现功能的复用,以及对其他逻辑的解耦.但经过一段时间的使用,我发现自己并没有在业务中发挥出组件的最大价值.相信很多刚开始使用vue的朋友 ...
- Android Widget小组件开发(一)——Android实现时钟Widget组件的步骤开发,这些知识也是必不可少的!
Android Widget小组件开发(一)--Android实现时钟Widget组件的步骤开发,这些知识也是必不可少的! PS:学习自某网站(不打广告) 这个小组件相信大家都很熟悉吧,以前的墨迹天气 ...
- vue和echarts 封装的 v-charts 图表组件
https://v-charts.js.org/#/ 在使用 echarts 生成图表时,经常需要做繁琐的数据类型转化.修改复杂的配置项,v-charts 的出现正是为了解决这个痛点.基于 Vue2. ...
随机推荐
- html 基本用法
html表单表格基本用法,直接贴代码. <html> <head> <title>html基础</title> </head> </b ...
- [ 9.24 ]CF每日一题系列—— 468A构造递推
Description: 1 - n个数问你能否经过加减乘除这些运算n -1次的操作得到24 Solutrion: 一开始想暴力递推,发现n的范围太大直接否决,也否决了我的跑dfs,后来就像肯定有个递 ...
- 连接SSH服务器的脚本,自动发送用户名和密码
利用expect 自动输入用户名和密码 脚本如下 #!/usr/bin/expect # connect ssh server set timeout 30 spawn ssh -l user_nam ...
- C#实现录音录像录屏源码
以前写过两篇录音和录像的文章(实现语音视频录制.在服务器端录制语音视频),最近有朋友问,如果要实现屏幕录制这样的功能,该怎么做了?实际上录屏的原理跟录音.录像是差不多的,如果了解了我前面两篇文章中介绍 ...
- js验证身份证号,超准确
js验证身份证号,超准确 看程序前先来了解下身份证号的构成:身份证号分为两种,旧的为15位,新的为18位.身份证15位编码规则:dddddd yymmdd xx p 其中 dddddd:地区码 ...
- [CocoaPods]客户端加载第三方库
请先阅读另一篇博文铺垫知识基础:[CocoaPods]终端方式集成第三方库 客户端的Github地址:CocoaPods-app 点击下载客户端: [CocoaPods客户端] 安装下载的文件.软件界 ...
- Linux - 日志文件简介
Linux日志文件绝大多数存放在/var/log目录,其中一些日志文件由应用程序创建,其他的则通过syslog来创建. Linux系统日志文件通过syslog守护程序在syslog套接字/dev/lo ...
- hdu 6127---Hard challenge(思维)
题目链接 Problem Description There are n points on the plane, and the ith points has a value vali, and i ...
- Eclipse建立Java工程中的三个JRE选项的区别(Use an execution environment JRE,Use a project specific JRE,Use default JRE)
本博客部分转载自: http://blog.csdn.net/wdjhzw/article/details/42086615 这篇博客写的非常好,很用心. 一.首先看新建Java Project时候 ...
- Log4Net使用详解2
首先说明一点的是,log4net解决的问题是提供一个记录日志的框架,它提供了向多种目标写入的实现,比如利用log4net可以方便地将日志信息记录到文件.控制台.Windows事件日志和数据库(包括MS ...