如何让微信小程序中的富文本图片宽度实现自适应布局?
- 内容介绍
- 文章标签
- 相关推荐
本文共计680个文字,预计阅读时间需要3分钟。
引言:在微信小程序中,商品详情展示与PC端存在差异,特别是图片显示问题。以下是对这一问题的处理建议。
在微信小程序中,商品详情展示时,图片可能因为尺寸限制而无法完整显示。为了优化用户体验,应采取以下措施:
1. 根据不同设备的屏幕尺寸,动态调整图片大小;
2.提供图片放大功能,让用户可以自由查看图片细节;
3.优化图片加载速度,确保用户流畅浏览商品详情;
4.在图片下方添加文字描述,补充图片信息。
引言:在微信小程序里,比如商品展示页面的商品详情会有图片展示,PC端设置的商品详情是PC端的宽度,所以在小程序里图片会显示不全,这时就应该做相应的处理,使小程序里图片显示正确
思路
- 把图片的宽度改为手机屏幕对应的宽度
微信小程序需要知道的知识
- 需要知道微信小程序里有自己的宽度标准,单位为rpx;
- 针对所有不同尺寸的浏览器,微信小程序里规定屏幕宽为750rpx;
解决
WXML
<view class='html_detail'> <rich-text nodes='{{artical}}'></rich-text> </view>
WXS
data={artical:''} async onLoad(){ const json = await api.getDetail(); if(json !== null){ this.artical = util.formatRichText(json.detail.description); } }
若artical里只有图片,并且图片没有设置style和宽度/高度
util.js
function formatRichText(html){ let newContent= html.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;"'); return newContent; } module.exports = { formatRichText }
若artical里包含多种标签
util.js
/** * 处理富文本里的图片宽度自适应 * 1.去掉img标签里的style、width、height属性 * 2.img标签添加style属性:max-width:100%;height:auto * 3.修改所有style里的width属性为max-width:100% * 4.去掉<br/>标签 * @param html * @returns {void|string|*} */ function formatRichText(html){ let newContent= html.replace(/<img[^>]*>/gi,function(match,capture){ match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, ''); match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, ''); match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, ''); return match; }); newContent = newContent.replace(/style="[^"]+"/gi,function(match,capture){ match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;'); return match; }); newContent = newContent.replace(/<br[^>]*\/>/gi, ''); newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;"'); return newContent; } module.exports = { formatRichText }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。
本文共计680个文字,预计阅读时间需要3分钟。
引言:在微信小程序中,商品详情展示与PC端存在差异,特别是图片显示问题。以下是对这一问题的处理建议。
在微信小程序中,商品详情展示时,图片可能因为尺寸限制而无法完整显示。为了优化用户体验,应采取以下措施:
1. 根据不同设备的屏幕尺寸,动态调整图片大小;
2.提供图片放大功能,让用户可以自由查看图片细节;
3.优化图片加载速度,确保用户流畅浏览商品详情;
4.在图片下方添加文字描述,补充图片信息。
引言:在微信小程序里,比如商品展示页面的商品详情会有图片展示,PC端设置的商品详情是PC端的宽度,所以在小程序里图片会显示不全,这时就应该做相应的处理,使小程序里图片显示正确
思路
- 把图片的宽度改为手机屏幕对应的宽度
微信小程序需要知道的知识
- 需要知道微信小程序里有自己的宽度标准,单位为rpx;
- 针对所有不同尺寸的浏览器,微信小程序里规定屏幕宽为750rpx;
解决
WXML
<view class='html_detail'> <rich-text nodes='{{artical}}'></rich-text> </view>
WXS
data={artical:''} async onLoad(){ const json = await api.getDetail(); if(json !== null){ this.artical = util.formatRichText(json.detail.description); } }
若artical里只有图片,并且图片没有设置style和宽度/高度
util.js
function formatRichText(html){ let newContent= html.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;"'); return newContent; } module.exports = { formatRichText }
若artical里包含多种标签
util.js
/** * 处理富文本里的图片宽度自适应 * 1.去掉img标签里的style、width、height属性 * 2.img标签添加style属性:max-width:100%;height:auto * 3.修改所有style里的width属性为max-width:100% * 4.去掉<br/>标签 * @param html * @returns {void|string|*} */ function formatRichText(html){ let newContent= html.replace(/<img[^>]*>/gi,function(match,capture){ match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, ''); match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, ''); match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, ''); return match; }); newContent = newContent.replace(/style="[^"]+"/gi,function(match,capture){ match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;'); return match; }); newContent = newContent.replace(/<br[^>]*\/>/gi, ''); newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;"'); return newContent; } module.exports = { formatRichText }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

