-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
executable file
·36 lines (28 loc) · 1022 Bytes
/
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
/* Exercise 2: Color picker */
var colors = [ "22ac5e", "d68236", "99ca2d", "ff6961", "CB99C9"];
function setPreviewColor(color) {
$(".preview").css("background-color", color);
$(".color-code").text($(".preview").css("background-color"));
}
function addBox(color) {
$("#colors").prepend("<div class='item' style='background-color: " + color +";'></div>");
}
$(document).ready(function() {
random_position = Math.floor( Math.random() * colors.length );
setPreviewColor(colors[random_position]);
$.each(colors, function(index, element) {
addBox(element);
});
$(document).on("keydown keyup keypress", "#color", function() {
setPreviewColor($(this).val());
});
$(document).on("click", "#add-to-favorite", function() {
if ( $("#colors .item").length >=5 )
$("#colors .item").last().remove();
addBox($("#color").val());
$("#color").val("").focus();
});
$(document).on('mouseenter', '.item', function() {
setPreviewColor($(this).css('background-color'));
});
});