1+ document . addEventListener ( "DOMContentLoaded" , function ( ) {
2+ const gradientDiv = document . getElementById ( "gradient" ) ;
3+ const generateButton = document . getElementById ( "generateButton" ) ;
4+ const gradientCodeInput = document . getElementById ( "gradientCode" ) ;
5+ const copyButton = document . getElementById ( "copyButton" ) ;
6+
7+ // Function to generate a random color
8+ function generateRandomColor ( ) {
9+ return '#' + Math . floor ( Math . random ( ) * 16777215 ) . toString ( 16 ) ;
10+ }
11+
12+ // Function to generate random gradient and set as background
13+ function generateRandomGradient ( ) {
14+ const color1 = generateRandomColor ( ) ;
15+ const color2 = generateRandomColor ( ) ;
16+ const gradientDirection = Math . random ( ) < 0.5 ? "to right" : "to left" ; // Random gradient direction
17+ const gradient = `linear-gradient(${ gradientDirection } , ${ color1 } , ${ color2 } )` ;
18+ gradientDiv . style . background = gradient ;
19+ gradientCodeInput . value = `background: ${ gradient } ;` ;
20+ }
21+
22+ // Event listener for the button click
23+ generateButton . addEventListener ( "click" , generateRandomGradient ) ;
24+
25+ // Event listener for the copy button click
26+ copyButton . addEventListener ( "click" , function ( ) {
27+ gradientCodeInput . select ( ) ;
28+ document . execCommand ( "copy" ) ;
29+ } ) ;
30+
31+ // Initial random gradient
32+ generateRandomGradient ( ) ;
33+ } ) ;
34+
0 commit comments