How to use style binding in Vue3, related instructions and some practical application scenarios

Styles play a vital role in front-end development, providing beauty and usability to a web page or application. In Vue3, style bindings are a convenient and flexible way to dynamically control the style of an element. This article will introduce in detail how to use style binding in Vue3, related instructions and some practical application scenarios.

Basic Style Binding

Class binding

In Vue3, we can use v-binddirectives or shorthand :for style binding. For the binding of class names, we can implement it through object syntax or array syntax. Here is an example:

<template>
  <div>
    <p :class="{ 'red': isRed }">Hello, Vue3!</p>
    <button @click="toggleRed">Toggle Red</button>
  </div>
</template>

<script>
import { ref } from 'vue'

export default {
  setup() {
    const isRed = ref(false)

    const toggleRed = () => {
      isRed.value = !isRed.value
    }

    return {
      isRed,
      toggleRed
    }
  }
}
</script>

<style>
.red {
  color: red;
}
</style>

In the above code, we :classbind an object using the directive, and when ,isRed the class name is added to the label, making the text appear red. By clicking the button, we can switch the value, so as to dynamically change the style.truered<p>isRed

In addition to object syntax, we can also use array syntax to bind class names. Here is an example:

<template>
  <div>
    <p :class="[color, size]">Hello, Vue3!</p>
    <button @click="toggleStyle">Toggle Style</button>
  </div>
</template>

<script>
import { ref } from 'vue'

export default {
  setup() {
    const color = ref('red')
    const size = ref('big')

    const toggleStyle = () => {
      if (color.value === 'red') {
        color.value = 'blue'
        size.value = 'small'
      } else {
        color.value = 'red'
        size.value = 'big'
      }
    }

    return {
      color,
      size,
      toggleStyle
    }
  }
}
</script>

<style>
.red {
  color: red;
}

.blue {
  color: blue;
}

.big {
  font-size: 24px;
}

.small {
  font-size: 14px;
}
</style>

In the above code, we use :classinstructions to bind an array, and the elements in the array correspond to different class names. By clicking the button, we can switch the value of colorand sizeto achieve dynamic style change.

Style binding

In addition to the binding of class names, we can also directly bind inline styles. In Vue3, we can implement style binding through object or array syntax. Here is an example:

<template>
  <div>
    <p :style="{ color: textColor, fontSize: textSize + 'px' }">Hello, Vue3!</p>
    <button @click="changeStyle">Change Style</button>
  </div>
</template>

<script>
import { ref } from 'vue'

export default {
  setup() {
    const textColor = ref('red')
    const textSize = ref(16)

    const changeStyle = () => {
      if (textColor.value === 'red') {
        textColor.value = 'blue'
        textSize.value = 24
      } else {
        textColor.value = 'red'
        textSize.value = 16
      }
    }

    return {
      textColor,
      textSize,
      changeStyle
    }
  }
}
</script>

In the above code, we use :styleinstructions to bind an object, and the properties of the object correspond to different style properties. By clicking the button, we can switch the value of textColorand textSizeto achieve dynamic style change.

In addition to object syntax, we can also use array syntax to bind inline styles. Here is an example:

<template>
  <div>
    <p :style="[color, size]">Hello, Vue3!</p>
    <button @click="toggleStyle">Toggle Style</button>
  </div>
</template>

<script>
import { ref } from 'vue'

export default {
  setup() {
    const color = ref({ color: 'red' })
    const size = ref({ fontSize: '24px' })

    const toggleStyle = () => {
      if (color.value.color === 'red') {
        color.value.color = 'blue'
        size.value.fontSize = '14px'
      } else {
        color.value.color = 'red'
        size.value.fontSize = '24px'
      }
    }

    return {
      color,
      size,
      toggleStyle
    }
  }
}
</script>

In the above code, we use :styleinstructions to bind an array, and the elements in the array correspond to different style objects. By clicking the button, we can switch the value of colorand sizeto achieve dynamic style change.

Conditional style binding

Use ternary expressions

In Vue3, we can use ternary expressions for conditional style binding. Here is an example:

<template>
  <div>
    <p :class="isRed ? 'red' : ''">Hello, Vue3!</p>
    <button @click="toggleRed">Toggle Red</button>
  </div>
</template>

<script>
import { ref } from 'vue'

export default {
  setup() {
    const isRed = ref(false)

    const toggleRed = () => {
      isRed.value = !isRed.value
    }

    return {
      isRed,
      toggleRed
    }
  }
}
</script>

<style>
.red {
  color: red;
}
</style>

In the above code, we judge whether to add reda class name through a ternary expression. When isRedistrue , the text will be displayed in red; otherwise, no class name will be added, and the default style will remain.

Using computed properties

In addition to ternary expressions, we can also use computed properties for conditional style binding. Here is an example:

<template>
  <div>
    <p :class="textClass">Hello, Vue3!</p>
    <button @click="toggleStyle">Toggle Style</button>
  </div>
</template>

<script>
import { ref, computed } from 'vue'

export default {
  setup() {
    const isRed = ref(false)
    const isBold = ref(true)

    const textClass = computed(() => {
      let classes = []

      if (isRed.value) {
        classes.push('red')
      }

      if (isBold.value) {
        classes.push('bold')
      }

      return classes.join(' ')
    })

    const toggleStyle = () => {
      isRed.value = !isRed.value
      isBold.value = !isBold.value
    }

    return {
      isRed,
      isBold,
      textClass,
      toggleStyle
    }
  }
}
</script>

<style>
.red {
  color: red;
}

.bold {
  font-weight: bold;
}
</style>

In the above code, we have used computed properties textClassto dynamically generate class names. According to the value of isRedand isBold, we add the corresponding class name to classesthe array and join()convert it to a string through the method. In the end, this string will be used as :classthe value of the binding, thus realizing conditional style binding.

Responsive style binding

In Vue3, style binding can also be combined with responsive data to achieve dynamic style control. Here is an example:

<template>
  <div>
    <p :class="{ red: isRed }" :style="{ fontSize: textSize + 'px' }">Hello, Vue3!</p>
    <button @click="increaseFontSize">Increase Font Size</button>
  </div>
</template>

<script>
import { ref } from 'vue'

export default {
  setup() {
    const isRed = ref(false)
    const textSize = ref(16)

    const increaseFontSize = () => {
      textSize.value += 2
    }

    return {
      isRed,
      textSize,
      increaseFontSize
    }
  }
}
</script>

<style>
.red {
  color: red;
}
</style>

In the above code, we will use it isRedas the judgment condition of the class name, when it trueis , the text will be displayed in red. At the same time, we will textSizedynamically change the font size by increasing the button click event as the value of the inline style.

Summarize

Vue3 provides a flexible and convenient way to implement style binding. We can use :classdirectives to bind class names, and implement different style controls through object or array syntax. Directives :styleare used to bind inline styles, and dynamic style adjustments can be achieved through object or array syntax. In addition, we can also use ternary expressions or computed properties to implement conditional style binding, and combine with responsive data to implement responsive style control.

Guess you like

Origin blog.csdn.net/weixin_43025343/article/details/131788850