前端实现主题切换的几种方式

A little of BB:嗯,今天看到了某些网站主题效果,闲来无事,写篇文章记录一下对于主题实现,有哪些方式,也顺带给新手小伙伴们提供个思路。

1,老规矩,贴代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>切换主题</title>
    <style>
        #box_1,#box_2,#box_3{
            width: 300px;
            height: 200px;
            border: 1px solid black;
            background: palevioletred;
        }
        #area_show_1,#area_show_2,#area_show_3{
            width: 100px;
            height: 100px;
            border: 1px solid black;
            float: right;
            margin-right: 20px;
        }

        /*第二种方式用到*/
        #area_show_2.red{background: red;}
        #area_show_2.yellow{background: yellow;}
        #area_show_2.orange{background: orange;}

        label.red{color: red;}
        label.yellow{color: yellow;}
        label.orange{color: orange;}
        /*第二种方式用到*/
    </style>
    <script type="text/javascript" src="../js/common/jquery-1.7.js"></script>
    <link rel="stylesheet" class="for_theme" theme_type="red" href="../css/theme_1.css">
    <link rel="stylesheetttttt" class="for_theme" theme_type="yellow" href="../css/theme_2.css">
    <link rel="stylesheetttttt" class="for_theme" theme_type="orange" href="../css/theme_3.css">
</head>
<body>
    <div id="box_1">
        <label style="color: red">第一种方式</label>
        <select id="to_chose_1">
            <option attr_id="red">红色</option>
            <option attr_id="yellow">黄色</option>
            <option attr_id="orange">橙色</option>
        </select>
        <div id="area_show_1" style="background: red"></div>
    </div>

    <div id="box_2">
        <label class="red">第二种方式</label>
        <select id="to_chose_2">
            <option attr_id="red">红色</option>
            <option attr_id="yellow">黄色</option>
            <option attr_id="orange">橙色</option>
        </select>
        <div id="area_show_2" class="red"></div>
    </div>

    <div id="box_3">
        <label>第三种方式</label>
        <select id="to_chose_3">
            <option attr_id="red">红色</option>
            <option attr_id="yellow">黄色</option>
            <option attr_id="orange">橙色</option>
        </select>
        <div id="area_show_3" class="red"></div>
    </div>
<script>
    //第一种,用jqcss方法直接设置用于行内样式
    $(document).on("change","#to_chose_1",function () {
        var _this = $(this);
        var color = _this.find("option:checked").attr("attr_id");
        $("#area_show_1").css("background",color);
        $("#box_1 label").css("color",color);
    });

    //第二种,用选择器切换设置法
    $(document).on("change","#to_chose_2",function () {
        var _this = $(this);
        var color = _this.find("option:checked").attr("attr_id");
        console.log(color);
        $("#area_show_2").removeProp("class");
        $("#area_show_2").addClass(color);
        $("#box_2 label").addClass(color);
    });

    //第三种引用css样式文件切换法
    //利用link标签的rel属性属性设置为非stylesheet的值,再根据自定义标记的theme_type来灵活选择
    //这里为了把别的非主题的css区分开,把主题的css引用单独加个class=for_theme来区分,就是别的css文件免遭修改(你懂我意思吧)
    $(document).on("change","#to_chose_3",function () {
        var _this = $(this);
        var color = _this.find("option:checked").attr("attr_id");
        //先把所有的主题都作废,然后再将对应的主题设置上
        $(".for_theme").attr("rel","qwerty");
        $('.for_theme[theme_type="'+color+'"]').attr("rel","stylesheet");//按照选择器格式灵活拼接,别看不懂了哈
    });
</script>
</body>
</html>
2,至于引用的theme_1、2、3,就截图吧,这个没啥好说的


在这里插入图片描述

3,效果图,就是点击下拉框然后更换颜色,三种方式均可实现

在这里插入图片描述

4,至于每种效果解释呢,在代码注释里写了,这里就不赘述了;在这里总结一手:

  a,第一种呢,我觉着是比较low的,直接写,适合那种比较少的样式风格 切换去设置

  b,第二种呢,又比第一种要稍微好一些,可以预设写好一些选择器来用

  c,第三种,其实我觉得还是这种方式要好一些,毕竟对于主题而言,要设置的样式有很多,可以直接准备几套css样式文件,甚至可以深一点,搞一个自定义主题添加功能

一般主题,大多就是放一个按钮然后整个白天模式呀,夜晚模式呀,或者护眼模式呀等,对整个界面背景色或者背景图换一下,然后菜单栏颜色换一下,然后就是字体颜色等

5.我没秃,但我始终有一颗变强的心!
6.转载需注明出处,盗版必究

原文地址:https://blog.csdn.net/qq_40000351/article/details/129825328

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_11555.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注