-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
50 lines (41 loc) · 1.38 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
$(document).ready(function() {
// Code that runs when the document is ready
colors.forEach(function(color) {
console.log(color);
addBox(color);
})
colorIdx = Math.floor(Math.random() * colors.length);
previewColor = colors[colorIdx];
setPreviewColor(previewColor);
});
function setPreviewColor(color) {
$('.preview').css('background-color', color);
$('.color-code').text($('.preview').css('background-color'));
}
$(document).on('keyup', '#color', function() {
setPreviewColor($('#color').val());
});
function addBox(color) {
if ($('#colors .item').length >= 16) {
$('#colors .item').last().remove();
}
$('#colors').prepend('<div class="item" style="background-color: ' + color + ';"></div>');
previewColor = color;
}
$(document).on('click', '#add-to-favorite', function() {
addBox($('#color').val());
$('#color').val(''); // Reset the value of #color field (text box)
$('#color').focus(); // Set focus back on text box
});
$(document).on('mouseenter', '.item', function() {
setPreviewColor($(this).css('background-color'));
});
$(document).on('mouseleave', '.item', function() {
setPreviewColor(previewColor);
});
$(document).on('click', '.item', function() {
previewColor = $(this).css('background-color');
setPreviewColor(previewColor);
});
var previewColor;
var colors = [ '770077', '008080' ];