【前端demo】动态赋值CSS

文章目录

其他demo

效果

动态显示CSS样式,由:root统一配置。

效果预览:https://codepen.io/karshey/pen/BavLrwy

参考:

Dynamic CSS Variables(codepen.io)

漫谈document.documentElement与document.body - 简书 (jianshu.com)

过程

html实现

滑动滑块和滑动border-radius<input type='range'>实现,选择颜色由<input type='color'>实现

对于radius,四个角一起变化的最大值是50%。

oninput与onchange事件

oninput 事件在用户输入时触发。

该事件在 <input><textarea> 元素的值发生改变时触发。

该事件类似于 onchange事件。不同之处:oninput 事件在元素值发生变化是立即触发, onchange 在元素失去焦点时触发。

在这里,显然滑块的滑动是不能失去焦点才触发的(滑块滑动的时候box要同步移动),但颜色的选择可以(可以试一试,改变颜色是选择颜色之后再点击空白区域,方块颜色才会发生改变)。

统一配置CSS

我们的目的是实现动态改变box的样式。因此,可以将box的样式统一在root处配置:

css 复制代码
/* 统一配置box的样式 */
:root {
    --color: #000;
    --location: 0;
    --radius: 0;
}

.box {
    height: 200px;
    width: 200px;
    
    background-color: var(--color);
    border-radius: var(--radius);
    /* 向右移动 */
    transform: translateX(var(--location));
    transition: width 2s;
}

因此,如果想改变box的样式,只需改变:root中的值即可。

注意,这里root中的变量名(colorlocationradius),正好对应方法changeValue的第一个参数。

因此可以通过将输入的type与--拼接直接形成CSS变量,然后直接赋值。

javascript 复制代码
root.style.setProperty(`--${type}`, value + add)

注意,颜色的赋值是不用加%的,而border-radiustransform: translateX()的数值要加%

javascript 复制代码
const root = document.documentElement;
function changeValue(type, value) {
    console.log(root)
    const add = (type != 'color' ? '%' : '');
    root.style.setProperty(`--${type}`, value + add)
}

这里的root是从html标签开始的整个文档树(DOM树)。我们使用root.style.setProperty将CSS变量直接赋值在文档树开头的style,就覆盖了原本CSS中的:root中的变量。

这样就可以动态赋值CSS。

参考:漫谈document.documentElement与document.body - 简书 (jianshu.com)

代码

HTML

html 复制代码
<!-- author:https://blog.csdn.net/karshey -->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dynamic CSS Variables</title>
    <link rel="stylesheet" href="style.css">
</head>

<body>
    <div class="container">
        <div class="location item">
            <p>滑动移动方块位置</p>
            <input type="range" value="0" min="0" max="200" oninput="changeValue('location',this.value)">
        </div>
        <div class="radius item">
            <p>滑动改变方块弧度</p>
            <input type="range" value="0" min="0" max="50" oninput="changeValue('radius',this.value)">
        </div>
        <div class="color item">
            <p>选择改变方块颜色</p>
            <input type="color" value="#000000" onchange="changeValue('color',this.value)">
        </div>

        <div class="box"></div>
    </div>
</body>

</html>

<script src="index.js"></script>

CSS

css 复制代码
/* 统一配置box的样式 */
:root {
    --color: #000;
    --location: 0;
    --radius: 0;
}

.container {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    height: 100vh;
    background-image: linear-gradient(120deg, #84fab0 0%, #8fd3f4 100%);
}

.item {
    display: flex;
    align-items: center;
    justify-content: center;
}

.item p {
    margin-right: 10px;
}

.box {
    height: 200px;
    width: 200px;
    
    background-color: var(--color);
    border-radius: var(--radius);
    /* 向右移动 */
    transform: translateX(var(--location));
    transition: width 2s;
}

JS

javascript 复制代码
const root = document.documentElement;
function changeValue(type, value) {
    console.log(root)
    const add = (type != 'color' ? '%' : '');
    root.style.setProperty(`--${type}`, value + add)
}
相关推荐
怕浪猫1 小时前
FDE 如何正确解决现场项目工程性的问题
前端·后端·ai编程
西瓜太郎12341 小时前
外层模型卡片与分组详情成功率不一致,应该怎么排查?
前端·typescript·go·软件工程·react
GreenTea2 小时前
vLLM 与 SGLang KV Cache 底层实现机制深度调研报告
前端·后端·算法
几何心凉5 小时前
没有万能模型:我用 AiiOnly Token Plan,把多款大模型的长处拼进同一个项目
前端
kyriewen5 小时前
Claude Code 的额度今天缩水了17%——官方公告上写的是"永久提高25%"
前端·ai编程·claude
雪芽蓝域zzs5 小时前
vue解构平铺VS对象包裹
前端·javascript·vue.js
风骏时光牛马6 小时前
从零到实战,完整AI学习路线
前端
三十而立洋7 小时前
深入理解 Monorepo:子包安装依赖
前端·前端工程化
IT_陈寒7 小时前
Java Stream处理大集合,我的内存怎么就炸了
前端·人工智能·后端
Captaincc7 小时前
AI 用量桌面端-桌面宠物自定义指南
前端·人工智能