当前位置:   article > 正文

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

微信小程序图片高度自适应


 

--

-

-

-

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

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

不是很符合我的需求/

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

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

-

-

-

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

2.在main.js中导入

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

 3.编写组件代码

  1. /**
  2. * 微信公众号小程序商城系统!
  3. * Copyright © 2024 保留所有权利
  4. * =========================================================
  5. * 版本:V1
  6. * 授权主体:chenfeili
  7. * 授权域名:*****
  8. * 授权码:*******
  9. * ----------------------------------------------
  10. * 您只能在商业授权范围内使用,不可二次转售、分发、分享、传播
  11. * 未经授权任何企业和个人不得对代码以任何目的任何形式的再发布
  12. * =========================================================
  13. */
  14. <template>
  15. <bctos-rich-text v-if="nodes" :nodes="nodes"></bctos-rich-text>
  16. </template>
  17. <script>
  18. export default {
  19. props: {
  20. imgURl: {
  21. type: String,
  22. default: ''
  23. }
  24. },
  25. data() {
  26. return {
  27. nodes: ''
  28. }
  29. },
  30. watch: {
  31. imgURl: {
  32. handler(val) {
  33. this.nodes = val ? `<img src="${val}" style="max-width:100%;height:auto;"/>` : ''
  34. },
  35. immediate: true,
  36. deep: true
  37. }
  38. }
  39. }
  40. </script>
  41. <style lang="scss" scoped>
  42. </style>

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

4.在页面文件中使用

  1. <view v-if="caateInfo.flow_img" class="mt-30 pl-30 pr-30">
  2. <bl-adaptation-img :imgURl="caateInfo.flow_img"></bl-adaptation-img>
  3. </view>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/954066
推荐阅读
相关标签
  

闽ICP备14008679号