微信小程序手动获取图片真实宽高自适应

方法一

1、视图

<image src='' bindload="imageLoad" style="width:{{imgwidth}}rpx; height:{{imgheight }}rpx;"></image>

2.1、在data中定义宽高

  data:{
    imgwidth: 0,
    imgheight: 0,
  }

2.2、和onLoad同级方法

imageLoad: function(e) {
    var $width = e.detail.width; //获取图片真实宽度
    var $height = e.detail.height; //获取图片真实高度
    this.setData({
      imgwidth: $width,
      imgheight: $height
    })
  },

方法二

微信小程序image组件

蒙层居中:

1、视图

<view catchtouchmove='move' class='mongolia'>
  <view class='popup'>
  </view>
</view>

2、样式

.mongolia {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.3);
  z-index: 99999;
}

.popup {
  position: fixed;
  top: 50%;
  left: 50%;
  max-height: 100%;
  /* background-color: #fff; */
  transform: translate3d(-50%, -50%, 0);
  z-index: 999999;
  width: 100%;
  text-align: center;
}

蒙层显示时候,底部不可以滚动

move: function() {},
<view  catchtouchmove="return">
  <view  catchtouchmove="return"></view>
  <view> // 有需要滚动的列表不要加 catchtouchmove="return", 否则列表无法滚动
      <view>滚动列表</view>
  </view>
</view>

vue的pc端
(1)获取图片的实际大小

getImageInfo(url, callback) {
   var img = new Image();
   img.src = url;
   if(img.complete) {
    // 如果图片被缓存,则直接返回缓存数据
    callback(img.width, img.height);
   } else {
    img.onload = function() {
     callback(img.width, img.height);
    }
   }
},

(2)调用

this.getImageInfo(url, function(width, height) {
    // 在这里面使用
    console.log(width);
    console.log(height);
})
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 每天的学习记录,可能有的地方写的不对,因为刚学,以后发现错的话会回来改掉整体流程 https://develope...
    有点健忘阅读 10,253评论 0 7
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,216评论 4 61
  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML标准。 注意:讲述HT...
    kismetajun阅读 28,047评论 1 45
  • 1.小程序起步 (1)点击https://mp.weixin.qq.com/wxopen/waregister?a...
    GXW_Lyon阅读 8,742评论 0 0
  • 作为一个N年的程序猿,不得不说,code的境界永无止境啊。如果你打算一辈子做好一个码农的话,必须老老实实地与时俱进...
    a85d6aa7027f阅读 3,966评论 0 7