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

Detailed explanation of JavaScript program loop structure

Table of contents Select Structure Loop Structure...

Navicat Premium operates MySQL database (executes sql statements)

1. Introduction to Navicat 1. What is Navicat? Na...

Sublime Text - Recommended method for setting browser shortcut keys

It is common to view code effects in different br...

A brief analysis of the differences between undo, redo and binlog in MySQL

Table of contents Preface 【undo log】 【redo log】 【...

Implementation of importing and exporting vue-element-admin projects

vue-element-admin import component encapsulation ...

Detailed explanation of JavaScript's built-in objects Math and strings

Table of contents Math Objects Common properties ...

Why is there this in JS?

Table of contents 1. Demand 2. Solution 3. The fi...

How to completely uninstall node and npm on mac

npm uninstall sudo npm uninstall npm -g If you en...

How to use Docker container to access host network

Recently, a system was deployed, using nginx as a...

Semantics, writing, and best practices of link A

The semantics, writing style, and best practices ...

Two methods to stretch the background image of a web page

There are two solutions: One is CSS, using backgro...

Implementation of postcss-pxtorem mobile adaptation

Execute the command to install the plugin postcss...

Introduction to query commands for MySQL stored procedures

As shown below: select name from mysql.proc where...

MySQL database basic syntax and operation

MySQL database basic syntax DDL Operations Create...