Skip to content

Commit ebb3185

Browse files
committed
giphysingers
0 parents  commit ebb3185

File tree

3 files changed

+177
-0
lines changed

3 files changed

+177
-0
lines changed

index.html

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<title>Singers</title>
7+
<link rel="stylesheet" type="text/css" href="style.css">
8+
</head>
9+
10+
<body>
11+
12+
<div id="singer-buttons">
13+
14+
</div>
15+
16+
<form id="singer-form">
17+
<label for="singer-input">Add a singers</label>
18+
<input type="text" id="singer-input"><br>
19+
<input id="add-singer" type="submit" value="Submit">
20+
</form>
21+
22+
<div id="singers">
23+
24+
</div>
25+
26+
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
27+
<script src="main.js"></script>
28+
29+
</body>
30+
</html>

main.js

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
$(document).ready(function() {
2+
3+
var singers = [
4+
"rihanna", "eminem", "rammstein", "katty perry", "50 cent", "selena gomez",
5+
"kygo", "avener", "imany", "cardi b", "sam smith",
6+
"jessie j", "crhis brown", "ed sheeran", "adele", "bruno mars",
7+
"taylor swift", "beyonce", "miley cyrus", "justin timberlake", "demi lovato"
8+
];
9+
10+
function populateButtons(arrayToUse, classToAdd, areaToAddTo) {
11+
$(areaToAddTo).empty();
12+
13+
for (var i = 0; i < arrayToUse.length; i++) {
14+
var a = $("<button>");
15+
a.addClass(classToAdd);
16+
a.attr("data-type", arrayToUse[i]);
17+
a.text(arrayToUse[i]);
18+
$(areaToAddTo).append(a);
19+
}
20+
21+
}
22+
23+
$(document).on("click", ".singer-button", function() {
24+
$("#singers").empty();
25+
$(".singer-button").removeClass("active");
26+
$(this).addClass("active");
27+
28+
var type = $(this).attr("data-type");
29+
var queryURL = "http://api.giphy.com/v1/gifs/search?q=" + type + "&api_key=W1O1HdwgIGmQI35GMNFelt3sejuFSRRY";
30+
31+
$.ajax({
32+
url: queryURL,
33+
method: "GET"
34+
})
35+
.then(function(response) {
36+
var results = response.data;
37+
38+
for (var i = 0; i < results.length; i++) {
39+
var singerDiv = $("<div class=\"singer-item\">");
40+
41+
var rating = results[i].rating;
42+
43+
var p = $("<p>").text("Rating: " + rating);
44+
45+
var animated = results[i].images.fixed_height.url;
46+
var still = results[i].images.fixed_height_still.url;
47+
48+
var singerImage = $("<img>");
49+
singerImage.attr("src", still);
50+
singerImage.attr("data-still", still);
51+
singerImage.attr("data-animate", animated);
52+
singerImage.attr("data-state", "still");
53+
singerImage.addClass("singer-image");
54+
55+
singerDiv.append(p);
56+
singerDiv.append(singerImage);
57+
58+
$("#singers").append(singerDiv);
59+
}
60+
});
61+
});
62+
63+
$(document).on("click", ".singer-image", function() {
64+
65+
var state = $(this).attr("data-state");
66+
67+
if (state === "still") {
68+
$(this).attr("src", $(this).attr("data-animate"));
69+
$(this).attr("data-state", "animate");
70+
}
71+
else {
72+
$(this).attr("src", $(this).attr("data-still"));
73+
$(this).attr("data-state", "still");
74+
}
75+
});
76+
77+
$("#add-singer").on("click", function(event) {
78+
event.preventDefault();
79+
var newsinger = $("input").eq(0).val();
80+
81+
if (newsinger.length > 2) {
82+
singers.push(newsinger);
83+
}
84+
85+
populateButtons(singers, "singer-button", "#singer-buttons");
86+
87+
});
88+
89+
populateButtons(singers, "singer-button", "#singer-buttons");
90+
});

style.css

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
body {
2+
font: 18px arial, sans-serif;
3+
background: #FFD700;
4+
}
5+
6+
#singers {
7+
width: 70%;
8+
}
9+
10+
.singer-item {
11+
float: left;
12+
padding: 10px;
13+
}
14+
15+
form {
16+
float: right;
17+
width: 30%;
18+
padding: 20px;
19+
}
20+
21+
label {
22+
display: block;
23+
margin-bottom: 10px;
24+
font-weight: 700;
25+
}
26+
27+
input[type="text"] {
28+
width: 100%;
29+
height: 30px;
30+
margin-bottom: 10px;
31+
font-size: 18px;
32+
}
33+
34+
button,
35+
input[type="submit"] {
36+
padding: 5px 20px;
37+
margin: 0 10px 10px 0;
38+
font-size: 18px;
39+
color: #fff;
40+
cursor: pointer;
41+
background: #00BFFF;
42+
border: 0;
43+
border-radius: 5px;
44+
box-shadow: 2px 2px 3px #bbb;
45+
}
46+
47+
button:hover,
48+
input[type="submit"]:hover {
49+
background: #334e4d;
50+
}
51+
52+
button.active {
53+
background: #334e4d;
54+
}
55+
#singer-form{
56+
float: left;
57+
}

0 commit comments

Comments
 (0)