banner



How To Change Background Color Based On Scroll Javascript

In this tutorial yous'll learn how to alter the groundwork color of a page randomly, using JavaScript. You'll also learn how to modify your lawmaking using HSL color values to generate only pastel colors or dark colors.

Permit's take a look at what we'll exist building:

In this demo, we change the background colour and text color of the page every 1500ms. Most of the heavy lifting is done with JavaScript but permit's take a expect at the content and styling:

HTML

For the content of our page, we'll create an chemical element with an id background and put some text in information technology.

CSS

We'll employ CSS to command the background-color transition and so the change looks smoother.

JavaScript

We'll exist generating random colors in JavaScript past combining these ii methods:

  1. HSL color note, and
  2. The Math library

"The hsl() functional annotation expresses a given color co-ordinate to its hue, saturation, and lightness components" - MDN

The hue value has a maximum of 360 and represents the degree of the color position on a color wheel. The saturation and lightness values accept a maximum of 100 and correspond the percentage saturation and lightness of a color respectively.

Fiddle with the ranges below to see how the Hue, Saturation and Lightness values impact how a color looks.

In order to generate completely random colors, we'll pass random numbers within a fixed range to the three HSL values. Nosotros can practise this using Math.random and Math.floor

Math.random generates random numbers between 0 and one. We can multiply these numbers by our specified range and apply Math.floor to round up to the nearest whole number.

Then we'll use the getRandomNumber function to generate random values for our HSL note.

Finally, nosotros pass the random generated colour to our groundwork chemical element.

This allows us to fix a random color to our groundwork.

Applying the New Color

We can make up one's mind whether to change the background color whenever the user loads the page or regularly at a set interval by passing the in a higher place code into an onLoad or a setInterval function.

Hither's the completed JavaScript lawmaking:

In the above demo, we can come across that the text color also changes randomly but in an inverted manner to keep it readable against the groundwork. This is done using CSS.

Invert Text Colors with CSS

First, we pass the same color every bit the background for the text colour in our setBackgroundColor function.

And so nosotros apply the CSS invert filter to make sure the text color is contrasted against the groundwork.

How to Randomly Generate Pastel Colors

1 benefit to using HSL color values is that we can control how saturated or vivid the randomly generated colors are. In this manner we tin can change our code to generate only pastel colors.

We do this past setting a fixed value of 100% to the saturation and a value of 90% to the lightness. Our updated code looks like this:

How to Randomly Generate Dark Colors

We can apply the same logic to generate darker colors.

Conclusion

This tutorial has primarily taught you how to generate random colors with JavaScript, but it should also take given you lot a good grasp on the HSL notation for colors, and using the Math library for generating numbers.

Feel free to utilize this to a project you're working on to bandbox upwards your page!

Learn JavaScript Skills

Nosotros take a wide range of beginner to intermediate level JavaScript tutorials, teaching you foundational cognition and practical skills.

Did you find this post useful?

How To Change Background Color Based On Scroll Javascript,

Source: https://webdesign.tutsplus.com/tutorials/generate-random-background-colors-javascript--cms-37030

Posted by: cliffordponeely.blogspot.com

0 Response to "How To Change Background Color Based On Scroll Javascript"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel