Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
wangliang181230 committed May 3, 2024
1 parent b938a61 commit 8d5b06c
Showing 1 changed file with 34 additions and 22 deletions.
56 changes: 34 additions & 22 deletions p/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<div>
<label for="pid">PID:</label>
<input id="pid" type="text"/>
<input id="enter" type="button" value="确定"/>

<label for="count" style="margin-left:20px">图片数:</label>
<span id="count">0</span>
Expand All @@ -31,6 +32,8 @@
const loading = document.getElementById("loading");
const imgs = document.getElementById("imgs");

let curPid = 0;

// 监听回车键
pid.addEventListener('keypress', function (e) {
clearPid();
Expand All @@ -53,8 +56,9 @@
}

if (pid.value !== '') {
loading.style.display = 'block';
createImage(pid.value, 1);
enter();
} else {
pid.value = localStorage.getItem('pid') || '116000000';
}

pid.focus(); // 自动获取焦点
Expand All @@ -80,6 +84,8 @@
pid.focus();
pid.selectionStart = pid.selectionEnd = pid.value.length;
});
// 确定
document.getElementById("enter").onclick = enter;
// 上一张
document.getElementById("prev").onclick = function () {
if (pid.value === '') {
Expand Down Expand Up @@ -109,57 +115,63 @@
pid.selectionStart = pid.selectionEnd = pid.value.length;
}

function createImage (pid, n) {
count.innerHTML = n || 1;
function createImage (id, n) {
if (!(id > 0)) {
return;
}

const img = document.createElement("img");
if (n > 0) {
img.src = `https://pixiv.nl/${pid}-${n}.jpg`;
img.title = `${pid}-${n}`;
if (n > 1) {
img.src = `https://pixiv.nl/${id}-${n}.jpg`;
} else {
img.src = `https://pixiv.nl/${pid}.jpg`;
img.title = pid;
img.src = `https://pixiv.nl/${id}.jpg`;
}
img.title = `${id}-${n}`;
img.height = 400;
img.style.cursor = 'pointer';
img.style.display = 'block';
img.onclick = function () {
window.open(img.src);
};
img.onload = function () {
if (n >= 1) {
createImage(pid, n + 1);
} else {
loading.style.display = 'none';
count.innerHTML = 1;
if (id !== curPid) {
return;
}
count.innerHTML = n;
createImage(id, n + 1);
};
img.onerror = function () {
if (id !== curPid) {
return;
}
loading.style.display = 'none';
if (n > 1) {
imgs.removeChild(img);
loading.style.display = 'none';
count.innerHTML = n - 1;
} else if (n === 1) {
imgs.removeChild(img);
createImage(pid);
} else {
loading.style.display = 'none';
imgs.innerHTML = '图片不存在';
count.innerHTML = 0;
}
};
imgs.appendChild(img);
}

function enter () {
if (pid.value === '') {
return;
}

// 设置参数
location.hash = pid.value;
localStorage.setItem('pid', pid.value);
curPid = pid.value - 0;

// 初始化参数
count.innerHTML = 0; // 重置图片数
imgs.innerHTML = ''; // 清空图片

// 显示loading
loading.style.display = 'block';

createImage(pid.value, 1);
createImage(pid.value - 0, 1);
}
</script>
</body>
Expand Down

0 comments on commit 8d5b06c

Please sign in to comment.