Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to change background color on scroll

const [red, green, blue] = [69, 111, 225]
const section1 = document.querySelector('.section1')

window.addEventListener('scroll', () => {
  let y = 1 + (window.scrollY || window.pageYOffset) / 150
  y = y < 1 ? 1 : y // ensure y is always >= 1 (due to Safari's elastic scroll)
  const [r, g, b] = [red/y, green/y, blue/y].map(Math.round)
  section1.style.backgroundColor = `rgb(${r}, ${g}, ${b})`
})
Comment

PREVIOUS NEXT
Code Example
Javascript :: for loop set timeout 
Javascript :: ajax submit form data 
Javascript :: prettier vscode settings 
Javascript :: nodejs bodyparser form data 
Javascript :: appending an element to another element javascript 
Javascript :: click counter in js 
Javascript :: iife js arrow function 
Javascript :: what is 5+5 
Javascript :: jquery 1 cdn 
Javascript :: Downward Triangle Star Pattern in JavaScript 
Javascript :: js + before variable 
Javascript :: validation select option jquery 
Javascript :: how to know which button is clicked in jquery 
Javascript :: check upload img extension jquery 
Javascript :: javascript competitive programming 
Javascript :: axios fetch 
Javascript :: import js in jupyter notebook 
Javascript :: angular is not defined In AngularJS 
Javascript :: chart js in angular 13 
Javascript :: string literal javascript 
Javascript :: javascript new date from string dd/mm/yyyy 
Javascript :: sort array object 
Javascript :: window.addeventlistener 
Javascript :: javascript ready state 
Javascript :: get value from textbox in vanilla javascript 
Javascript :: find a word in string javascript 
Javascript :: javascript date to string format dd mmm yyyy 
Javascript :: react usereducer 
Javascript :: polyfill of bind 
Javascript :: list of higher-order functions javascript 
ADD CONTENT
Topic
Content
Source link
Name
5+6 =