It really blew up: the "Picture lazy loading" ultimate solution of the little program that makes people headache

Source: Internet
Author: User

Reprint: Please specify the original link and the author's name ' Little Small '
Will the applet really replace everything? QQ Group: 139128168← Click Add Group

In small programs, lazy loading effect is a headache, because the small program is completely unable to manipulate the DOM, so the location of the operation in the small program, become extremely difficult ~ ~

See Special effects First:

We will split it into the following steps to explain ~ ~

1) How to get the position height of the picture

First look at a picture:

By knowing, the image position height can actually be calculated by Img.height + margin value.

JS Code:

Math.floor(i/2)*(img.height + margin-bottom);

Why Math.floor (I/2), because the same row two picture height, such as i=0 and I=1, through the Math.floor to the value of 0, so you can guarantee that the same row of two picture position height is the same value.

2) Replace the default picture

First look at the film:

Wxml Code:

<image src="{{arr[index] ? productArr[index].Image : ‘default.jpg‘}}"></image>
    • The idea is obvious, the first Arr[index] is false, so the default is the defaults picture
    • But as you move down, some arr[index] values become true, so replace the default picture

JS Code:

for (var0this.data.productArr.length; i++) {  if (arrHight[i] < scrollTop) {      iffalse) {          true;      }  }}

The idea is quite clear, needless to say ~ ~

3) Lazy loading in fade effects

First look at the effect:

WXSS Code:

. Product_image{  opacity: 0;   width: %;   height: %;  transition: opacity 1s linear 2s; }. Loaded{  opacity: 1;} 

is actually a transition animation of opacity, so easy~~



SOURCE download

"Duang, not to forget the attention ~ ~"

It really blew up: the "Picture lazy loading" ultimate solution of the little program that makes people headache

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.