样式的双向绑定的2种方式,实现样式交互效果

与样式标签实现双向绑定

  • 通过布尔值来决定样式是出现还是消失

    show代表着布尔值,show的初始值是false所以文本不会有高亮的效果,当用户点击了按钮,就会调用shows这个函数,并将show的相反值true赋值并覆盖给show,此时show的值为true,这个时候样式起效实现高亮效果

vue 复制代码
<template>
<div :class="{'backgroundColor': show}">王侯将相另有种乎</div>
<div><button @click="shows">高亮</button></div>
</template>

<script setup>
import {ref} from 'vue'
const show = ref(false)
const shows = () =>{
  show.value =! show.value
}
</script>

<style>
.backgroundColor{
  background-color: yellow
}
</style>
  • 效果如下:
  • 列表格式的样式绑定

    用于实现多从复杂的效果样式,指那些通过用户交互实现双向绑定的样式效果,例如用户在色彩盘中选择颜色,对应背景颜色,字体等样式发生改变。这样使得页面上的效果有了交互的效果,可以用于用户的自定义个性化界面!!!在双向样式绑定数组中可以无限添加新的样式,若出现重复的样式,后面的样式会覆盖前面的样式。

vue 复制代码
<template>
  <p     :style="[obj,obj2]"   >
      Lorem ipsum dolor, sit amet consectetur adipisicing elit. Quas eum suscipit beatae hic omnis. Quisquam saepe recusandae quas in, esse ipsum eius id perspiciatis minus earum? Qui nemo atque neque!

      Lorem ipsum dolor sit amet consectetur adipisicing elit. Quos debitis enim quis possimus, natus quia voluptatem vero amet numquam, necessitatibus, ratione deserunt culpa similique aperiam facilis modi ducimus officiis? Aspernatur.
  </p>
  <hr>
调整字体的颜色:
  <input type="color" v-model="obj.color">
  <hr>
调整背景的颜色:
<input type="color"  v-model="obj.backgroundColor">
<hr>
调整字体的大小
  <input type="text" v-model="obj.fontSize">
  <hr>
调整边框的弧度
  <!--  v-bind   属性绑定, 绑定的值里面是js的合法表达式    -->
  <input type="range"  :min=" minval *2 + 9"  :max="maxval" v-model="ccc">
  <hr>
</template>
<script   setup>
import {ref,  reactive  , computed} from 'vue'
const minval=ref(1);
const maxval =ref(50);
const ccc =ref(0)
// 实现边框弧度的调节
const bor = computed(

  ()  =>   ccc.value + 'px'

);
const obj = reactive({
  color: '',
  fontSize: "",
  backgroundColor:'', 
  'border-radius': '1px',
  border:'2px dashed',
  padding:'20px',

})

const obj2 = reactive(
  {
      margin:"200px",
      borderRadius:bor
  }
)
</script>


<style>

p {
  color: red;
  border: 1px solid;
  background-color: lightblue;
  border: 2px dashed;
  padding: 20px;

 
}
</style>
  • 效果如下:

相关推荐
不会敲代码11 小时前
TCP/IP 与前端性能:从数据包到首次渲染的底层逻辑
前端·tcp/ip
kyriewen1 小时前
奥特曼借GPT-5.5干杯,而你的Copilot正按Token收钱
前端·github·openai
AC赳赳老秦1 小时前
投标合规提效:用 OpenClaw 实现标书 / 合同自动审核、关键词校验、格式优化,降低废标风险
开发语言·前端·python·eclipse·emacs·deepseek·openclaw
kyriewen1 小时前
代码写成一锅粥?3个设计模式让你的项目“起死回生”
前端·javascript·设计模式
不会敲代码11 小时前
从零搭建 AI 日记助手:用 Milvus 向量数据库实现语义搜索
javascript·openai
千寻girling2 小时前
《 Git 详细教程 》
前端·后端·面试
threelab3 小时前
Three.js UV 图像变换效果 | 三维可视化 / AI 提示词
javascript·人工智能·uv
之歆3 小时前
DAY08_CSS浮动与行内块布局实战指南(下)
前端·css
yqcoder3 小时前
CSS Position 全解析:5 种定位模式详解
前端·css
Rhi6374 小时前
从零搭建项目:React 19 + Vite 8 + Tailwind CSS v4 实战配置
前端