Heim  >  Artikel  >  WeChat-Applet  >  Implementieren Sie das Wasserfall-Flow-Layout und das unbegrenzte Laden im WeChat-Miniprogramm

Implementieren Sie das Wasserfall-Flow-Layout und das unbegrenzte Laden im WeChat-Miniprogramm

迷茫
迷茫Original
2017-03-25 16:01:051522Durchsuche


bf2d792e5561c89f9b1ed6182129fc5211cbfe5a27ba823f85e0ca73695caa9b
3b7f08c33b880b29f189568e9347b46ff8e950ebc6c1ea27f76cf997b9216fe6de5f4c1163741e920c998275338d29b2

Wir können die Datenbindung in Page übergeben, um die Anforderung zu laden Bildinformationen in wxml, lassen Sie die dc0870658837139040642baa5555a380-Komponente die Bildressource laden, und wenn das Laden des Bildes abgeschlossen ist, erfolgt die weitere Verarbeitung durch die durch bindload angegebene Ereignisverarbeitungsfunktion.

Werfen wir einen Blick auf die in der Page-Datei definierte onImageLoad-Funktion. Darin können wir umfangreiche Informationen über die dc0870658837139040642baa5555a380-Komponente aus dem eingehenden Ereignisobjekt e erhalten, einschließlich der tatsächlichen Größe des durch sie geladenen Bildes. Dann berechnen wir die Größe, nachdem wir das Bild entsprechend der tatsächlichen Größe skaliert haben, die auf der Seite angezeigt werden muss. Dann können wir basierend auf der aktuellen akkumulierten Inhaltshöhe der linken und rechten Spalte entscheiden, auf welcher Seite das aktuell geladene Bild platziert werden soll.

let col1H = 0;let col2H = 0;

Page({    data: {        scrollH: 0,        imgWidth: 0,        loadingCount: 0,        images: [],        col1: [],        col2: []
   },    onLoad: function () {
       wx.getSystemInfo({            success: (res) => {                
       let ww = res.windowWidth;                
       let wh = res.windowHeight;                
       let imgWidth = ww * 0.48;                
       let scrollH = wh;                
       this.setData({                    
       scrollH: scrollH, 
       imgWidth: imgWidth
               });                //加载首组图片
               this.loadImages();
           }
       })
   },    onImageLoad: function (e) {        
   let imageId = e.currentTarget.id;        
   let oImgW = e.detail.width;         //图片原始宽度
       let oImgH = e.detail.height;        //图片原始高度
       let imgWidth = this.data.imgWidth;  //图片设置的宽度
       let scale = imgWidth / oImgW;        //比例计算
       let imgHeight = oImgH * scale;      //自适应高度

       let images = this.data.images;        let imageObj = null;        
       for (let i = 0; i < images.length; i++) {            let img = images[i];            
       if (img.id === imageId) {
               imageObj = img;                break;
           }
       }

       imageObj.height = imgHeight;        
       let loadingCount = this.data.loadingCount - 1;        
       let col1 = this.data.col1;        
       let col2 = this.data.col2;        //判断当前图片添加到左列还是右列
       if (col1H <= col2H) {
           col1H += imgHeight;
           col1.push(imageObj);
       } else {
           col2H += imgHeight;
           col2.push(imageObj);
       }        let data = {            
       loadingCount: loadingCount,            col1: col1,            col2: col2
       };        //当前这组图片已加载完毕,则清空图片临时加载区域的内容
       if (!loadingCount) {
           data.images = [];
       }        this.setData(data);
   },    loadImages: function () {        let images = [
           { pic: "../../images/1.png", height: 0 },
           { pic: "../../images/2.png", height: 0 },
           { pic: "../../images/3.png", height: 0 },
           { pic: "../../images/4.png", height: 0 },
           { pic: "../../images/5.png", height: 0 },
           { pic: "../../images/6.png", height: 0 },
           { pic: "../../images/7.png", height: 0 },
           { pic: "../../images/8.png", height: 0 },
           { pic: "../../images/9.png", height: 0 },
           { pic: "../../images/10.png", height: 0 },
           { pic: "../../images/11.png", height: 0 },
           { pic: "../../images/12.png", height: 0 },
           { pic: "../../images/13.png", height: 0 },
           { pic: "../../images/14.png", height: 0 }
       ];        let baseId = "img-" + (+new Date());       
        for (let i = 0; i < images.length; i++) {
           images[i].id = baseId + "-" + i;
       }        this.setData({            
       loadingCount: images.length,            images: images
       });
   }

})
  if (col1H <= col2H) {
            col1H += imgHeight;
            col1.push(imageObj);
        } else {
            col2H += imgHeight;
            col2.push(imageObj);
        }        let data = {            loadingCount: loadingCount,            col1: col1,            col2: col2
        };        //当前这组图片已加载完毕,则清空图片临时加载区域的内容
        if (!loadingCount) {
            data.images = [];
        }        this.setData(data);
    },    loadImages: function () {        let images = [
            { pic: "../../images/1.png", height: 0 },
            { pic: "../../images/2.png", height: 0 },
            { pic: "../../images/3.png", height: 0 },
            { pic: "../../images/4.png", height: 0 },
            { pic: "../../images/5.png", height: 0 },
            { pic: "../../images/6.png", height: 0 },
            { pic: "../../images/7.png", height: 0 },
            { pic: "../../images/8.png", height: 0 },
            { pic: "../../images/9.png", height: 0 },
            { pic: "../../images/10.png", height: 0 },
            { pic: "../../images/11.png", height: 0 },
            { pic: "../../images/12.png", height: 0 },
            { pic: "../../images/13.png", height: 0 },
            { pic: "../../images/14.png", height: 0 }
        ];        let baseId = "img-" + (+new Date());        
        for (let i = 0; i < images.length; i++) {
            images[i].id = baseId + "-" + i;
        }        this.setData({            loadingCount: images.length,            images: images
        });
    }
})


Hier ist der WXML-Code zum Anzeigen des Bildes in zwei Spalten, den wir im < sehen können ;scroll Auf der -view>-Komponente richten wir die Ereignisüberwachungsfunktion mithilfe von bindscrolltolower ein. Beim Scrollen nach unten wird LoadImages ausgelöst, um den nächsten Satz von Bilddaten zu laden, wodurch ein unendliches Laden entsteht:

<scroll-view scroll-y="true" style="height:{{scrollH}}px" bindscrolltolower="loadImages">
  <view style="width:100%">
    <view class="img_item">
      <view wx:for="{{col1}}" wx:key="id">
        <image src="{{item.pic}}" style="width:100%;height:{{item.height}}px"></image>
      </view>
    </view>
    <view class="img_item">
      <view wx:for="{{col2}}" wx:key="id">
        <image src="{{item.pic}}" style="width:100%;height:{{item.height}}px"></image>
      </view>
    </view>
  </view>
</scroll-view>


Das obige ist der detaillierte Inhalt vonImplementieren Sie das Wasserfall-Flow-Layout und das unbegrenzte Laden im WeChat-Miniprogramm. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:Auswahl-Scroll-AuswahlNächster Artikel:Auswahl-Scroll-Auswahl