怎么在微信小程序中實現(xiàn)富文本圖片寬度自適應(yīng)?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

創(chuàng)新互聯(lián)專注于溧陽企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,購物商城網(wǎng)站建設(shè)。溧陽網(wǎng)站建設(shè)公司,為溧陽等地區(qū)提供建站服務(wù)。全流程按需求定制網(wǎng)站,專業(yè)設(shè)計,全程項目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)
思路
把圖片的寬度改為手機(jī)屏幕對應(yīng)的寬度
微信小程序需要知道的知識
需要知道微信小程序里有自己的寬度標(biāo)準(zhǔn),單位為rpx;
針對所有不同尺寸的瀏覽器,微信小程序里規(guī)定屏幕寬為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里只有圖片,并且圖片沒有設(shè)置style和寬度/高度
util.js
function formatRichText(html){
let newContent= html.replace(/\<img/gi, '<img ');
return newContent;
}
module.exports = {
formatRichText
}若artical里包含多種標(biāo)簽
util.js
/**
* 處理富文本里的圖片寬度自適應(yīng)
* 1.去掉img標(biāo)簽里的style、width、height屬性
* 2.img標(biāo)簽添加style屬性:max-width:100%;height:auto
* 3.修改所有style里的width屬性為max-width:100%
* 4.去掉<br/>標(biāo)簽
* @param html
* @returns {void|string|*}
*/
function formatRichText(html){
let newContent= html.replace(/<img[^>]*>/gi,function(match,capture){
match = match.replace(/]+"/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(/]+"/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 ');
return newContent;
}
module.exports = {
formatRichText
}看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。
分享標(biāo)題:怎么在微信小程序中實現(xiàn)富文本圖片寬度自適應(yīng)
本文地址:http://www.chinadenli.net/article4/gideoe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、自適應(yīng)網(wǎng)站、虛擬主機(jī)、網(wǎng)站收錄、搜索引擎優(yōu)化、服務(wù)器托管
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)