微信小程序--图片等比例缩放 获取屏幕尺寸图片尺寸 自适应

时间:2024-10-22 11:11:05
  • //
  • function imageUtil(e) {
  • var imageSize = {};
  • var originalWidth = e.detail.width;//图片原始宽
  • var originalHeight = e.detail.height;//图片原始高
  • var originalScale = originalHeight/originalWidth;//图片高宽比
  • ('originalWidth: ' + originalWidth)
  • ('originalHeight: ' + originalHeight)
  • //获取屏幕宽高
  • ({
  • success: function (res) {
  • var windowWidth = ;
  • var windowHeight = ;
  • var windowscale = windowHeight/windowWidth;//屏幕高宽比
  • ('windowWidth: ' + windowWidth)
  • ('windowHeight: ' + windowHeight)
  • if(originalScale < windowscale){//图片高宽比小于屏幕高宽比
  • //图片缩放后的宽为屏幕宽
  • = windowWidth;
  • = (windowWidth * originalHeight) / originalWidth;
  • }else{//图片高宽比大于屏幕高宽比
  • //图片缩放后的高为屏幕高
  • = windowHeight;
  • = (windowHeight * originalWidth) / originalHeight;
  • }
  • }
  • })
  • ('缩放后的宽: ' + )
  • ('缩放后的高: ' + )
  • return imageSize;
  • }
  • = {
  • imageUtil: imageUtil
  • }