知識
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價值,我們在追求其視覺表現(xiàn)的同時,更側(cè)重于功能的便捷,營銷的便利,運營的高效,讓網(wǎng)站成為營銷工具,讓軟件能切實提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序為后期升級提供便捷的支持!
您當前位置>首頁 » 新聞資訊 » 小程序相關(guān) >
微信小程序 Notes | 開發(fā)常用事例(四)
發(fā)表時間:2021-1-5
發(fā)布人:葵宇科技
瀏覽次數(shù):62
1、List item 和 button 沖突怎么玩?
這個事情是這樣的,由于韓總死乞白賴的非要列表新增轉(zhuǎn)發(fā) PDF 功能,由于微信小程序限制只有 button 才具有開放的一些權(quán)限,所以直接采用 button 包裹 image 的方案,如下:
<view class="news">
<block wx:for="{{ newsList }}" wx:for-index="index" wx:key="news">
<view class="news-item" bindtap="onNewsItemClick" data-newsID="{{ item.newID }}">
<text class="content">{{ item.content }}</text>
<view class="item_new_bottom">
<text class="createTime">{{ item.createTime }}</text>
<button open-type="share" bindtap="onShareAppMessage" hover-stop-propagation="true"
data-shareid="{{ item.newID }}">
<image src="/images/ic_share.svg" mode="aspectFit"></image>
</button>
</view>
</view>
<van-divider wx:if="{{ index != newsList.length -1 }}" />
</block>
</view>
效果如下:
有個比較尷尬的問題是,當點擊分享圖標時,對應(yīng)的 item 事件也會執(zhí)行,查閱了官方手冊后。
將 button 事件類型調(diào)整為:catchtap 即可。
針對這兩種方式,這里簡單理解下。
- 使用 catchtap 方式暖男,且只對你一個人暖,也就是說事件不會再次向上傳遞,自我消費;
- bindtap 方式則是渣男,挨個寵幸。
2、如何分享頁面并攜帶參數(shù)?
這個需求是真惡心,先來看下效果圖:
簡單說下步驟:
- button 設(shè)置 open-type 為 share;
- onShareAppMessage() 中設(shè)置 title、path 以及 imageUrl;
- onLoad 中接收到解析即可。
下面附上關(guān)鍵 js 代碼:
/**
* 生命周期函數(shù)--監(jiān)聽頁面加載
*/
onLoad: function (options) {
let sharePDFId = parseInt(options.sharePDFId);
if (sharePDFId >= 0) {
var newFilePath = wx.env.USER_DATA_PATH + '/' + this.data.newsList[sharePDFId].content + '.pdf';
let downloadPDFUrl = this.data.newsList[sharePDFId].pdfUrl;
handleLoadPDF(newFilePath, downloadPDFUrl);
}
},
/**
* 用戶點擊右上角分享
*/
onShareAppMessage: function (res) {
let that = this;
let sharePDFId = parseInt(res.target.dataset.shareid);
return {
title: that.data.newsList[sharePDFId].content + '.pdf',
path: '/pages/index/index?sharePDFId=' + sharePDFId,
imageUrl: urlUtils.getComposeUrl('/images/img_share_pdf.png')
}
},
3、如何實現(xiàn)列表點擊 item title 變色,markers 同時變色?
先來看個效果吧,可能我得描述不是那么準確:
思路都是一樣的:
- 知曉當前 item 點擊位置;
- 更新 markers 中對應(yīng)的 marker。
給出部分頁面代碼:
<view class="port_desc">
<map id="map" bindmarkertap="onMarkersClick" setting="{{ setting }}" show-location markers="{{ markers }}"/>
<scroll-view scroll-y>
<block wx:for="{{ portList }}" wx:key="port">
<view class="item_port" bindtap="onPortItemClick" data-portid="{{ item.portId }}" data-index="{{ index }}">
</block>
</scroll-view>
</view>
對應(yīng)的 js 文件:
Page({
/**
* 頁面的初始數(shù)據(jù)
*/
data: {
mCurPosititon: 0,
markers: [{
iconPath: "/images/icon_prot.png",
id: 0,
latitude: 19.731021,
longitude: 109.205006,
width: 30,
height: 36
}, {
iconPath: "/images/icon_prot.png",
id: 1,
latitude: 20.022159,
longitude: 110.283528,
width: 30,
height: 36
}],
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面顯示
*/
onShow: function () {
this.refreshMarkers(0);
},
/**
* 港口 item 點擊 - 地圖 markers 平移
*/
onPortItemClick: function (event) {
let that = this;
let currentId = event.currentTarget.dataset.portid;
let portBean = that.data.portList[currentId];
// 平移 markers 到地圖中心
this.mapContext.moveToLocation({
longitude: portBean.longitude,
latitude: portBean.latitude,
success(res) {
console.log('---> 平移成功 ', res);
},
fail(err) {
console.log('---> 平移失敗 ', err);
}
});
that.refreshMarkers(1);
that.setData({
mCurPosititon: event.currentTarget.dataset.index,
});
that.refreshMarkers(0);
},
/**
* 刷新當前選中的 Markers 點
*/
refreshMarkers: function (type) {
let that = this;
var tempMarkers = that.data.markers;
tempMarkers[that.data.mCurPosititon].iconPath = type == 0 ? '/images/icon_prot_sel.png' : '/images/icon_prot.png';
that.setData({
markers: tempMarkers,
});
}
})
有時候想想,這東西真的是相通的。遇到問題,靜下心來,慢慢梳理,別慌。
4、wxml 中的三元運算符使用
這個比較 easy,直接放上代碼:
<text class="title" style="color:{{ mCurPosititon == index ? 'red' : 'black' }}">{{ item.title }}</text>
5、map 如何自定義氣泡窗口,支持動態(tài)切換,并且支持點擊?
先來看個效果圖,一目了然:
這塊內(nèi)容相對 easy,直接放上代碼咯。
首先是 js 關(guān)鍵代碼:
/**
* 頁面的初始數(shù)據(jù)
*/
data: {
portName: '',
markerId: 0,
markers: [{
id: 0, iconPath: "/images/icon_prot.png",
latitude: 19.731021, longitude: 109.205006,
width: 30, height: 36, customCallout: {
anchorX: 0,
anchorY: 0,
display: "ALWAYS"
}
}, {
id: 1, iconPath: "/images/icon_prot.png",
latitude: 20.022159, longitude: 110.283528,
width: 30, height: 36, customCallout: {
anchorX: 0,
anchorY: 0,
display: "ALWAYS"
}
}, ],
portList: [{
portId: 0, markerId: 0, title: '洋浦港',
desc: '洋浦港....',
avatar: 'https:/xxxx9e.jpg',
latitude: 19.731021, longitude: 109.205006,
}, {
portId: 1, markerId: 1, title: '??诟?,
desc: '??诟踴xx',
avatar: 'https://xxxae.jpeg',
latitude: 20.022159, longitude: 110.283528,
}, ]
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面顯示
*/
onShow: function () {
let that = this;
// 初始化數(shù)據(jù)
let portBean = that.data.portList[0];
that.setData({
portName: portBean.title,
markerId: portBean.markerId
});
},
/**
* 港口 item 點擊 - 地圖 markers 平移
*/
onPortItemClick: function (event) {
let that = this;
let currentId = event.currentTarget.dataset.portid;
let portBean = that.data.portList[currentId];
// 平移 markers 到地圖中心
this.mapContext.moveToLocation({
longitude: portBean.longitude,
latitude: portBean.latitude,
success(res) {
console.log('---> 平移成功 ', res);
},
fail(err) {
console.log('---> 平移失敗 ', err);
}
});
that.refreshMarkers(1);
that.setData({
mCurPosititon: event.currentTarget.dataset.index,
});
that.refreshMarkers(0);
// 更新氣泡數(shù)據(jù)
that.setData({
portName: portBean.title,
markerId: portBean.markerId
});
},
/**
* Markers 點擊查看詳情
*/
bindcallouttap: function (event) {
let markerId = parseInt(event.detail.markerId); // 其實這就是 id,為了實現(xiàn)對應(yīng)的詳情切換
wx.navigateTo({
url: '/pages/portDetail/portDetail?portId=' + markerId
})
},
/**
* 刷新當前選中的 Markers 點
*/
refreshMarkers: function (type) {
let that = this;
var tempMarkers = that.data.markers;
tempMarkers[that.data.mCurPosititon].iconPath = type == 0 ? '/images/icon_prot_sel.png' : '/images/icon_prot.png';
that.setData({
markers: tempMarkers,
});
}
然后就是對應(yīng)的 wxml 關(guān)鍵代碼:
<map bindcallouttap="bindcallouttap" id="map" setting="{{ setting }}" show-location markers="{{ markers }}">
<cover-view slot="callout">
<cover-view marker-id="{{ markerId }}">
<cover-view class="map_custiom_callout">
<cover-view class="portName">{{ portName }}</cover-view>
</cover-view>
</cover-view>
</cover-view>
</map>
<scroll-view scroll-y>
<block wx:for="{{ portList }}" wx:key="port">
<view class="item_port" bindtap="onPortItemClick" data-portid="{{ item.portId }}" data-index="{{ index }}">
<!-- ... -->
</block>
</scroll-view>
這塊從一開始自己就進入了一個誤區(qū)。其實很多事情都是循循漸進,太過于急功近利,反而有點得不償失了。無論身處何地,保持自身冷靜,條理分析。
6、關(guān)于那些煩人的相對路徑處理
相信大家都遇到過如下情況,比如我定義一個 urlUtils 工具類,那么在對應(yīng)使用的 js 中就需要通過如下方式引用:
- const urlUtils = require('../../utils/urlUtils.js')
看到前面的 ../ 就說煩不煩?
咨詢大佬,大佬提供了一種使用絕對路徑方案,如下:
Step 1: app.js 新增 require 方法:
require: function ($url) { return require($url) },
Step 2: 替換原有很 low 的方式。
//獲取應(yīng)用實例
const app = getApp();
const urlUtils = app.require('utils/urlUtils.js');
ummm,爽多了。哈哈哈。
對了,記得關(guān)閉「上傳時進行代碼保護」
7、如何實現(xiàn)點擊圖片彈出并播放視頻?
還是老規(guī)矩,放個效果圖,稍等,等我錄制,
相關(guān)案例查看更多
相關(guān)閱讀
- 定制小程序開發(fā)
- 網(wǎng)站建設(shè)優(yōu)化
- 網(wǎng)站搭建
- 云南網(wǎng)站優(yōu)化公司
- 云南省城鄉(xiāng)建設(shè)廳網(wǎng)站
- 開發(fā)制作小程序
- 網(wǎng)站建設(shè)招商
- 云南網(wǎng)站建設(shè)服務(wù)
- php網(wǎng)站
- 出入小程序
- 云南網(wǎng)站建設(shè)報價
- 汽車報廢軟件
- 網(wǎng)站排名
- 云南軟件開發(fā)
- 開發(fā)微信小程序
- 網(wǎng)站建設(shè)需要多少錢
- 云南網(wǎng)站建設(shè)開發(fā)
- 霸屏推廣
- 云南小程序哪家好
- 表單
- 報廢車回收管理系統(tǒng)
- 云南網(wǎng)站建設(shè)公司
- 云南軟件定制
- 網(wǎng)站小程序
- 網(wǎng)站建設(shè)專家
- 網(wǎng)站建設(shè)公司哪家好
- 云南網(wǎng)站建設(shè)公司地址
- 網(wǎng)站優(yōu)化
- 網(wǎng)站建設(shè)快速優(yōu)化
- 云南科技公司