visit
Basic Structure
<div class="random-color">
</div>
Color Display
<!-- color Display -->
<div class="display" id="gradient"></div>
Color Code Box
<!-- color code box -->
<div class="codess"></div>
Color Input
<!-- color input -->
<input type="color" class="color1" name="color1" value="#FFAAAA">
<input type="color" class="color2" name="color2" value="#734C8F">
HTML Select Box
<!-- select box -->
<select name="toDirection">
<option value="to right">to right</option>
<option value="to left bottom">to left bottom</option>
<option value="to left top">to left top</option>
<option value="to bottom">to bottom</option>
<option value="to top">to top</option>
<option value="to right bottom">to right bottom</option>
<option value="to right top">to right top</option>
<option value="to left">to left</option>
</select>
html {
height: 100%;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
background-repeat:no-repeat;
}
body {
color: #035aab;
text-align: center;
}
.random-color{
width: 350px;
box-shadow: 0 0 20px rgba(0,139,253,0.25);
margin: 50px auto;
padding: 20px;
height: 400px;
}
Now I have designed the display. Display width: 350px
and height: 200px
. Here a background Gradient color is used, which can be seen in normal condition. This color will change when you change the value of the input function.
.display{
width: 350px;
height: 200px;
border: 2px solid white;
box-shadow: 0 0 20px rgba(1,1,1,0.35);
background: linear-gradient(to right, #FFAAAA, #734C8F)
}
Now the box is designed to see the color code. It will determine its own size based on the amount of content.
.codess{
padding: 5px;
margin-top: 30px;
margin-bottom: 30px;
font-family: sans-serif;
letter-spacing: 1px;
box-shadow: 0 0 20px rgba(0,139,253,0.25);
}
Now the space for color input has been designed. The width of those two input boxes: 70px, height: 40px and float: left
have been used to keep it along the left side.
.color1,.color2{
width: 70px;
height: 40px;
float: left;
margin: 10px;
margin-top: 20px;
}
Now I have done some basic design of the select box. Box width: 130px
and height: 35px
.
select{
float: right;
margin-top: 25px;
width: 130px;
height: 35px;
}
Above, we have completed the basic design of the Gradient Color Generator. But now, it is time to implement this color generator with the help of JavaScript.
//Some classes and html functions need to determine a constant
var css = document.querySelector(".codess") // color code
var color1 = document.querySelector(".color1") // 1st color
var color2 = document.querySelector(".color2") // 2nd color
var bodys = document.getElementById("gradient") // color display
var linearDirection = document.getElementsByName("toDirection")[0] //Select box
GetPropertyValue
helped make this work. getPropertyValue () method interface returns a DOMString containing the value of a specified CSS property.
Then I took the help of textContent
to show all the information in the box.
function currentSettings() {
var CSSprop = window.getComputedStyle(bodys,null).getPropertyValue("background-image")
// console.log(CSSprop)
css.textContent = CSSprop
}
currentSettings()
I have given instructions here in which format the color codes can be seen in this code box. The color codes can be seen in the box according to this format.
With this, I have arranged to see the color in the display. In "bodys.style.background"
I have added all formats. Its color can be found on the display. That means it will do two things at once.
function returnColor(){
bodys.style.background =
"linear-gradient("
+ linearDirection.value
+ ", "
+ color1.value
+ ","
+ color2.value
+ ")";
currentSettings()
}
Now I have connected all the input functions with the calculations above. "ReturnColor"
helped. As a result, when you make any changes to the input box, all of the above systems will change.
document.querySelector('select[name="toDirection"]').onchange=returnColor;
color1.addEventListener("input", returnColor)
color2.addEventListener("input", returnColor)
Complete JavaScript
//Some classes and html functions need to determine a constant
var css = document.querySelector(".codess") // color code
var color1 = document.querySelector(".color1") // 1st color
var color2 = document.querySelector(".color2") // 2nd color
var bodys = document.getElementById("gradient") // color display
var linearDirection = document.getElementsByName("toDirection")[0] //Select box
function currentSettings() {
var CSSprop = window.getComputedStyle(bodys,null).getPropertyValue("background-image")
// console.log(CSSprop)
css.textContent = CSSprop
}
currentSettings()
function returnColor(){
bodys.style.background =
"linear-gradient("
+ linearDirection.value
+ ", "
+ color1.value
+ ","
+ color2.value
+ ")";
currentSettings()
}
document.querySelector('select[name="toDirection"]').onchange=returnColor;
color1.addEventListener("input", returnColor)
color2.addEventListener("input", returnColor)
If you like this tutorial, then you must like the article
. Please comment on how you like it. You can download the if you want.