微信小程序 图片自适应高度 宽度 完美适配原生或者uniapp

--

查了一下百度看到网上图片高度自适应的解决方案

基本是靠JS获取图片的宽度进行按比例计算得出图片高度。

不是很符合我的需求/

于是我脑瓜子一转 想到一种新的解决方案 不用JS计算也能完美解决。

我写了一个组件,直接导入可以使用。

1.新建一个组件bl-adaptation-img.vue

2.在main.js中导入

复制代码
// 自适应图片图片高度 宽度100%
import blAdaptationImg from '@/components/baseUI/bl-adaptation-img/index.vue'
Vue.component('bl-adaptation-img', blAdaptationImg);

3.编写组件代码

复制代码
/**
 * 微信公众号小程序商城系统!
 * Copyright © 2024 保留所有权利
 * =========================================================
 * 版本:V1
 * 授权主体:chenfeili
 * 授权域名:*****
 * 授权码:*******
 * ----------------------------------------------
 * 您只能在商业授权范围内使用,不可二次转售、分发、分享、传播
 * 未经授权任何企业和个人不得对代码以任何目的任何形式的再发布
 * =========================================================
 */


<template>
	<bctos-rich-text v-if="nodes" :nodes="nodes"></bctos-rich-text>
</template>

<script>
	export default {
		props: {
			imgURl: {
				type: String,
				default: ''
			}
		},
		data() {
			return {
				nodes: ''
			}
		},
		watch: {
			imgURl: {
				handler(val) {
					this.nodes = val ? `<img src="${val}" style="max-width:100%;height:auto;"/>` : ''
				},
				immediate: true,
				deep: true
			}
		}
	}
</script>

<style lang="scss" scoped>
</style>

目前使用的是uniapp的富文本组件bctos-rich-text 如果是原生的小程序可以使用 <rich-text></rich-text>

4.在页面文件中使用

复制代码
<view v-if="caateInfo.flow_img" class="mt-30 pl-30 pr-30">
	<bl-adaptation-img :imgURl="caateInfo.flow_img"></bl-adaptation-img>
</view>
相关推荐
Stringzhua38 分钟前
微信小程序快速入门【02】
微信小程序·小程序
赵庆明老师2 小时前
Uniapp微信小程序开发:全局变量的使用
微信小程序·小程序·uni-app
2501_915921432 小时前
iOS 应用加固与苹果软件混淆全解析 IPA 文件防反编译、混淆加密与无源码加固策略
android·macos·ios·小程序·uni-app·cocoa·iphone
2401_885405513 小时前
定位守护童年,科技构筑安全屏障
科技·物联网·安全·小程序·宠物·web app·智能手表
lvchaoq3 小时前
记录小程序真机bug,而模拟器无法复现
小程序·bug
Jyywww1214 小时前
uniapp uni.chooseImage+uni.uploadFile使用方法与详解
开发语言·javascript·uni-app
2501_916007476 小时前
iOS 代上架实战指南,从账号管理到使用 开心上架 上传IPA的完整流程
android·macos·ios·小程序·uni-app·cocoa·iphone
2501_915918416 小时前
iOS混淆与IPA文件加固深度解析,从反编译风险到苹果应用安全工程实践
android·macos·ios·小程序·uni-app·cocoa·iphone
毛毛三由17 小时前
【uniapp】微信小程序修改按钮样式
微信小程序·小程序·uni-app
巴博尔17 小时前
uniapp的IOS中首次进入,无网络问题
前端·javascript·ios·uni-app