IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> JavaScript知识库 -> 桌面时钟(JS版本)-electron -> 正文阅读

[JavaScript知识库]桌面时钟(JS版本)-electron

无聊的时候随手写的,实现桌面的时钟,别问我为哈不直接看电脑屏幕时间,因为好看啊

适用于Mac以及win

?下面直接上代码

(结构目录)

clock.html:时钟界面绘制

clock.css:css部分

index.html:引入

main.js:配置文件

package.json:依赖

favicon.ico:桌面图标

第一部分:用jQuery以及基础js语法绘制环形页面

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>jquery环形进度条</title>
		<script src="./assets/js/jquery-3.2.1.js"></script>
		<link href="./assets/css/clock.css" rel="stylesheet" type="text/css" />
	</head>
	<body>
		<div class="circle-wraps" >
			<div class="circle-wrap">
				<div class="circle">
					<div class="pie-left">
						<div class="left"></div>
					</div>
					<div class="pie-right">
						<div class="right"></div>
					</div>
					<div class="mask"><span></span></div>
				</div>
			</div>
			<div class="circle-wrap_m">
				<div class="circle">
					<div class="pie-left">
						<div class="left"></div>
					</div>
					<div class="pie-right">
						<div class="right"></div>
					</div>
				</div>
			</div>

			<div class="circle-wrap_h">
				<div class="circle">
					<div class="pie-left">
						<div class="left"></div>
					</div>
					<div class="pie-right">
						<div class="right"></div>
					</div>
				</div>
			</div>

			<div class="circle-wrap_y">
				<div class="circle">
					<div class="pie-left">
						<div class="left"></div>
					</div>
					<div class="pie-right">
						<div class="right"></div>
					</div>
				</div>
			</div>

		</div>

		<script>
			$(function() {
				var leftContent_s = document.querySelector(".circle-wrap .left");
				var rightContent_s = document.querySelector(".circle-wrap .right");
				var leftContent_m = document.querySelector(".circle-wrap_m .left");
				var rightContent_m = document.querySelector(".circle-wrap_m .right");
				var leftContent_h = document.querySelector(".circle-wrap_h .left");
				var rightContent_h = document.querySelector(".circle-wrap_h .right");
				var leftContent_y = document.querySelector(".circle-wrap_y .left");
				var rightContent_y = document.querySelector(".circle-wrap_y .right");
				var textCircle = document.querySelector(".mask");
				var monthArr = ['', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'July', 'Aug', 'Sept', 'Oct', 'Nov',
					'Dec'
				]
				var weekArr = ['', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
				//先是leftContent旋转角度从0增加到180度,
				//然后是rightContent旋转角度从0增加到180度
				var angle_s = 0;
				var angle_m = 0;
				var angle_h = 0;
				var angle_y = 0;

				var timerId = setInterval(function() {
					var myDate = new Date();
					var month = myDate.getMonth() + 1; //获取当前月
					var week = myDate.getDay();
					var date = myDate.getDate(); //获取当前日
					
					var s = myDate.getSeconds();

					angle_y = parseInt(month) * 30;
					angle_h = parseInt(myDate.getHours()) * 15; //获取当前小时数(0-23)
					angle_m = parseInt(myDate.getMinutes()) * 6; //获取当前分钟数(0-59)
					angle_s = parseInt(getNow(s)) * 6;
					var noon = 'AM';
					if(myDate.getHours()>12){
						noon = 'PM';
					}else{
						noon = 'AM';
					}
					now = weekArr[week] + '<br>' + monthArr[month] + ' ' + date + '<br><p style="margin:0;margin-top:10px;font-size:20px;line-height:24px">' + getNow(myDate.getHours()) +':'+ getNow(myDate.getMinutes()) +':'+ getNow(s)+'</p><br>'+noon;
					if (angle_s > 360) {
						clearInterval(timerId);
					} else {
						console.log(myDate.getHours());
						if (angle_s == 0) {
							angle_s = 360;
						}
						if (angle_s > 180) {
							leftContent_s.setAttribute('style', 'transform: rotate(' + (angle_s) + 'deg)');
						} else {
							rightContent_s.setAttribute('style', 'transform: rotate(' + (angle_s + 180) +
								'deg)');
							leftContent_s.setAttribute('style', 'transform: rotate(180deg)');
						}

						if (angle_m == 0) {
							angle_m = 360;
						}
						if (angle_m > 180) {
							leftContent_m.setAttribute('style', 'transform: rotate(' + (angle_m) + 'deg)');

						} else {
							rightContent_m.setAttribute('style', 'transform: rotate(' + (angle_m + 180) +
								'deg)');
							leftContent_m.setAttribute('style', 'transform: rotate(180deg)');
						}

						if (angle_h == 0) {
							angle_h = 360;
						}
						if (angle_h > 180) {
							leftContent_h.setAttribute('style', 'transform: rotate(' + (angle_h) + 'deg)');

						} else {
							rightContent_h.setAttribute('style', 'transform: rotate(' + (angle_h + 180) +
								'deg)');
							leftContent_h.setAttribute('style', 'transform: rotate(180deg)');
						}

						if (angle_y == 0) {
							angle_y = 360;
						}
						if (angle_y > 180) {
							leftContent_y.setAttribute('style', 'transform: rotate(' + (angle_y) + 'deg)');

						} else {
							rightContent_y.setAttribute('style', 'transform: rotate(' + (angle_y + 180) +
								'deg)');
							leftContent_y.setAttribute('style', 'transform: rotate(180deg)');
						}
						setPercent(now);
					}
				}, 1000);

				function setPercent(text) {
					textCircle.innerHTML = text;
				}

				function getNow(s) {
					return s < 10 ? '0' + s : s;
				}
			});
		</script>
	</body>
</html>
body {
 	background: transparent;
 }

 .platelet {
 	-webkit-app-region: drag;
 	position: fixed;
 	bottom: 0;
 	left: 10;
 	z-index: 1;
 	font-size: 0;
 	transition: all 0.3s ease-in-out;
 	-webkit-transform: translateY(3px);
 	transform: translateY(3px);
 }

 .platelet:hover {
 	-webkit-transform: translateY(0);
 	transform: translateY(0);
 }

 @media (max-width: 768px) {
 	.platelet {
 		display: none;
 	}
 }

 .circle {
 	width: 180px;
 	height: 180px;
 	position: absolute;
 	border-radius: 50%;
 	border: 10px solid rgba(39, 19, 54, 0.8);
 }

 .pie-left,
 .pie-right {
 	width: 200px;
 	height: 200px;
 	position: absolute;
 	left: -10px;
 	right: -10px;
 	top: -10px;
 }

 .left,
 .right {
 	display: block;
 	width: 180px;
 	height: 180px;
 	border-radius: 50%;
 	border: 10px solid rgba(168, 0, 241, 1);
 	position: absolute;
 	left: 0px;
 	top: 0px;
 }

 .pie-right,
 .right {
 	clip: rect(0, auto, auto, 100px);
 }

 .pie-left,
 .left {
 	clip: rect(0, 100px, auto, 0);
 }

 .mask {
 	width: 200px;
 	height: 200px;
 	border-radius: 50%;
 	position: absolute;
 	left: -10px;
 	top: -10px;
 	background: transparent;
 	display: flex;
 	align-items: center;
 	justify-content: center;
 	text-align: center;
 	font-size: 8px;
 	color: rgba(255, 255, 255, 0.8);
 	line-height: 10px;
 	font-family: 'BlinkMacSystemFont', "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
 	flex-direction: column;
 }

 .circle-wraps {
 	width: 200px;
 	height: 200px;
 	position: relative;
 	margin: 0 auto;
 	display: flex;
 	align-items: center;
 	justify-content: center;
 }

 .circle-wrap_m {
 	width: 155px;
 	height: 155px;
 	position: absolute;
 	top: 11.5px;
 	left: 11.5px;
 }

 .circle-wrap_m .circle {
 	width: 155px;
 	height: 155px;
 	position: absolute;
 	border-radius: 50%;
 	border: 10px solid rgba(20, 35, 54, 0.8);
 }

 .circle-wrap_m .pie-left,
 .circle-wrap_m .pie-right {
 	width: 175px;
 	height: 175px;
 	left: -10px;
 	right: -10px;
 	top: -10px;
 }

 .circle-wrap_m .left,
 .circle-wrap_m .right {
 	display: block;
 	width: 155px;
 	height: 155px;
 	border-radius: 50%;
 	border: 10px solid rgba(30, 90, 240, 1);
 	position: absolute;
 	left: 0px;
 	top: 0px;
 }

 .circle-wrap_m .pie-right,
 .circle-wrap_m .right {
 	clip: rect(0, auto, auto, 87.5px);
 }

 .circle-wrap_m .pie-left,
 .circle-wrap_m .left {
 	clip: rect(0, 87.5px, auto, 0);
 }

 .circle-wrap {
 	width: 200px;
 	height: 200px;
 	position: absolute;
 }

 .circle-wrap_h {
 	width: 115px;
 	height: 115px;
 	position: absolute;
 	top: 25px;
 	left: 25px;
 }

 .circle-wrap_h .circle {
 	width: 130px;
 	height: 130px;
 	position: absolute;
 	border-radius: 50%;
 	border: 10px solid rgba(52, 36, 23, 0.8);
 }

 .circle-wrap_h .pie-left,
 .circle-wrap_h .pie-right {
 	width: 150px;
 	height: 150px;
 	left: -10px;
 	right: -10px;
 	top: -10px;
 }

 .circle-wrap_h .left,
 .circle-wrap_h .right {
 	display: block;
 	width: 130px;
 	height: 130px;
 	border-radius: 50%;
 	border: 10px solid rgba(239, 172, 29, 1);
 	position: absolute;
 	left: 0px;
 	top: 0px;
 }

 .circle-wrap_h .pie-right,
 .circle-wrap_h .right {
 	clip: rect(0, auto, auto, 75px);
 }

 .circle-wrap_h .pie-left,
 .circle-wrap_h .left {
 	clip: rect(0, 75px, auto, 0);
 }

 .circle-wrap_y {
 	width: 100px;
 	height: 100px;
 	position: absolute;
 	top: 38px;
 	left: 38px;
 }

 .circle-wrap_y .circle {
 	width: 105px;
 	height: 105px;
 	position: absolute;
 	border-radius: 50%;
 	border: 10px solid rgba(52, 20, 40, 0.8);
 }

 .circle-wrap_y .pie-left,
 .circle-wrap_y .pie-right {
 	width: 125px;
 	height: 125px;
 	left: -10px;
 	right: -10px;
 	top: -10px;
 }

 .circle-wrap_y .left,
 .circle-wrap_y .right {
 	display: block;
 	width: 105px;
 	height: 105px;
 	border-radius: 50%;
 	border: 10px solid rgba(238, 0, 77, 1);
 	position: absolute;
 	left: 0px;
 	top: 0px;
 }

 .circle-wrap_y .pie-right,
 .circle-wrap_y .right {
 	clip: rect(0, auto, auto, 62.5px);
 }

 .circle-wrap_y .pie-left,
 .circle-wrap_y .left {
 	clip: rect(0, 62.5px, auto, 0);
 }

第二部分:index.html引入时钟模块?

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width,initial-scale=1">
		<meta http-equiv="X-UA-Compatible" content="ie=edge">
		<link href="./assets/css/clock.css" rel="stylesheet" type="text/css">
		<script>
			delete module.exports;
		</script>
	</head>
	<body>
		<div class="platelet" style="display:block">
			<div style="width:250px;overflow:hidden" class="wid"><iframe frameborder="0" scrolling="no" src="clock.html"
					style="width:300px;height:300px;cursor:pointer" id="canvas"></iframe></div>
		</div>
		<div id="main" class="container"></div>
	</body>
	<script src="./assets/js/jquery-3.2.1.js"></script>
</html>

第三部分:系统配置

const path = require("path");
const {
	app,
	BrowserWindow,
	TouchBar,
	ipcMain,
	screen
} = require("electron");
const {
	TouchBarButton
} = TouchBar;
const MAIN_WIDTH = 320;
const MAIN_HEIGHT = 350;
const spin = new TouchBarButton({
	label: "时钟",
	backgroundColor: "#7851A9",
	click: () => {
		console.log("时钟")
	}
});
let spins = [spin];
const touchBar = new TouchBar({
	items: spins
});
let mainWindow, settingWindow;

function createWindow() {
	const display = screen.getPrimaryDisplay();
	const {
		width,
		height
	} = display.bounds;
	mainWindow = new BrowserWindow({
		width: MAIN_WIDTH,
		height: MAIN_HEIGHT,
		title: "桌面时钟",
		hasShadow: false,
		transparent: true,
		resizable: false,
		frame: false,
		focusable: true,
		alwaysOnTop: true,
		maximizable: false,
		show: false,
		x: width - MAIN_WIDTH,
		y: height - MAIN_HEIGHT,
		webPreferences: {
			devTools: true,
			nodeIntegration: true,
			nodeIntegrationInWorker: true
		}
	});
	mainWindow.on("closed", () => (mainWindow = null));
	mainWindow.loadFile(path.join(__dirname, "index.html"));
	mainWindow.once("ready-to-show", () => {
		mainWindow.show();
		mainWindow.setTouchBar(touchBar)
	})
}
app.on("ready", () => createWindow());
app.on("window-all-closed", () => {
	if (process.platform !== "darwin") {
		app.quit()
	}
});
if (process.platform === "darwin") {
	app.setAboutPanelOptions({
		applicationName: "deskDopClock",
		applicationVersion: app.getVersion(),
		copyright: "2022-4-11",
		credits: "Sun"
	})
}
app.on("activate", () => {
	if (mainWindow === null) {
		createWindow()
	}
});
app.on("second-instance", () => {
	if (mainWindow) {
		if (mainWindow.isMinimized()) mainWindow.restore();
		mainWindow.focus()
	}
});
ipcMain.on("show-setting-window", () => {
	createSettingWindow()
});
ipcMain.on("hide-setting-window", event => {
	settingWindow.hide()
});

第三部分:所含依赖以及打包配置(我就是小太阳,哈哈哈)

{
  "name": "clock",
  "version": "2.0.0",
  "description": "桌面时钟-by_-littleSun",
  "main": "main.js",
  "scripts": {
    "start": "electron .",
    "package": "electron-packager . DeskClock --platform=mas --out=./out --election-version=0.0.1 --overwrite --icon=./favicon.ico",
    "package:win": "electron-packager . DeskClock --platform=win32 --arch=x64 --out=./outWin --election-version=0.0.1 --ignore=node_modules --overwrite --icon=./favicon.ico"
  },
  "keywords": [
    "deskDop",
    "CLOCK"
  ],
  "author": "littleSun",
  "license": "CC0-1.0",
  "devDependencies": {
    "electron": "9.4.4"
  },
  "dependencies": {
    "dark-mode": "^3.0.0",
    "electron-store": "^5.1.1"
  }
}

?放在黑色背景里还阔以吧!

后来我又进行优化,用svg语法重写了这个组件

增加了弧度和渐变色 以及流畅度,资源 看情况下载,或者自己根据JS版本进行

桌面应用程序-时钟svg-Javascript文档类资源-CSDN下载

  JavaScript知识库 最新文章
ES6的相关知识点
react 函数式组件 & react其他一些总结
Vue基础超详细
前端JS也可以连点成线(Vue中运用 AntVG6)
Vue事件处理的基本使用
Vue后台项目的记录 (一)
前后端分离vue跨域,devServer配置proxy代理
TypeScript
初识vuex
vue项目安装包指令收集
上一篇文章      下一篇文章      查看所有文章
加:2022-09-21 00:19:29  更:2022-09-21 00:20:42 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/23 11:04:45-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码