是否是长按按钮 判断

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>


相关推荐
不坑老师9 分钟前
不坑盒子的插入网页功能是完全免费的!
前端·html
Wang's Blog13 分钟前
前端FAQ: 描述⼀下你最近使⽤过的前端框架,并解释为何选择它们?
前端·vue.js·faq
wgego13 分钟前
做题笔记BUU (XSS-Lab)(1-14)
前端·笔记·xss
dllxhcjla28 分钟前
css第二天
java·前端·css
callmeSoon30 分钟前
Solid 初探:启发 Vue Vapor 的极致框架
vue.js·前端框架·响应式设计
远航_31 分钟前
10 个被严重低估的 JS 特性,直接少写 500 行代码
前端·javascript
起这个名字32 分钟前
感觉这篇 DeepSeek 给出的微前端实践很牛!轻喷!
前端
小高00732 分钟前
当前端面临百万级 API 请求:从"修 CSS 的"到架构师的进化之路
前端·javascript·面试
P7Dreamer33 分钟前
package.json:你以为只是配置文件,其实是项目的命门!
前端·node.js
BBB努力学习程序设计34 分钟前
让数据阅读更轻松:隔行换色表格的妙用
前端·html