【前端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)
}
相关推荐
MickeyCV25 分钟前
Nginx学习笔记:常用命令&端口占用报错解决&Nginx核心配置文件解读
前端·nginx
eggcode40 分钟前
CSS通过webkit-scrollbar设置滚动条样式
css·webkit·scrollbar
祈澈菇凉42 分钟前
webpack和grunt以及gulp有什么不同?
前端·webpack·gulp
zy0101011 小时前
HTML列表,表格和表单
前端·html
初辰ge1 小时前
【p-camera-h5】 一款开箱即用的H5相机插件,支持拍照、录像、动态水印与样式高度定制化。
前端·相机
HugeYLH1 小时前
解决npm问题:错误的代理设置
前端·npm·node.js
六个点2 小时前
DNS与获取页面白屏时间
前端·面试·dns
道不尽世间的沧桑2 小时前
第9篇:插槽(Slots)的使用
前端·javascript·vue.js
bin91532 小时前
DeepSeek 助力 Vue 开发:打造丝滑的滑块(Slider)
前端·javascript·vue.js·前端框架·ecmascript·deepseek
uhakadotcom2 小时前
最新发布的Tailwind CSS v4.0提供了什么新能力?
前端