window.matchMedia Javascript在用户更换页面时记住用户的选择。

问题描述 投票:0回答:1

我在使用 prefers-color-scheme 时遇到了一些问题,我想实现的逻辑。例如,在我的网站上,我的prefers-color-scheme有一个toggle,当用户使用浅色模式时,它可以覆盖黑色,反之亦然。我遇到的问题是,我不能把它切换到用户改变切换设置为操作系统颜色主题时,当他们切换页面时,主题又切换回prefers颜色方案。我已经设置了本地存储和名为主题类型的变量。

当我注释掉检测配色方案功能时,本地存储会记住用户所需的主题设置。当没有注释时,它将覆盖并始终选择主题os的配色方案。我怎样才能使我的逻辑工作正确,当用户在创建本地存储之前的第一个入口点时,它读取主题OS,但如果用户改变主题为黑色,反之亦然,当页面改变时,OS不会被覆盖?

谢谢,所以 detectColorScheme 的检查是在本地存储创建之前的第一个切入点。

所以 detectColorScheme 会检查用户的 OS 主题。

function detectColorScheme(){

var on = 1;
on = 1;

if (window.matchMedia('(prefers-color-scheme: dark)').matches && on <= 1) {
  if (on = 1 ) {
      on = 2;
      darkmode();
      console.log("OS Setting DARK MODE");
  }

}

 if (window.matchMedia("(prefers-color-scheme: light)").matches ) {
   if (on = 1) {
     lightmode();
     console.log("OS Setting LIGHT MODE");
   }
}

}

然后在javascript文件的开头,我做了如下操作。

document.body.style.backgroundColor = "";

if (localStorage.themepref == 1 ) {
    detectColorScheme();
    document.body.style.backgroundColor = "#FFF";
    lightmode();
}
else {
    detectColorScheme();
    document.body.style.backgroundColor = "#0a0a0a";
    darkmode();
    localStorage.themepref = 2;
}


window.onload = function() {
  console.log('First');

  if (event.target.readyState === 'loading') {
    detectColorScheme();
    $('body').css({ background: ''});
    document.body.style.backgroundColor = "inherit";

   if(lightmodeON == true) {
     detectColorScheme();
     $('body').css({background: "#fbfcfd"});
     document.body.style.backgroundColor = "#FFF";
   }

   if(lightmodeON == false) {
     detectColorScheme();
     $('body').css({background: "#0a0a0a"});
     document.body.style.backgroundColor = "#0a0a0a";
   }
}
};

最后是

document.addEventListener("DOMContentLoaded", function() {

    window.scrollTo(0, 0);
    $(window).scrollTop( $("#top").offset().top );
    $(document).scrollTop(0);

  document.body.style.backgroundColor = "";

  if (document.readyState === 'complete') {


    if(lightmodeON == true) {
      $('body').css({background: "#fbfcfd"});
      console.log('loading white bg');
    }

    if(lightmodeON == false) {
      $('body').css({background: "#0a0a0a"});
      console.log('loading black bg');
    }
  }


  if (typeof (Storage) !=="undefined") {
    if (localStorage.themepref == 1 ) {
      lightmode();
    }
    else {
      darkmode();
      localStorage.themepref = 2;
    }

    if(lightmodeON == true) {
      $('body').css({background: "#fbfcfd"});
      console.log('loading fffwhite bg');
    }

    if(lightmodeON == false) {
      $('body').css({background: "#0a0a0a"});
      console.log('loading black bg');
    }
  }
javascript jquery css background local-storage
1个回答
2
投票

我已经做了这个小努力,我做的是。

当用户第一次加载网站时,本地存储将是空的,所以我们将根据操作系统设置主题,但如果用户玩我们的toggle,如果开启则为lightmode,否则为darkmode,那么我们也将保存在本地存储中,下次当用户访问我们的页面时,我们将从本地存储中检查它的偏好,永远不会回到操作系统主题。

请看一下,如果有帮助的话,请告诉我。由于本地存储不支持,所以在这里是行不通的。请复制并在浏览器中测试。

<html lang="en">
<head>
<meta charset="utf-8">
<title>test</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
	<label style="color:yellow;">IsLightMode: </label>
	<input id="toggleCheck" type="checkBox" onclick="toggle()" />
	<script>
		var lightmodeOn = true;
		function changeColorOfBackground(lightmodeOn) {
			if(lightmodeOn == true) {
			  // you can call your lightmode function here if want to set other things too.
			  $('body').css({background: "#fbfcfd"});
			  console.log('loading white bg');
			}

			if(lightmodeOn == false) {
			  // you can call your darkmode function here if want to set other things too.
			  $('body').css({background: "#0a0a0a"});
			  console.log('loading black bg');
			}
		}
		function toggle() {
			lightmodeOn = !lightmodeOn;
			changeColorOfBackground(lightmodeOn);
			localStorage.themepref = lightmodeOn?1:2;
		}
		
		document.addEventListener("DOMContentLoaded", function() {
			
	        //if it is not set in localStorage only then check OS theme otherwise always load from localStorage
			if(localStorage.themepref === null) {
				//if windows is light, then we should go with dark theme
				if (window.matchMedia('(prefers-color-scheme: light)').matches) {
					lightmodeOn = false;
				}
			}
			else if (localStorage.themepref == 2 ) {
				lightmodeOn = false;
			}
			
			if(lightmodeOn) {
				$('#toggleCheck').prop('checked',true);
			}
			
			changeColorOfBackground(lightmodeOn);
		});
	</script>
</body>
</html>
© www.soinside.com 2019 - 2024. All rights reserved.