Skip to content

Instantly share code, notes, and snippets.

@raifcoonjah
Created March 16, 2022 18:32
Show Gist options
  • Save raifcoonjah/be43dc456642bee98e661bf38315b562 to your computer and use it in GitHub Desktop.
Save raifcoonjah/be43dc456642bee98e661bf38315b562 to your computer and use it in GitHub Desktop.
experimental-feature

// add button to change background to image from unsplash and save change to local storage var unsplashBtn = document.getElementById("unsplash"); unsplashBtn.addEventListener("click", function () { document.body.style.backgroundImage = "url(https://source.unsplash.com/random'" + "screenSize" + "screenHeight" + +"?ocean" + ")"; localStorage.setItem("background", "https://source.unsplash.com/random"); });

// check if local storage is set if (localStorage.getItem("background")) { document.body.style.backgroundImage = "url(" + localStorage.getItem("background") + ")"; }

// remove background image and remove from local storage var removeBgBtn = document.getElementById("default-bg"); removeBgBtn.addEventListener("click", function () { document.body.style.backgroundImage = "none"; localStorage.removeItem("background"); });

// get current screen size var screenSize = window.innerWidth; var screenHeight = window.innerHeight;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment