Judging the dark mode of the system

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<title></title>
		<style>
			:root {
      
      
			    color-scheme: light dark;
			    background: white;
			    color: black;
			}
			
			@media (prefers-color-scheme: dark) {
      
      
			    :root {
      
      
			        background: black;
			        color: white;
			    }
			}
		</style>
	</head>
	<body>
		
	</body>
	<script type="text/javascript">
		const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)')
		
		function darkModeHandler() {
      
      
		    if (mediaQuery.matches) {
      
      
		        console.log('现在是深色模式')
		    } else {
      
      
		        console.log('现在是浅色模式')
		    }
		}
		
		// 判断当前模式
		darkModeHandler()
		// 监听模式变化
		mediaQuery.addListener(darkModeHandler)
	</script>
</html>

Guess you like

Origin blog.csdn.net/zhai_865327/article/details/123110547