Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Dark/Light color switch Reply To: Dark/Light color switch

#268340
Shikkediel
Participant

Check it out:

(function() {

  var motif = document.getElementById('theme'),
  flip = document.querySelector('.theme-switcher'),
  tone = true, depot = (function() {

    try {
    var bin = localStorage;
    bin.temp = 1;
    delete bin.temp;
    return bin || false;
    }
    catch(e) {
    return false;
    }
  })();

  if (depot) {
    if (!depot.shade) depot.shade = true;
    else {
      tone = JSON.parse(depot.shade);
      if (tone) motif.setAttribute('class', 'wrap light');
      else {
        motif.setAttribute('class', 'wrap dark');
        flip.innerHTML = 'bright';
      }
    }
  }

  flip.addEventListener('click', function() {

    tone = !tone;
    if (depot) depot.shade = tone;

    if (this.innerHTML == 'bright') this.innerHTML = 'dark';
    else this.innerHTML = 'bright';

    motif.classList.toggle('light');
    motif.classList.toggle('dark');
  });
})();

Dunno about the anons, haven’t noticed anything myself yet…