样式的双向绑定的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>
  • 效果如下:

相关推荐
刘发财4 小时前
弃用html2pdf.js,这个html转pdf方案能力是它的几十倍
前端·javascript·github
牛奶7 小时前
2026年大模型怎么选?前端人实用对比
前端·人工智能·ai编程
牛奶7 小时前
前端人为什么要学AI?
前端·人工智能·ai编程
Kagol9 小时前
🎉OpenTiny NEXT-SDK 重磅发布:四步把你的前端应用变成智能应用!
前端·开源·agent
GIS之路10 小时前
ArcGIS Pro 中的 notebook 初识
前端
JavaGuide11 小时前
7 道 RAG 基础概念知识点/面试题总结
前端·后端
ssshooter11 小时前
看完就懂 useSyncExternalStore
前端·javascript·react.js
格砸12 小时前
从入门到辞职|从ChatGPT到OpenClaw,跟上智能时代的进化
前端·人工智能·后端
Live0000012 小时前
在鸿蒙中使用 Repeat 渲染嵌套列表,修改内层列表的一个元素,页面不会更新
前端·javascript·react native
柳杉12 小时前
使用Ai从零开发智慧水利态势感知大屏(开源)
前端·javascript·数据可视化