Table of Contents
Dynamic setting of styles
Dynamic setting of different styles
Changing styles by judging conditions
Changing styles through style sheets
Home Web Front-end uni-app How to set dynamically different styles in uniapp

How to set dynamically different styles in uniapp

Apr 27, 2023 am 09:04 AM

With the continuous development of the mobile application market and the increasing user demand for mobile applications, developers have gradually embarked on a multi-terminal sharing path for mobile application development. Mobile applications also need to provide different services in different scenarios. Visual effects and interactive experience, and Uniapp can meet this demand. By writing a code, you can achieve differentiated output of visual effects on different terminals and different resolutions.

Uniapp is a front-end framework developed based on Vue.js. It can implement one code to build applications for multiple platforms, including WeChat applet, H5, Alipay applet, APP, etc. This article will focus on how to set different styles for Uniapp dynamically.

Dynamic setting of styles

In Uniapp, if you want to dynamically set the style of a component, you can use the :style attribute to achieve this. For example, define a view component in the vue file, and then set its style through the :style attribute:

<template>
  <view :style="dynamicStyle"></view>
</template>

<script>
export default {
  data () {
    return {
      dynamicStyle: {
        backgroundColor: '#f0f0f0'
      }
    }
  }
}
</script>
Copy after login

In the above code, we defined a view component, and set a dynamicStyle variable through the data attribute, in which we set the backgroundColor to gray. Then, we use the :style attribute in the view component to dynamically set the style.

Here we only set a simple style attribute. In fact, we can also set more attributes, including font-size, width, height, margin, padding, etc.

However, using the above method to set styles can only achieve global style settings. If a certain scene requires setting different styles, the styles need to be changed dynamically.

Dynamic setting of different styles

The above mentioned how to dynamically set styles, the following will introduce how to dynamically set different styles.

Uniapp supports operating styles through JavaScript, which makes it possible for us to dynamically set different styles.

For example, for the view component, we can change its properties through JavaScript to modify the component style:

this.$refs.target.style.backgroundColor = '#F00'
Copy after login

In the above code, we first obtain Go to the ref of the view component, and then dynamically change the background color of the component through style.

So, how to implement different style settings according to different scenarios in Uniapp?

Changing styles by judging conditions

The first way to implement it is to dynamically change styles by judging conditions. For example, we can control the settings of different styles by judging the device type.

Here is an example of determining whether the device is an iOS device:

<template>
  <view :style="dynamicStyle"></view>
</template>

<script>
export default {
  data () {
    return {
      dynamicStyle: {}
    }
  },
  onLoad () {
    // 判断是否是 iOS 设备
    const isIOS = uni.getSystemInfoSync().platform === 'ios'
    if (isIOS) {
      this.dynamicStyle.backgroundColor = '#ff0'
    } else {
      this.dynamicStyle.backgroundColor = '#f00'
    }
  }
}
</script>
Copy after login

In the above code, we first define an empty object dynamicStyle, and then onLoad Determine the device type in the life cycle hook function. If it is an iOS device, set the background color to yellow, otherwise set it to red.

In this way, we can dynamically change the style according to different conditions, thereby achieving differentiation of different styles.

Changing styles through style sheets

The second implementation method is to dynamically introduce style sheets to control the settings of different styles.

First, we need to write the style sheet in the style tag. For example, we define a style sheet named red-bg:

<style>
.red-bg {
  background-color: #f00;
}
</style>
Copy after login

Then, when we need it, we can dynamically introduce the style sheet through the this.$refs object. For example, we need to add ## to a certain component in the page when a certain condition is true. #red-bg style, you can write the code like this:

this.$refs.target.classList.add('red-bg')
Copy after login
In the above code, we get the component named

target in the page and pass classList The add method of the object to add the red-bg style sheet.

In this way, we can introduce different style sheets in different situations to achieve different style settings.

Summary

This article mainly introduces how to set dynamically different styles in Uniapp, and control the setting of different styles by judging conditions or dynamically introducing style sheets.

In actual development, we need to choose different implementation methods according to specific business scenarios and needs, so as to build mobile applications that meet user needs.

The above is the detailed content of How to set dynamically different styles in uniapp. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How do I handle local storage in uni-app? How do I handle local storage in uni-app? Mar 11, 2025 pm 07:12 PM

This article details uni-app's local storage APIs (uni.setStorageSync(), uni.getStorageSync(), and their async counterparts), emphasizing best practices like using descriptive keys, limiting data size, and handling JSON parsing. It stresses that lo

How to rename UniApp download files How to rename UniApp download files Mar 04, 2025 pm 03:43 PM

This article details workarounds for renaming downloaded files in UniApp, lacking direct API support. Android/iOS require native plugins for post-download renaming, while H5 solutions are limited to suggesting filenames. The process involves tempor

How to handle file encoding with UniApp download How to handle file encoding with UniApp download Mar 04, 2025 pm 03:32 PM

This article addresses file encoding issues in UniApp downloads. It emphasizes the importance of server-side Content-Type headers and using JavaScript's TextDecoder for client-side decoding based on these headers. Solutions for common encoding prob

How do I use uni-app's geolocation APIs? How do I use uni-app's geolocation APIs? Mar 11, 2025 pm 07:14 PM

This article details uni-app's geolocation APIs, focusing on uni.getLocation(). It addresses common pitfalls like incorrect coordinate systems (gcj02 vs. wgs84) and permission issues. Improving location accuracy via averaging readings and handling

How do I make API requests and handle data in uni-app? How do I make API requests and handle data in uni-app? Mar 11, 2025 pm 07:09 PM

This article details making and securing API requests within uni-app using uni.request or Axios. It covers handling JSON responses, best security practices (HTTPS, authentication, input validation), troubleshooting failures (network issues, CORS, s

How do I manage state in uni-app using Vuex or Pinia? How do I manage state in uni-app using Vuex or Pinia? Mar 11, 2025 pm 07:08 PM

This article compares Vuex and Pinia for state management in uni-app. It details their features, implementation, and best practices, highlighting Pinia's simplicity versus Vuex's structure. The choice depends on project complexity, with Pinia suita

How do I use uni-app's social sharing APIs? How do I use uni-app's social sharing APIs? Mar 13, 2025 pm 06:30 PM

The article details how to integrate social sharing into uni-app projects using uni.share API, covering setup, configuration, and testing across platforms like WeChat and Weibo.

How do I use uni-app's easycom feature for automatic component registration? How do I use uni-app's easycom feature for automatic component registration? Mar 11, 2025 pm 07:11 PM

This article explains uni-app's easycom feature, automating component registration. It details configuration, including autoscan and custom component mapping, highlighting benefits like reduced boilerplate, improved speed, and enhanced readability.

See all articles