forked from pearmini/guess-word
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.js
73 lines (62 loc) · 1.59 KB
/
init.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
var container = document.querySelector("#word-container > span");
var words = unique(DICT);
var currentIndex = -1;
var font = {
fontSize: 200,
fontFamily: "",
fontWeight: "bold",
fontStyle: "",
fontVariant: "",
}
function getTextFontSize(text) {
var width = measureTextByDOM(text, font);
return window.innerWidth * 0.8 / width * font.fontSize;
}
function updateWord(index) {
var word = words[index];
container.innerHTML = word;
// 自适应字体大小
container.style.fontSize = getTextFontSize(word) + "px";
}
function nextWord() {
currentIndex++;
if (currentIndex >= words.length) {
alert("没有更多的词了~");
return;
}
updateWord(currentIndex);
}
function previousWord() {
currentIndex--;
if (currentIndex < 0) {
alert("已经第一个词了~");
return;
}
updateWord(currentIndex);
}
nextWord();
alert(
"点击屏幕右边切换到下一个词,\n点击屏幕左边切换到上一个词,\n目前一共有" + words.length + "个词语。"
);
var onClickWithDebounce = debounce(function onClick(e) {
e.preventDefault();
if (e.pageX < window.innerWidth / 2) {
previousWord();
} else {
nextWord();
}
});
// 事件,pc 和 mobile
window.addEventListener("click", onClickWithDebounce);
window.addEventListener("touchstart", onClickWithDebounce);
window.addEventListener("gesturestart", function (e) {
e.preventDefault();
});
window.addEventListener("dblclick", function (e) {
e.preventDefault();
})
window.addEventListener("keydown", function(e) {
if (e.key === "Enter") {
document.documentElement.requestFullscreen();
}
});