Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Utils based on http://youmightnotneedjquery.com/ */
- /* JQuery ready */
- function ready(fn) {
- if (document.readyState != 'loading'){
- fn();
- } else if (document.addEventListener) {
- document.addEventListener('DOMContentLoaded', fn);
- } else {
- document.attachEvent('onreadystatechange', function() {
- if (document.readyState != 'loading')
- fn();
- });
- }
- }
- /* JQuery addClass */
- function addClass (el, className) {
- if (el.classList) {
- el.classList.add(className);
- } else {
- var current = el.className, found = false;
- var all = current.split(' ');
- for(var i=0; i<all.length, !found; i++) found = all[i] === className;
- if(!found) {
- if(current === '') el.className = className;
- else el.className += ' ' + className;
- }
- }
- }
- /* JQuery removeClass */
- function removeClass (el, className) {
- if (el.classList)
- el.classList.remove(className);
- else
- el.className = el.className.replace(new RegExp('(^|\\b)' + className.split(' ').join('|') + '(\\b|$)', 'gi'), ' ');
- }
- /* JQuery hasClass */
- function hasClass (el, className) {
- if (el.classList)
- return el.classList.contains(className);
- else
- return new RegExp('(^| )' + className + '( |$)', 'gi').test(el.className);
- }
- /* JQuery on */
- function addEventListener(el, eventName, handler) {
- if (el.addEventListener) {
- el.addEventListener(eventName, handler);
- } else {
- el.attachEvent('on' + eventName, function(){
- handler.call(el);
- });
- }
- }
- /* Utils end */
- /* Script start here */
- ready(function () {
- const wrapper = document.getElementById('wrapper')
- const toggler = document.getElementById('theme-toggler')
- if (localStorage.theme) {
- addClass(wrapper, localStorage.theme)
- } else {
- addClass(wrapper, 'light-mode')
- }
- addEventListener(toggler, 'click', function () {
- if (hasClass(wrapper, 'light-mode')) {
- removeClass(wrapper, 'light-mode')
- addClass(wrapper, 'night-mode')
- localStorage.theme = 'night-mode'
- } else {
- removeClass(wrapper, 'night-mode')
- addClass(wrapper, 'light-mode')
- localStorage.theme = 'light-mode'
- }
- })
- })
- /* End */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement