mehaotian-numled.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <template>
  2. <view class="wrap">
  3. <view class="box" :class="{'circle':item === ':','clear':item === ':'&&!close,'active':item === ':'&&twinkle}"
  4. v-for="(item,index) in list" :key="index">
  5. <block v-if="item === ':'">
  6. <view v-if="close" class="circle-box "></view>
  7. <view v-if="close" class="circle-box "></view>
  8. </block>
  9. <block v-else>
  10. <view :class="{'box-top':idx === 0 , 'box-bottom':idx === 1 }" v-for="(num , idx) in 2" :key="idx">
  11. <view v-if="idx === 0" class="box-top-top" :class="{'active':timeList[item][0]}"></view>
  12. <view class="box-top-right" :class="{'active':timeList[item][idx===0 ?1:4]}"></view>
  13. <view class="box-top-bottom" :class="{'active':timeList[item][idx===0 ?2:5]}"></view>
  14. <view class="box-top-left " :class="{'active':timeList[item][idx===0 ?3:6]}"></view>
  15. </view>
  16. </block>
  17. </view>
  18. </view>
  19. </template>
  20. <script>
  21. export default {
  22. props: {
  23. /**
  24. * 模式
  25. * 1. 时间模式 : 正常模式 : 显示十、分、秒
  26. * 2. 时间模式 : 不显示秒
  27. * 3. 时间记录模式
  28. * 4. 自定义模式 ,随意输入数字数组
  29. */
  30. mode: {
  31. type: Number,
  32. default: 1
  33. },
  34. /**
  35. * 是否开启间隔符号
  36. */
  37. close: {
  38. type: Boolean,
  39. default: true
  40. },
  41. /**
  42. * 间隔符号是否闪烁
  43. */
  44. twinkle: {
  45. type: Boolean,
  46. default: false
  47. },
  48. valArr: {
  49. type: Array,
  50. default: () => {
  51. return []
  52. }
  53. },
  54. },
  55. data() {
  56. return {
  57. // 定义时间显示规律
  58. timeList: {
  59. '1': [false, true, false, false, true, false, false],
  60. '2': [true, true, true, false, false, true, true],
  61. '3': [true, true, true, false, true, true, false],
  62. '4': [false, true, true, true, true, false, false],
  63. '5': [true, false, true, true, true, true, false],
  64. '6': [true, false, true, true, true, true, true],
  65. '7': [true, true, false, false, true, false, false],
  66. '8': [true, true, true, true, true, true, true],
  67. '9': [true, true, true, true, true, true, false],
  68. '0': [true, true, false, true, true, true, true]
  69. },
  70. list: [], // 显示时间
  71. timer: null,
  72. state: 0, //0停止 1启动
  73. };
  74. },
  75. created() {
  76. // 显示模式1 初始化
  77. // if (this.mode < 3) {
  78. // clearInterval(this.timer);
  79. // this.list = this.strToArr(this.Format(new Date()), this.mode);
  80. // this.timer = setInterval(() => {
  81. // this.list = this.strToArr(this.Format(new Date()), this.mode);
  82. // }, 1000);
  83. // }
  84. if (this.mode === 3) {
  85. this.list = [0, 0, ':', 0, 0];
  86. this.state && this.restart();
  87. }
  88. if (this.mode === 4) {
  89. this.list = this.valArr
  90. }
  91. },
  92. watch: {
  93. valArr(newVal, oldVal) {
  94. this.list = newVal
  95. }
  96. },
  97. beforeDestroy() {
  98. clearInterval(this.timer);
  99. },
  100. methods: {
  101. recover(){
  102. this.state = 1;
  103. },
  104. pause() {
  105. this.state = 0;
  106. },
  107. /**
  108. * 计时器
  109. */
  110. restart() {
  111. this.list = [0, 0, ':', 0, 0];
  112. let num = 0;
  113. this.state = 1;
  114. clearInterval(this.timer);
  115. this.timer = setInterval(() => {
  116. if (this.state == 0) {
  117. return;
  118. }
  119. num++;
  120. // 倒计时
  121. if (num > 9) {
  122. this.$set(this.list, 4, 0);
  123. num = this.list[3];
  124. num++;
  125. this.$set(this.list, 3, num);
  126. if (num > 5) {
  127. this.$set(this.list, 3, 0);
  128. num = this.list[1];
  129. num++;
  130. this.$set(this.list, 1, num);
  131. if (num > 9) {
  132. this.$set(this.list, 1, 0);
  133. num = this.list[0];
  134. num++;
  135. this.$set(this.list, 0, num);
  136. if (num > 5) {
  137. this.$set(this.list, 0, 0);
  138. // num = this.list[1];
  139. // num++;
  140. // this.$set(this.list, 1, num);
  141. // if (num > 9) {
  142. // this.$set(this.list, 1, 0);
  143. // num = this.list[0];
  144. // num++;
  145. // this.$set(this.list, 0, num);
  146. // if (num > 5) {
  147. // this.$set(this.list, 0, 0);
  148. // // 如果时间到底,不循环就打开这段代码
  149. // // clearInterval(this.timer);
  150. // }
  151. // }
  152. }
  153. }
  154. }
  155. num = 0;
  156. }
  157. this.$set(this.list, 4, num);
  158. }, 1000);
  159. },
  160. /**
  161. * 时间格式化
  162. */
  163. Format(fmt) {
  164. let date = new Date(fmt);
  165. var o = {
  166. h: date.getHours(), //小时
  167. m: date.getMinutes(), //分
  168. s: date.getSeconds() //秒
  169. };
  170. // console.log(o.h, o.m, o.s);
  171. return (
  172. (o.h < 10 ? '0' + o.h : o.h) +
  173. ':' +
  174. (o.m < 10 ? '0' + o.m : o.m) +
  175. ':' +
  176. (o.s < 10 ? '0' + o.s : o.s)
  177. );
  178. },
  179. /**
  180. * 字符串转数组
  181. */
  182. strToArr(str, num) {
  183. if (num === 1) {
  184. return str.split('');
  185. }
  186. if (num === 2) {
  187. return [
  188. str.split('')[0],
  189. str.split('')[1],
  190. str.split('')[2],
  191. str.split('')[3],
  192. str.split('')[4]
  193. ];
  194. }
  195. }
  196. }
  197. };
  198. </script>
  199. <style lang="scss" scoped>
  200. @import './mehaotian-numled.scss';
  201. .wrap {
  202. display: flex;
  203. align-items: center;
  204. width: 100%;
  205. box-sizing: border-box;
  206. .content-box {
  207. display: flex;
  208. align-items: center;
  209. width: 100%;
  210. box-sizing: border-box;
  211. }
  212. .box {
  213. display: flex;
  214. flex-direction: column;
  215. justify-content: center;
  216. align-items: center;
  217. width: 100%;
  218. margin: 2%;
  219. box-sizing: border-box;
  220. &.circle {
  221. width: 20%;
  222. .circle-box {
  223. width: 100%;
  224. height: 0;
  225. padding-top: 100%;
  226. background: #FFF;
  227. &:last-child {
  228. margin-top: 200%;
  229. }
  230. }
  231. &.active {
  232. opacity: 0.05;
  233. animation: twinkle 1s infinite;
  234. }
  235. @keyframes twinkle {
  236. 0%,
  237. 100% {
  238. opacity: 0;
  239. }
  240. 50% {
  241. opacity: 1;
  242. }
  243. }
  244. }
  245. &.clear {
  246. display: none;
  247. }
  248. .box-top,
  249. .box-bottom {
  250. position: relative;
  251. width: 100%;
  252. height: 0;
  253. padding-top: 100%;
  254. &.box-top {
  255. .box-top-top {
  256. top: 0;
  257. left: 0;
  258. width: 100%;
  259. height: $border-width;
  260. @include diffstyle($imgtTansverse);
  261. }
  262. .box-top-right {
  263. top: 0;
  264. right: 0;
  265. width: $border-width;
  266. height: 100%;
  267. @include diffstyle($imgVertical);
  268. }
  269. .box-top-bottom {
  270. bottom: 0;
  271. height: $border-width;
  272. width: 100%;
  273. @include diffstyle($imgtTansverse);
  274. }
  275. .box-top-left {
  276. top: 0;
  277. left: 0;
  278. width: $border-width;
  279. height: 100%;
  280. @include diffstyle($imgVertical);
  281. }
  282. }
  283. &.box-bottom {
  284. margin-top: -$border-width;
  285. .box-top-right {
  286. top: 0;
  287. right: 0;
  288. width: $border-width;
  289. height: 100%;
  290. @include diffstyle($imgVertical);
  291. }
  292. .box-top-bottom {
  293. bottom: 0;
  294. height: $border-width;
  295. width: 100%;
  296. @include diffstyle($imgtTansverse);
  297. }
  298. .box-top-left {
  299. top: 0;
  300. left: 0;
  301. width: $border-width;
  302. height: 100%;
  303. @include diffstyle($imgVertical);
  304. }
  305. }
  306. }
  307. }
  308. }
  309. </style>