Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to generate random gradient javascript

// Generate random Gradient on click
var myButton = document.querySelector('.classNamehere');
myButton.addEventListener('click', randomGradient);
function randomGradient() {

        var angle = Math.floor(Math.random() * 360);

  // You can add/remove colors simply by calling or removing the colorCode() function
        var gradient = `linear-gradient(${angle}deg,
        #${colorCode()},
        #${colorCode()},
        #${colorCode()})`;

        const element = document.querySelector("whateverElement").style.background = gradient;

		// uncomment the console log to see the generated gradient
        // console.log(gradient);

    }

// this function generates random color(HEX) and returns it
function colorCode() {
        var hexCode1 = "";
        var hexValues1 = "0123456789abcdef";

        for (var i = 0; i < 6; i++) {
            hexCode1 += hexValues1.charAt(Math.floor(Math.random() * hexValues1.length));
        }
        return hexCode1;
    }
Comment

PREVIOUS NEXT
Code Example
Javascript :: get data firebase 
Javascript :: javascript make title blink 
Javascript :: play a sound in js 
Javascript :: shadow generator react native 
Javascript :: index of javascript 
Javascript :: timestamp discord.js 
Javascript :: remove array value by index js 
Javascript :: new features of angular 11 
Javascript :: angular convert boolean to yes no 
Javascript :: Century From Year 
Javascript :: Convert pixels to number js 
Javascript :: angular debug chrome launch.json 
Javascript :: javascript math 
Javascript :: how to fetch data from json file in flutter 
Javascript :: javascript not running 
Javascript :: react native fontsize not affected by phone settings 
Javascript :: router.push next js 
Javascript :: expo cli vs react native cli 
Javascript :: creating room in ws node js 
Javascript :: hook usePreloadImages 
Javascript :: script defer attribute 
Javascript :: change element text innerhtml keeping the elements or tags inside 
Javascript :: cypress/react yarn 
Javascript :: servicenow gliderecord lookup 
Javascript :: how to push in object in javascript 
Javascript :: sequelize update index column 
Javascript :: json.parse 
Javascript :: universal mobile number regex 
Javascript :: event delegation javascript 
Javascript :: splice 
ADD CONTENT
Topic
Content
Source link
Name
6+6 =