-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshowPic.js
More file actions
111 lines (86 loc) · 5.35 KB
/
showPic.js
File metadata and controls
111 lines (86 loc) · 5.35 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
function showPic(whichPic) {
if (!document.getElementById('placeholder')) {
return false;
}
const source = whichPic.getAttribute('href');
const placeholder = document.getElementById('placeholder');
if (placeholder.nodeName !== 'IMG') {
return false;
}
placeholder.setAttribute('src', source);
if (document.getElementById('description')) {
const titleContent = whichPic.getAttribute('title');
const text = titleContent ? titleContent : '';
const description = document.getElementById('description');
if (description.nodeType === 3) {
description.firstChild.nodeValue = text;
}
}
return true;
}
function preparePlaceholder() {
if (!document.createElement) {
return false
}
if (!document.createTextNode) {
return false
}
if (!document.getElementById) {
return false
}
if (!document.getElementById('imageGallery')) {
return false
}
const placeholder = document.createElement('img');
const description = document.createElement('p');
const descText = document.createTextNode('Choose an image');
placeholder.setAttribute('id', 'placeholder');
placeholder.setAttribute('src', 'image/dust.jpg');
placeholder.setAttribute('alt', 'my image gallery');
description.setAttribute('id', 'description');
description.appendChild(descText);
const gallery = document.getElementById('imageGallery');
insertAfter(placeholder, gallery);
insertAfter(description, placeholder);
}
function prepareGallery() {
if (!document.getElementsByTagName) {
return false;
}
if (!document.getElementById) {
return false;
}
if (!document.getElementById('imageGallery')) {
return false;
}
const gallery = document.getElementById('imageGallery');
const nodeList = gallery.getElementsByTagName('a');
const len = nodeList.length;
for (let i = 0; i < len; i++) {
const node = nodeList[i];
node.onclick = function () {
return !showPic(this);
};
}
}
function insertAfter(newElement, targetElement) {
const parent = targetElement.parentNode;
if (parent.lastChild === targetElement) {
parent.appendChild(newElement);
} else {
parent.insertBefore(newElement, targetElement.nextSibling);
}
}
function addLoadEvent(func) {
const oldOnLoad = window.onload;
if (typeof oldOnLoad != 'function') {
window.onload = func;
} else {
window.onload = function () {
oldOnLoad();
func();
}
}
}
addLoadEvent(preparePlaceholder);
addLoadEvent(prepareGallery);