inject css with javascript

Solutions on MaxInterview for inject css with javascript by the best coders in the world

showing results for - "inject css with javascript"
Juan Pablo
13 Apr 2020
1let isScrolled = false;
2
3window.addEventListener("scroll", () => {
4  if (window.scrollY < 800) {
5    let styleString1 = `body {background: silver;}`
6      const style1 = document.createElement('style');
7      style1.textContent = styleString1;
8      document.head.append(style1);
9      // console.log("style1: ", style1)
10    isScrolled = false;
11    console.log("not past 800px, isScrolled: ", isScrolled)
12    
13  } else {
14    let styleString2 = `body {background: red;}`
15      const style2 = document.createElement('style');
16      style2.textContent = styleString2;
17      document.head.append(style2);
18      // console.log("style2: ", style2)
19    isScrolled = true;
20    console.log("past 800px, isScrolled: ", isScrolled)
21  }
22});
23
24console.log("isScrolled 3", isScrolled)
25
26