小程序image图片mode=“widthFix“图片显示瞬间竖向拉伸变形闪烁解决办法

在这里插入图片描述

在微信小程序给图片设置了宽度,然后设置了mode=“widthfix”,但是发现有的手机上会出现第一次进入页面加载图片的瞬间,图片会被拉的很长,然后恢复正常。然后把图片的高度也设置rpx的话,就不会出现这种情况了,所以小程序里图片需要设置高度。
在这里插入图片描述
官方文档中 mode=“widthFix” 宽度不变,高度自动变化,保持原图宽高比不变,即设置图片宽度而高度自适应。

但是在实际开发中发现图片在初始加载时瞬间竖向拉伸变形闪烁然后恢复正常宽高,也许是高度没设置的原因。

所以需要在app.wxss全局中把图片设置

image{
  width: 100%;
  height: auto;
}

即可解决问题

  • 16
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论
以下是一个简单的微信小程序拉伸图片四个角变形的示例代码: wxml文件: ```html <view class="container"> <canvas class="canvas" canvas-id="canvas"></canvas> </view> ``` js文件: ```javascript Page({ data: { imageSrc: '', // 图片路径 startX: 0, startY: 0, imgWidth: 0, imgHeight: 0, isMoving: false, }, onLoad() { // 加载图片 wx.getImageInfo({ src: 'https://example.com/image.png', success: (res) => { this.setData({ imageSrc: res.path, }) this.initCanvas() }, }) }, // 初始化canvas initCanvas() { const ctx = wx.createCanvasContext('canvas', this) ctx.drawImage(this.data.imageSrc, 0, 0, 300, 300) ctx.draw() // 获取图片信息 wx.getImageInfo({ src: this.data.imageSrc, success: (res) => { this.setData({ imgWidth: res.width, imgHeight: res.height, }) }, }) }, // 触摸开始 touchStart(e) { const { x, y } = e.touches[0] this.setData({ startX: x, startY: y, isMoving: true, }) }, // 触摸移动 touchMove(e) { if (!this.data.isMoving) { return } const { x, y } = e.touches[0] const offsetX = x - this.data.startX const offsetY = y - this.data.startY const ctx = wx.createCanvasContext('canvas', this) // 左上角 if (offsetX < 0 && offsetY < 0) { ctx.drawImage(this.data.imageSrc, x, y, -offsetX, -offsetY, 0, 0, -offsetX, -offsetY) } // 右上角 else if (offsetX > 0 && offsetY < 0) { ctx.drawImage(this.data.imageSrc, 0, y, this.data.imgWidth + offsetX, -offsetY, offsetX, 0, this.data.imgWidth + offsetX, -offsetY) } // 左下角 else if (offsetX < 0 && offsetY > 0) { ctx.drawImage(this.data.imageSrc, x, 0, -offsetX, this.data.imgHeight + offsetY, 0, offsetY, -offsetX, this.data.imgHeight + offsetY) } // 右下角 else { ctx.drawImage(this.data.imageSrc, 0, 0, this.data.imgWidth + offsetX, this.data.imgHeight + offsetY, offsetX, offsetY, this.data.imgWidth + offsetX, this.data.imgHeight + offsetY) } ctx.draw() }, // 触摸结束 touchEnd() { this.setData({ isMoving: false, }) }, // 保存图片 saveImage() { wx.canvasToTempFilePath({ x: 0, y: 0, width: 300, height: 300, destWidth: 300, destHeight: 300, canvasId: 'canvas', success: (res) => { wx.saveImageToPhotosAlbum({ filePath: res.tempFilePath, }) }, }, this) }, }) ``` css文件: ```css .container { display: flex; justify-content: center; align-items: center; width: 100%; height: 100vh; } .canvas { width: 300rpx; height: 300rpx; border: 1px solid #ccc; } ``` 注:以上代码仅作为示例,实际应用可能需要根据具体需求进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柠檬树上柠檬果柠檬树下你和我

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值