是否是长按按钮 判断

csharp 复制代码
<!DOCTYPE html>
<html>
	<head>
		<style>
			.triangle {
				width: 0;
				height: 0;
				border-style: solid;
				border-width: 50px 100px 50px 0;
				border-color: transparent transparent transparent black;
				border-radius: 5px;
			}
		</style>
	</head>
	<body>

		<div class="triangle"></div>

	</body>
</html>
<!DOCTYPE html>
<html>
	<head>
		<title>长按按钮示例</title>
		<style>
			#myButton {
				padding: 10px 20px;
				font-size: 16px;
				cursor: pointer;
			}

			#progressBarContainer {
				position: relative;
				width: 100px;
				height: 20px;
			}

			#progressBar {
				width: 100%;
				height: 100%;
				background-color: #f0f0f0;
				position: absolute;
			}

			#progress {
				height: 100%;
				background-color: #4CAF50;
				position: absolute;
				top: 0;
			}

			#text {
				position: absolute;
				top: 50%;
				left: 50%;
				transform: translate(-50%, -50%);
				color: #333;
			}
		</style>
	</head>
	<body>
		<button id="myButton">点击或长按我</button>
		<div id="progressBarContainer" style="display: none;">
			<div id="progressBar">
				<div id="progress"></div>
				<div id="text">长按中...</div>
			</div>
		</div>

		<script>
			const button = document.getElementById('myButton');
			const progressBarContainer = document.getElementById('progressBarContainer');
			const progress = document.getElementById('progress');
			const text = document.getElementById('text');

			let pressStartTime = null;
			let feedbackTimer = null;
			const LONG_PRESS_THRESHOLD = 1000; // 长按阈值,单位:毫秒

			function showVisualFeedback() {
				if (!progressBarContainer.style.display || progressBarContainer.style.display === 'none') {
					progressBarContainer.style.display = 'block';
				}
			}

			function hideVisualFeedback() {
				if (progressBarContainer.style.display === 'block') {
					progressBarContainer.style.display = 'none';
				}
			}

			function updateProgress() {
				const currentTime = Date.now();
				const pressDuration = currentTime - pressStartTime;
				const progressPercentage = (pressDuration / LONG_PRESS_THRESHOLD) * 100;

				if (progressPercentage > 100) {
					progress.style.width = '100%';
					text.textContent = '长按触发!';
				} else {
					progress.style.width = `${Math.min(progressPercentage, 100)}%`;
					text.textContent = `按压中!(进度:${Math.min(Math.round(progressPercentage), 100)}%)`;
				}
			}

			function handleLongPress() {
				hideVisualFeedback();
				console.log("长按触发!");
				// 在这里添加长按时需要执行的操作
			}

			function handleClick() {
				console.log("点击触发!");
				// 在这里添加普通点击时需要执行的操作
			}

			button.addEventListener('mousedown', () => {
				pressStartTime = Date.now();
				showVisualFeedback();
				updateProgress(); // 立即显示进度

				feedbackTimer = setInterval(updateProgress, 50); // 每隔50毫秒更新一次进度
			});

			button.addEventListener('mouseup', () => {
				clearInterval(feedbackTimer);
				feedbackTimer = null;

				if (pressStartTime !== null) {
					const currentTime = Date.now();
					const pressDuration = currentTime - pressStartTime;

					if (pressDuration >= LONG_PRESS_THRESHOLD) {
						handleLongPress();
					} else {
						handleClick();
					}
				}

				hideVisualFeedback();
				pressStartTime = null;
			});

			button.addEventListener('mouseleave', () => {
				if (feedbackTimer) {
					clearInterval(feedbackTimer);
					feedbackTimer = null;
				}
				hideVisualFeedback();
				pressStartTime = null;
			});
		</script>
	</body>
</html>


相关推荐
海盗强几秒前
Webpack打包优化
前端·webpack·node.js
星之卡比*2 分钟前
前端面试题---vite和webpack的区别
前端·面试
^^为欢几何^^7 分钟前
npm、pnpm和yarn有什么区别
前端·npm·node.js
前端菜鸟日常14 分钟前
vue2和vue3的按需引入的详细对比通俗易懂
javascript·vue.js·ecmascript
AC-PEACE29 分钟前
Vue 中 MVVM、MVC 和 MVP 模式的区别
前端·vue.js·mvc
播播资源31 分钟前
ChatGPT付费创作系统V3.1.3独立版 WEB端+H5端+小程序端 (DeepSeek高级通道+推理输出格式)安装教程
前端·ai·chatgpt·ai作画·小程序·deepseek·deepseek-v3
冷琴199640 分钟前
基于Python+Vue开发的反诈视频宣传管理系统源代码
开发语言·vue.js·python
zhrb1 小时前
打开Firefox自动打开hao360.hjttif.com标签解决方案
前端·firefox
安大桃子1 小时前
Cesium实现深色地图效果
前端·gis·cesium
程楠楠&M1 小时前
uni-app(位置1)
前端·javascript·uni-app·node.js