js一键切换dark模式
js一键切换dark模式
//初始化
$(function () {
let theme=$.cookie('theme');
if(theme=="dark"){
$("body").addClass('dark');
$("#theme").addClass('text-warning');
$("#theme").html('<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" class="feather feather-sun"><circle cx="12" cy="12" r="5"></circle><line x1="12" y1="1" x2="12" y2="3"></line><line x1="12" y1="21" x2="12" y2="23"></line><line x1="4.22" y1="4.22" x2="5.64" y2="5.64"></line><line x1="18.36" y1="18.36" x2="19.78" y2="19.78"></line><line x1="1" y1="12" x2="3" y2="12"></line><line x1="21" y1="12" x2="23" y2="12"></line><line x1="4.22" y1="19.78" x2="5.64" y2="18.36"></line><line x1="18.36" y1="5.64" x2="19.78" y2="4.22"></line></svg>');
}else{
$("body").removeClass('dark');
$("#theme").removeClass('text-warning');
$("#theme").html('<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" class="feather feather-moon"><path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"></path></svg>');
}
return false;
});
/**公共 **/
//主题切换
$("#theme").click(function () {
let theme=$.cookie('theme');
if(theme!="dark"){
$.cookie('theme','dark',{ expires: 7,path: '/' });
$("body").addClass('dark');
$(this).addClass('text-warning');
$(this).html('<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" class="feather feather-sun"><circle cx="12" cy="12" r="5"></circle><line x1="12" y1="1" x2="12" y2="3"></line><line x1="12" y1="21" x2="12" y2="23"></line><line x1="4.22" y1="4.22" x2="5.64" y2="5.64"></line><line x1="18.36" y1="18.36" x2="19.78" y2="19.78"></line><line x1="1" y1="12" x2="3" y2="12"></line><line x1="21" y1="12" x2="23" y2="12"></line><line x1="4.22" y1="19.78" x2="5.64" y2="18.36"></line><line x1="18.36" y1="5.64" x2="19.78" y2="4.22"></line></svg>');
}else{
$.removeCookie('theme');
$.removeCookie('theme',{ expires: 7,path: '/' });
$("body").removeClass('dark');
$(this).removeClass('text-warning');
$(this).html('<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" class="feather feather-moon"><path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"></path></svg>');
}
return false;
});
其他的一些js
//ajax
function Ajax(url,type,data,success,com=null,error2=null) {
$.ajax({
url:url,
headers: {'X-CSRF-TOKEN': $.cookie('X-CSRF-TOKEN')},
type:type,
data:data,
success:function (res) {
if(res.code==1){
success(res)
}else{
toastr.warning(res.msg||"未知错误");
}
},
error:function () {
toastr.error("Server Error");
if(error2!=null)
error2
},
complete:function (res) {
if(com !=null)
com(res)
}
})
}
function getObjectURL(file) {
var url = null ;
if (window.createObjectURL!=undefined) { // basic
url = window.createObjectURL(file) ;
} else if (window.URL!=undefined) { // mozilla(firefox)
url = window.URL.createObjectURL(file) ;
} else if (window.webkitURL!=undefined) { // webkit or chrome
url = window.webkitURL.createObjectURL(file) ;
}
return url ;
}
// 时间戳转多少分钟之前
function getDateDiff(dateTimeStamp){
var minute = 1000 * 60;
var hour = minute * 60;
var day = hour * 24;
var month = day * 30;
var now = new Date().getTime();
var diffValue = now - dateTimeStamp;
if(diffValue < 0){return;}
var monthC =diffValue/month;
var weekC =diffValue/(7*day);
var dayC =diffValue/day;
var hourC =diffValue/hour;
var minC =diffValue/minute;
if(monthC>=1){
result="" + parseInt(monthC) + "月前";
}
else if(weekC>=1){
result="" + parseInt(weekC) + "周前";
}
else if(dayC>=1){
result=""+ parseInt(dayC) +"天前";
}
else if(hourC>=1){
result=""+ parseInt(hourC) +"小时前";
}
else if(minC>=1){
result=""+ parseInt(minC) +"分钟前";
}else
result="刚刚";
return result;
}
function getDateTimeStamp(dateStr){
return Date.parse(dateStr.replace(/-/gi,"/"));
}
function goBack(){
if ((navigator.userAgent.indexOf('MSIE') >= 0) && (navigator.userAgent.indexOf('Opera') < 0)){ // IE
if(history.length > 0){
window.history.go( -1 );
}else{
window.opener=null;window.close();
}
}else{ //非IE浏览器
if (navigator.userAgent.indexOf('Firefox') >= 0 ||
navigator.userAgent.indexOf('Opera') >= 0 ||
navigator.userAgent.indexOf('Safari') >= 0 ||
navigator.userAgent.indexOf('Chrome') >= 0 ||
navigator.userAgent.indexOf('WebKit') >= 0){
if(window.history.length > 1){
window.history.go( -1 );
}else{
window.opener=null;window.close();
}
}else{ //未知的浏览器
window.history.go( -1 );
}
}
}
dark 其他方式:
<script src="https://cdn.jsdelivr.net/npm/darkmode-js@1.5.7/lib/darkmode-js.min.js"></script>
<script>
function addDarkmodeWidget() {
new Darkmode().showWidget();
}
window.addEventListener('load', addDarkmodeWidget);
</script>
文章来源:刘俊涛的博客 欢迎关注公众号、留言、评论,一起学习。
若有帮助到您,欢迎捐赠支持,您的支持是对我坚持最好的肯定(_)
你要保守你心,胜过保守一切。
本文来自博客园,作者:刘俊涛的博客,转载请注明原文链接:https://www.cnblogs.com/lovebing/p/16837173.html