nbugs commited on
Commit
7d11307
·
verified ·
1 Parent(s): 8756a89

Create static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +74 -0
static/script.js ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ document.addEventListener('DOMContentLoaded', function() {
2
+ const convertForm = document.getElementById('convertForm');
3
+ const resultCard = document.getElementById('resultCard');
4
+ const convertResult = document.getElementById('convertResult');
5
+ const copyBtn = document.getElementById('copyBtn');
6
+ const toast = document.getElementById('toast');
7
+
8
+ // 表单提交事件
9
+ convertForm.addEventListener('submit', function(e) {
10
+ e.preventDefault();
11
+
12
+ // 获取表单数据
13
+ const formData = new FormData(convertForm);
14
+
15
+ // 显示加载提示
16
+ showToast('正在处理,请稍候...');
17
+
18
+ // 发送 AJAX 请求
19
+ fetch('/convert', {
20
+ method: 'POST',
21
+ body: formData
22
+ })
23
+ .then(response => response.json())
24
+ .then(data => {
25
+ if (data.status === 'success') {
26
+ // 显示结果
27
+ convertResult.value = data.result;
28
+ resultCard.style.display = 'block';
29
+
30
+ // 滚动到结果区域
31
+ resultCard.scrollIntoView({ behavior: 'smooth' });
32
+ } else {
33
+ showToast('错误: ' + data.message, 'error');
34
+ }
35
+ })
36
+ .catch(error => {
37
+ showToast('请求失败: ' + error, 'error');
38
+ });
39
+ });
40
+
41
+ // 复制按钮事件
42
+ copyBtn.addEventListener('click', function() {
43
+ convertResult.select();
44
+ document.execCommand('copy');
45
+
46
+ // 不选中文本
47
+ window.getSelection().removeAllRanges();
48
+
49
+ showToast('已复制到剪贴板!');
50
+ });
51
+
52
+ // 显示提示消息
53
+ function showToast(message, type = 'info') {
54
+ toast.textContent = message;
55
+ toast.className = 'toast show ' + type;
56
+
57
+ // 3秒后隐藏
58
+ setTimeout(function() {
59
+ toast.className = 'toast';
60
+ }, 3000);
61
+ }
62
+
63
+ // 输入框聚焦动画
64
+ const inputs = document.querySelectorAll('input, textarea, select');
65
+ inputs.forEach(input => {
66
+ input.addEventListener('focus', function() {
67
+ this.parentElement.classList.add('focused');
68
+ });
69
+
70
+ input.addEventListener('blur', function() {
71
+ this.parentElement.classList.remove('focused');
72
+ });
73
+ });
74
+ });