網頁黑夜模式白天模式切換 html+css+js
效果:
先言:
實現是很簡單的,就是背景色的切換,用的是最基礎的js;
1.定義開關按鈕標籤,‘bai’就是裡面那個小圓圈:
<div class="kaiguan" >
<div class="bai" id="anniu"></div>
</div>
2.開關按鈕的樣式:
.kaiguan{
position: absolute;
top: 20px;
right: 50px;
width: 40px;
height: 20px;
border: 2px solid black;
border-radius: 20px;
cursor: pointer;
transition: all 3s;
}
.bai{
position: absolute;
top: 1.5px;
left: 2px;
height: 13px;
width: 13px;
background-color: rgb(0, 0, 0);
border-radius: 50%;
transition: all 1s;
}
3.js部分,點選開關後,開關和小球的樣式改變,body的字型和背景顏色改變:
<script>
var anniu =document.getElementById('anniu');
var kaiguan = document.querySelector('.kaiguan');
var body = document.getElementById('body');
var temp = 1;
anniu.addEventListener('click',function(){
if(temp==1){
this.className = 'hei';
temp=0;
kaiguan.style. border= '2px solid rgb(11, 243, 81)';
body.style. backgroundColor= 'rgb(7, 7, 29)';
body.style.color = 'white';
}else{
this.className = 'bai';
temp=1;
kaiguan.style. border= '2px solid black';
body.style. backgroundColor= 'white';
body.style.color = 'black';
}
})
</script>
4.修改後的樣式:
.hei{
position: absolute;
top: 1.5px;
right: 2px;
height: 13px;
width: 13px;
background-color: rgb(11, 243, 81);
border-radius: 50%;
transition: all 1s;
}
5.完整程式碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
padding: 0;
margin: 0;
box-sizing: border-box;
}
body{
height: 100vh;
transition: all 1s;
}
.kaiguan{
position: absolute;
top: 20px;
right: 50px;
width: 40px;
height: 20px;
border: 2px solid black;
border-radius: 20px;
cursor: pointer;
transition: all 3s;
}
.bai{
position: absolute;
top: 1.5px;
left: 2px;
height: 13px;
width: 13px;
background-color: rgb(0, 0, 0);
border-radius: 50%;
transition: all 1s;
}
.hei{
position: absolute;
top: 1.5px;
right: 2px;
height: 13px;
width: 13px;
background-color: rgb(11, 243, 81);
border-radius: 50%;
transition: all 1s;
}
h1{
margin: 100px auto;
width: 1000px;
text-align: center;
}
p{
margin: -60px auto;
width: 1100px;
text-align-last:center;
}
</style>
</head>
<body id="body" >
<div class="kaiguan" >
<div class="bai" id="anniu"></div>
</div>
<h1>Life of Pi</h1>
<p >
    He lives in Scarborough. He's a small, slim man – nomore than five foot five. Dark hair, dark eyes. Hair greyingat the temples. Can't be older than forty. leasingcoffee-coloured complexion1. Mild fall weather, yet puts on abig winter parka with fur-lined hood2 for the walk to thediner.
