• 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>
    

    文章来源:刘俊涛的博客 欢迎关注公众号、留言、评论,一起学习。


    若有帮助到您,欢迎捐赠支持,您的支持是对我坚持最好的肯定(_)

  • 相关阅读:
    20.C语言_数组参数传递
    19.C语言_取值运算符*
    Google Chrome 解决 “您的连接不是私密连接” 和被毒霸劫持
    教你如何一步步将项目部署到Github
    教你如何把Android手机的网络完全映射到PC上,比如免流给PC用…
    CSS background-size 属性详解
    display:block;是什么意思
    Cookie是储存在电脑文本文件中的数据,用于保存访问者的信息,并可以在下次打开页面时引用。
    Marquee(跑马灯)横向、纵向、无空白的不间断连续循环滚动代码
    Visual Studio中从应用程序中调试SQL脚本
  • 原文地址:https://www.cnblogs.com/lovebing/p/16837173.html
Copyright © 2020-2023  润新知