-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
84 lines (73 loc) · 1.77 KB
/
script.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
74
75
76
77
78
79
80
81
82
83
84
const projectName = 'random-quote-machine';
let quotesData;
var colors = [
'#652705',
'#4c1f27',
'#342842',
'#212d49',
'#223048',
'#431a03',
'#32141a',
'#231a2c',
'#161e31',
'#172030',
];
var currentQuote = '',
currentAuthor = '';
function getQuotes() {
return $.ajax({
headers: {
Accept: 'application/json'
},
url:
'https://gist.githubusercontent.com/camperbot/5a022b72e96c4c9585c32bf6a75f62d9/raw/e3c6895ce42069f0ee7e991229064f167fe8ccdc/quotes.json',
success: function (jsonQuotes) {
if (typeof jsonQuotes === 'string') {
quotesData = JSON.parse(jsonQuotes);
}
}
});
}
function getRandomQuote() {
return quotesData.quotes[
Math.floor(Math.random() * quotesData.quotes.length)
];
}
function getQuote() {
let randomQuote = getRandomQuote();
currentQuote = randomQuote.quote;
currentAuthor = randomQuote.author;
$('#tweet-quote').attr(
'href',
'https://twitter.com/intent/tweet?hashtags=quotes&related=freecodecamp&text=' +
encodeURIComponent('"' + currentQuote + '" ' + currentAuthor)
);
$('.text-container').animate({ opacity: 0 }, 500, function () {
$(this).animate({ opacity: 1 }, 500);
$('#author').html('- ' + randomQuote.author);
});
$('.quote').animate({ opacity: 0 }, 500, function () {
$(this).animate({ opacity: 1 }, 500);
$('#text').text(randomQuote.quote);
});
var color = Math.floor(Math.random() * colors.length);
$('html body').animate(
{
backgroundColor: colors[color],
color: colors[color]
},
1000
);
$('.button').animate(
{
backgroundColor: colors[color]
},
1000
);
}
$(document).ready(function () {
getQuotes().then(() => {
getQuote();
});
$('#new-quote').on('click', getQuote);
});