He lives in Scarborough. He's a small, slim man – nomore than five foot five. Dark hair, dark eyes. Hair greyingat the temples. Can't be older than forty. leasingcoffee-coloured complexion1. Mild fall weather, yet puts on abig winter parka with fur-lined hood2 for the walk to thediner.
He lives in Scarborough. He's a small, slim man – nomore than five foot five. Dark hair, dark eyes. Hair greyingat the temples. Can't be older than forty. leasingcoffee-coloured complexion1. Mild fall weather, yet puts on abig winter parka with fur-lined hood2 for the walk to thediner.
He lives in Scarborough. He's a small, slim man – nomore than five foot five. Dark hair, dark eyes. Hair greyingat the temples. Can't be older than forty. leasingcoffee-coloured complexion1. Mild fall weather, yet puts on abig winter parka with fur-lined hood2 for the walk to thediner.
He lives in Scarborough. He's a small, slim man – nomore than five foot five. Dark hair, dark eyes. Hair greyingat the temples. Can't be older than forty. leasingcoffee-coloured complexion1. Mild fall weather, yet puts on abig winter parka with fur-lined hood2 for the walk to thediner.
He lives in Scarborough. He's a small, slim man – nomore than five foot five. Dark hair, dark eyes. Hair greyingat the temples. Can't be older than forty. leasingcoffee-coloured complexion1. Mild fall weather, yet puts on abig winter parka with fur-lined hood2 for the walk to thediner.
</p>
<script>
var anniu =document.getElementById('anniu');
var kaiguan = document.querySelector('.kaiguan');
var body = document.getElementById('body');
var temp = 1;
anniu.addEventListener('click',function(){
if(temp==1){
this.className = 'hei';
temp=0;
kaiguan.style. border= '2px solid rgb(11, 243, 81)';
body.style. backgroundColor= 'rgb(7, 7, 29)';
body.style.color = 'white';
}else{
this.className = 'bai';
temp=1;
kaiguan.style. border= '2px solid black';
body.style. backgroundColor= 'white';
body.style.color = 'black';
}
})
</script>
</body>
</html>
總結:
233333
相關文章
- python 爬蟲 自動切換 learnku 的白天 / 夜間模式Python爬蟲模式
- 可自動切換 learnku 夜間 / 白天模式的 python 指令碼模式Python指令碼
- win10怎麼切換到桌面模式_win10平板模式和桌面模式的切換教程Win10模式
- Win10怎樣切換平板模式_win10如何切換到平板模式Win10模式
- 絕地求生激戰場黑夜模式玩法介紹 刺激戰場黑夜模式怎麼玩?模式
- 快速切換至Kotlin for Android模式KotlinAndroid模式
- win10安全模式切換正常模式的方法Win10模式
- KindEditor如何切換成簡單模式??模式
- vxe-table 一鍵切換編輯模式、只讀模式模式
- 深色模式適配和主題切換模式
- win10平板模式無法切換如何處理_win10平板模式切換不了怎麼解決Win10模式
- 使用RecyclerView展示資料(可切換列表模式)View模式
- redis sentinel哨兵模式安裝部署和切換Redis模式
- idea 中maven檢視 切換離線模式IdeaMaven模式
- 微星筆記本怎麼切換啟動模式?微星筆記本bios設定啟動模式切換的教程筆記模式iOS
- win10一鍵切換夜晚模式怎麼操作_win10怎麼實現夜間模式快速切換Win10模式
- win10平板模式怎麼切換電腦模式 win10怎麼退出平板模式到桌面模式Win10模式
- 將Debian從Legacy切換為UEFI啟動模式模式
- Oracle資料庫歸檔模式的切換ELOracle資料庫模式
- flutter 暗黑模式切換過渡動畫實現Flutter模式動畫
- Win10怎麼切換到黑暗模式_win10暗黑模式怎麼開Win10模式
- Win10平板模式如何切換_win10系統退出平板模式的教程Win10模式
- docker Redis單機主從哨兵模式切換失敗DockerRedis模式
- 幻燈片放映模式切換windows terminal背景圖片模式Windows
- win10平板模式怎麼回到桌面_win10平板模式切換到桌面如何操作Win10模式
- 谷歌瀏覽器相容模式怎麼設定 chrome瀏覽器相容模式切換方法介紹谷歌瀏覽器模式Chrome
- win10系統切換到快捷更新模式的方法Win10模式
- 已解決 phpstudy切換nts模式出現的No input file specifiedPHP模式
- 一鍵切換夜間模式、隱藏桌面,試試One Switch模式
- Silence 主題暗黑模式根據瀏覽器配置,以及切換頁面閃白屏的問題處理模式瀏覽器
- 原來還可以這麼切換到正常編輯器的模式模式
- EDBPPAS(Oracle相容版)Oracle與PostgreSQL相容模式的引數配置切換OracleSQL模式
- Emacs月月積累(二):視窗、緩衝區和常用模式切換Mac模式
- win10系統下電源模式總會自動切換更改為“節能”模式怎麼辦Win10模式
- 小程式標籤頁切換效果
- 使用Bootstrap tab頁切換的使用boot
- EDB PPAS(Oracle 相容版) Oracle與PostgreSQL 相容模式的引數配置切換OracleSQL模式
- win10系統切換平板模式沒有提示怎麼解決Win10模式