How to set dynamically different styles in uniapp
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>
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'
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>
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>
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')
target in the page and pass
classList The
add method of the object to add the
red-bg style sheet.
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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

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

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

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

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

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.

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.
