In this project, we are going to change the background color with the help of the Color Picker.
Glimpse of the Project:
Approach:
- Create an HTML file in which we are going to add the text and a color picker which helps to change the background color of our web-page.
- Create a CSS style to give some animation effects to the web-page elements.
- Create a JavaScript file for adding event-listeners that can detect the movement of the mouse.
Example:
HTML:
- Firstly, create an HTML file (index.html).
- Then we link the CSS (style.css) file that provides all the animations effect to our HTML page. This is also placed in between the <head> tag.
- We add a link from Google Fonts to use a different type of font family in our project.
- Then we have to add an input tag so that we can use a color picker!
- At the end of the body tag, we have to add 2 <script> tags. One for our index.js file and the other for the icon that we have used on our webpage.
<!DOCTYPE html> < html lang = "en" >
< head >
< link rel = "stylesheet" href = "style.css" >
< script src = "index.js" ></ script >
< script src =
crossorigin = "anonymous" >
</ script >
</ head >
< body >
< h1 class = "head" >
Choose the color from the selector
to change the bg-color
< i class = "fas fa-arrow-right" >
</ i >
</ h1 >
< input type = "color" id = "clr" >
</ body >
</ html >
|
CSS: The following is the code for the “style.css” file used above in the HTML code. CSS is used to give different types of animations and effects to our HTML page so that it looks interactive to all users.
- Restore all the browser effects.
- Use classes and ids to give effects to HTML elements.
- Use of :hover to use hover effects.
- Use of @keyframes to use animations on our webpage.
/* restoring all the default properties of the browser */
*{ margin : 0 ;
padding : 0 ;
box-sizing: border-box;
} /* all similar effects of the web page so we applied them to the body */
body{ height : 100 vh;
display : flex;
justify- content : center ;
align-items: center ;
} /* css effects for the color picker */ input{ margin : 2em ;
width : 3em ;
height : 3em ;
border : . 4em solid black ;
border-radius: 20% ;
outline : none ;
cursor : pointer ;
box-shadow: 0 0 . 5em #111 ;
} /* for the icon that we have added */ .fas{ margin : 1em ;
} /* hover effects on color picker */ input:hover{ animation: round 2 s infinite;
} /* animation for our color picker */ @keyframes round{ 0% {
transform: rotate( 0 deg);
}
10% {
transform: rotate( 60 deg);
}
20% {
transform: rotate( 120 deg);
}
40% {
transform: rotate( 180 deg);
}
60% {
transform: rotate( 240 deg);
}
80% {
transform: rotate( 300 deg);
}
100% {
transform: rotate( 360 deg);
}
} |
JavaScript: In this, we write code for the detection of mouse movement for changing the background color.
Returns the first element that matches with specified CSS selector:
document.querySelector(‘css_selector’) =>Returns all the elements that matches with specified HTML ID:
document.getElementbyId(‘html_id’) =>
// Selection of id's and classes from html document const bgclr = document.getElementById( "clr" );
const headingg = document.querySelector( ".head" );
// Here we are adding event listener which // is used to detect the mouse movement bgclr.addEventListener( "input" , () => {
// This updates the background color which is
// picked by the user from the picker
document.body.style.backgroundColor = bgclr.value;
// This is the conditional statement that is used
// to change the text color from BLACK to WHITE
// when the background color changes to dark!
if (
bgclr.value.includes( "00" ) ||
bgclr.value.includes( "0a" ) ||
bgclr.value.includes( "0b" ) ||
bgclr.value.includes( "0c" ) ||
bgclr.value.includes( "0d" ) ||
bgclr.value.includes( "0e" ) ||
bgclr.value.includes( "0f" )
) {
headingg.style.color = "#fff" ;
} else {
headingg.style.color = "#000" ;
}
}); |
Output: