-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
176 lines (143 loc) · 5.29 KB
/
index.html
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Searcher</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" integrity="sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<style>
body {
background: black;
color: white;
}
.row {
margin: 0 auto;
text-align: center;
}
.row-1 {
width: 50%;
}
input, button, select {
background: black;
color: white;
margin-bottom: 15px;
}
img {
margin: 15px;
box-shadow:
inset 0 0 30px whitesmoke,
inset 10px 0 40px #f0f,
inset -10px 0 40px #0ff,
inset 10px 0 100px #f0f,
inset -10px 0 100px #0ff,
0 0 20px #fff,
-10px 0 40px #f0f,
10px 0 40px #0ff;
transition: all 0.3s;
}
img:hover {
transform: scale(2);
}
#searchBtn {
padding: 5px;
border: 1px solid white;
}
.search-box {
width: 90%;
height: 50px;
}
.search-section {
display: flex;
justify-content: center;
align-items: center;
}
.randomize {
width: 10%;
background: none;
margin: 0;
}
.material-symbols-outlined {
height: 30px;
}
</style>
</head>
<body>
<pre><code id="code"></code></pre>
<div class="container">
<div class="row row-1">
<h1>Search for any images</h1>
<div class="search-section">
<input type="text" id="userInput" placeholder="Search images" class="search-box">
<span class="randomize"><button id="randomBtn"><img src="./assets/dice.svg" alt="icons8-dice-30" class="material-symbols-outlined"></button></span>
</div>
<label for="imgNumber">No. of images</label>
<select name="number" id="imgNumber">
<option value="1">1</option>
<option value="5">5</option>
<option value="10">10</option>
<option value="15">15</option>
<option selected="selected" value="20">20</option>
</select>
<button id="searchBtn">Search</button>
</div>
<div class="row">
<div class="col-md-12">
<div id="imgContainer"></div>
</div>
</div>
</div>
<script>
$(function(){
// API urls
var flickrApiUrl = "https://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?";
var randomWordUrl = "https://random-word-api.herokuapp.com/word"
// If enter key pressed on the userInput field, click the search button.
$("#userInput").keyup(function(event) {
if (event.keyCode === 13) {
$("#searchBtn").click();
}
});
// On click of the search button, get the value of the user input
$('#searchBtn').click(function() {
$('#imgContainer').empty(); // Empty the current images
var userInput = $('#userInput').val(); // get the value of the user input box
var imgNumber = $('#imgNumber').find(':selected').text(); // get the selected text of the drop down, set the img number to that
console.log(userInput)
$.getJSON(flickrApiUrl, { // Get the json data from api
tags: userInput, // set tag as the user input
tagmode: "any",
format: "json"
}).done(function(data) {
var i = 0;
//success
console.log(data);
$.each(data.items, function(index, item) { // for each data item
//Create img html element, then set src attribute to item.media
var img = $('<img>',
{ id: i + 1,
src: item.media.m
})
img.appendTo($('#imgContainer')) // and attach it to dom
if(index == imgNumber - 1) {
return false;
}
i++
})
}).fail(function() {
//failure
alert("Ajax call failed.")
})
});
$('#randomBtn').click(function() { // On click of random button
$.getJSON(randomWordUrl) // Get the random word from the api
.done(function(randomWord){
$('#userInput').val(randomWord); // set the user input box to that word
})
$('#searchBtn').click(); // Run the search automatically
})
}); // End main function
</script>
</body>
</html>