uniapp dynamic modification of element node style detailed explanation

uniapp dynamic modification of element node style detailed explanation

1. Modify by binding the style attribute

The first step: you must get the element node

There is no windou object in the uniApp project, so the dom node cannot be directly obtained through the document. The refs of vue are only valid for custom components and not for tags in uniapp.

Check the uniapp official website for a uni.createSelectorQuery() API; you can use this property to get the style of the label and then modify it by dynamically binding the style;

html:

<button type="default" @click="handleFont">Click to increase font</button>
<view class="weibo_box" id='index0' :style="{fontSize:vHeight + 'px'}">

Corresponding js:

data(){
	return {
		vHeight:22
	}
},


handleFont(){
	const that=this
	uni.createSelectorQuery().select('#index0').boundingClientRect(function (data) { 
	  console.log('element information 0:', data)
		  that.vHeight +=10
	}).exec()
}

The effect achieved:

insert image description here

Second, use ref to get the DOM element node

Code:

<button type="default" @click="handleFont">Click to increase font</button>
<view class="weibo_box" id='index1' ref="mydom">
	The second one
data(){
	return {
		vHeight:22
	}
},
//Some codes are irrelevant, omit handleFont(){
	const that=this
	that.$refs.mydom.$el.style.fontSize=that.vHeight+=1+'px'
}

The effect achieved:

insert image description here

Summarize

This article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • Detailed explanation of the difference between uniapp and vue
  • Detailed explanation of uniapp painless token refresh method
  • Detailed explanation of uniapp's global variable implementation
  • Detailed explanation of styles in uni-app

<<:  How to quickly copy large files under Linux

>>:  Summary of knowledge points related to null (IFNULL, COALESCE and NULLIF) in MySQL

Recommend

A quick guide to MySQL indexes

The establishment of MySQL index is very importan...

How to deploy Redis 6.x cluster through Docker

System environment: Redis version: 6.0.8 Docker v...

TypeScript installation and use and basic data types

The first step is to install TypeScript globally ...

How to view the creation time of files in Linux

1. Introduction Whether the creation time of a fi...

CSS to achieve zoom in and out close button (example code)

This effect is most common on our browser page. L...

Specific use of Linux which command

We often want to find a file in Linux, but we don...

Detailed explanation of Angular parent-child component communication

Table of contents Overview 1. Overview of input a...

Two usages of iFrame tags in HTML

I have been working on a project recently - Budou...

Use of Docker image storage overlayfs

1. Overview The image in Docker is designed in la...

Summary of some common writing methods that cause MySQL index failure

Preface Recently, I have been busy dealing with s...

Angular Cookie read and write operation code

Angular Cookie read and write operations, the cod...

CSS3 category menu effect

The CSS3 category menu effects are as follows: HT...

Detailed explanation of Vue project optimization and packaging

Table of contents Preface 1. Routing lazy loading...