百度小程序-圖片畫廊-使用previewImage方法實現


.swan

 1         <!-- 輪播圖 S-->
 2             <view class="swiper-box">
 3                 <swiper style='height:{{swiperH}}' bindchange="swiperChange" autoplay="true" interval="3000"
 4                     duration="500" circular="true">
 5                     <block s-for="banner" s-for-index="index" s-for-item="item">
 6                         <swiper-item >
 7                             <image bindtap="previewOriginImage" data-src="{{item.cover_id}}" lazy-load="true" src="{{item.cover_id}}" class="slide-image" mode="widthFix" bindload='imgHeight' />
 8                         </swiper-item>
 9                     </block>
10                 </swiper>
11                 <view class="dots">
12                     <block s-for="banner" s-for-index="index" s-for-item="imgUrl">
13                         <view class="dot {{index == swiperCurrent  ? 'active' : ''}}"></view>
14                     </block>
15                 </view>
16             </view>
17         <!-- 輪播圖 E-->

.css

1 .slide-image{width:100%;display:block}
2 .swiper-box{position:relative;width:100%;box-sizing:border-box}
3 .dots{position:absolute;left:0;right:0;bottom:0;display:-webkit-box;display:-webkit-flex;display:-moz-box;display:-ms-flexbox;display:flex;-webkit-box-pack:center;-webkit-justify-content:center;-moz-box-pack:center;-ms-flex-pack:center;justify-content:center;margin-bottom:40rpx}
4 .dots .dot{margin:0 8rpx;width:14rpx;height:14rpx;background:rgba(255,255,255,0.8);border-radius:50%;border:solid 1px #0b82f3;-webkit-transition:all 0.6s;transition:all 0.6s;box-sizing:border-box}
5 .dots .dot.active{background-color:#0b82f3}

.js

 1 const app = getApp();
 2 Page({
 3     data: {
 4         banner: [],//輪播圖
 5         swiperCurrent: "",//輪播圖圓點
 6         swiperH: "",  //這是swiper框要動態設置的高度屬性
 7         arrimgList:[],//圖片畫廊數組
 8     },
 9     onLoad: function () {
10         // 監聽頁面加載的生命周期函數
11         this.getBanner();
12     },
13     onReady: function() {
14         // 監聽頁面初次渲染完成的生命周期函數
15     },
16     onShow: function() {
17         // 監聽頁面顯示的生命周期函數
18         app.setInfo();
19     },
20     onHide: function() {
21         // 監聽頁面隱藏的生命周期函數
22     },
23     onUnload: function() {
24         // 監聽頁面卸載的生命周期函數
25     },
26     onPullDownRefresh: function() {
27         // 監聽用戶下拉動作
28     },
29     onReachBottom: function() {
30         // 頁面上拉觸底事件的處理函數
31     },
32     onShareAppMessage: function () {
33         // 用戶點擊右上角轉發
34     },
35     swiperChange: function (e) {
36         this.setData({
37             swiperCurrent: e.detail.current   //獲取當前輪播圖片的下標
38         })
39     },
40     imgHeight: function (e) {
41         var winWid = swan.getSystemInfoSync().screenWidth;
42         var imgh = e.detail.height;//圖片高度
43         var imgw = e.detail.width;//圖片寬度
44         var swiperH = winWid * imgh / imgw + "px";
45         //等比設置swiper的高度。 即 屏幕寬度 / swiper高度 = 圖片寬度 / 圖片高度  ==》swiper高度 = 屏幕寬度 * 圖片高度 / 圖片寬度
46         this.setData({
47             swiperH: swiperH//設置高度
48         });
49         // console.log(e.currentTarget.dataset.src);
50         //推入數組 難點可能在這里,數據推入到一個數組中
51         this.data.arrimgList.push(e.currentTarget.dataset.src);
52         // console.log(this.data.arrimgList);
53     },
54     getBanner: function () {//獲取banner輪播圖
55         var that = this;
56         swan.request({
57             url: app.globalData.baseUrl + 'list/banner',
58             method: 'GET',
59             header: {
60                 genToken: app.globalData.genToken,
61             },
62             success: function (res) {
63                 // console.log(res);
64                 that.setData({
65                     banner: res.data.lines
66                 })
67                 // console.log(that.data.banner)
68             }
69         });
70     },
71     previewOriginImage(e) {
72         console.log(e);
73         swan.previewImage({
74             current:e.currentTarget.dataset.src,
75             urls: this.data.arrimgList,  // 需要預覽的圖片http鏈接列表 
76         });
77     }
78 });

效果圖


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM