国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 課堂 > 小程序 > 正文

微信小程序?qū)崿F(xiàn)富文本圖片寬度自適應(yīng)的方法

2020-03-21 16:03:01
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

引言:在微信小程序里,比如商品展示頁(yè)面的商品詳情會(huì)有圖片展示,PC端設(shè)置的商品詳情是PC端的寬度,所以在小程序里圖片會(huì)顯示不全,這時(shí)就應(yīng)該做相應(yīng)的處理,使小程序里圖片顯示正確

微信小程序,富文本,圖片寬度,自適應(yīng)

思路

  • 把圖片的寬度改為手機(jī)屏幕對(duì)應(yīng)的寬度

微信小程序需要知道的知識(shí)

  • 需要知道微信小程序里有自己的寬度標(biāo)準(zhǔn),單位為rpx;
  • 針對(duì)所有不同尺寸的瀏覽器,微信小程序里規(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里只有圖片,并且圖片沒(méi)有設(shè)置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里包含多種標(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(/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}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 青龙| 古丈县| 哈巴河县| 九寨沟县| 修武县| 五指山市| 绥江县| 兴文县| 永丰县| 利辛县| 泗洪县| 湟源县| 肥东县| 卓尼县| 正阳县| 田阳县| 杭锦旗| 合肥市| 郓城县| 洞口县| 醴陵市| 汕尾市| 都兰县| 隆尧县| 榆林市| 香港| 开封市| 巫溪县| 惠水县| 锡林郭勒盟| 射洪县| 阿巴嘎旗| 都江堰市| 柏乡县| 东乌珠穆沁旗| 永昌县| 邹城市| 巧家县| 上蔡县| 长沙县| 邵阳市|