Front end: develop an interesting imitation wechat circle of friends application

Xu Xiaoxi 2021-06-18 05:55:49
end develop interesting imitation wechat


Preface

I originally wanted to open source what I wrote CMS Applied , But because of the author's mac The computer crashed , All data cannot be recovered , Causing part of the code to be lost , But fortunately cms The package file of has been uploaded to the server , Interested friends can visit the link at the end of the article .

What to write today H5 Circle of friends It is also based on the author's development cms Built , I will follow suit WeChat friend circle , Take you to develop a release dynamic ( Including picture upload ) The circle of friends of . About the server part, the author will not elaborate in this article , It will be later in cms2.0 In detail .

You will reap

  • Use umi Quickly create one H5 Mobile applications
  • be based on react-lazy-load Realize the picture / Content lazy load
  • Use css3 Dynamically change the layout based on the number of pictures
  • utilize FP Create a circle of friends form
  • Use rc-viewer see / rotate / Zoom in and out of your circle of friends
  • be based on axios + formdata Realize file upload function
  • ZXCMS Introduce

Preview of application effect

Circle of friends list Check out the circle of friends pictures Release news

Text

Before starting the article , I would like to make a rough summary of the development H5 Mobile applications need to consider the point . For any mobile application , We all have to consider the following questions :

  • First screen loading time
  • Adaptation problem
  • Page fluency
  • Animation performance
  • Interactive friendly
  • Provide user feedback These are not just issues for front-end engineers to consider , It's also a category for product managers and interaction designers to consider . Of course, there are many practical considerations that need to be optimized according to their own needs , The solutions to the above points are as follows :
  1. Increase the loading time of the first screen May adopt Resource lazy loading +gzip+ Static resources CDN To optimize , And provide loading animation to reduce user anxiety .

  2. Adaptation problem Mobile adaptation problem can be solved through js Dynamically set the width of the viewport / ratio Or use css Media search To deal with it , There are already very mature schemes on the market

  3. Page fluency We can do it in body Set up -webkit-overflow-scrolling:touch; To improve the flow of rolling , And you can a/img Use on label -webkit-touch-callout: none To prevent long press to generate menu bar .

  4. Animation performance In order to improve animation performance , We can use the properties that need to be changed transform Or use absolute Positioning instead of ,transform Does not cause the page to redraw .

  5. Provide user feedback We can provide friendly user feedback through reasonable settings toast,modal Wait to control

The above is only a few mobile optimization , There are many other solutions for front-end page performance optimization , The author has also introduced in detail in the previous article , Let's go to the main body .

1. Use umi Create an app quickly

I will adopt umi As a front-end integration solution for the project , It provides a lot of functions , It's also very convenient to use , And for the antd and antd-mobile Auto import on demand , So I'm familiar with react A friend of mine can try , The scheme of this paper is for vue It's also applicable to players , Because in any situation , Methods and thinking patterns are cross language and cross framework .

at present umi Has been upgraded to 3.0, What this article uses is 2.0, But the difference is not big , You can use it at ease 3.0. The specific steps are as follows

// umi2.0
//  New project directory
mkdir friendcircle
//  establish umi project
cd friendcircle
yarn create umi
//  Installation dependency
yarn
yarn add antd-moblie

Such a umi The project is created .

2. be based on react-lazy-load Realize the picture / Content lazy load

After the project is created , Let's first analyze the technical points we need to use : The author has studied a lot of lazy loading methods in the design , Currently adopted react-lazy-load To achieve , The benefit is that it supports loading event notification , For example, we need to do buried point or advertising reporting and other functions are very convenient . Of course, you can also pass observer API To achieve , The specific implementation plan is in A few very interesting javascript Summary of knowledge points It is introduced in the article . Specific usage :

<LazyLoad key={item.uid} overflow height={280} onContentVisible={onContentVisible}>
    //  Components that need lazy loading
    <ComponentA />
</LazyLoad>

react-lazy-load It's very simple to use , What you don't understand can be learned on the official website .

3. Use css3 Dynamically change the layout based on the number of pictures

At present, there is a core requirement in the list page of the circle of friends that we need to pass in different numbers of pictures when users pass in , Have a different layout , Just like wechat circle of friends , The main purpose is to let users see as many pictures as possible , Improve user experience , As shown in the figure below : We use it js It's very convenient to realize , But it's not very performance friendly , And for every dynamic picture released by users, you need to use js Recalculate it again , As a aspiring programmer, it's impossible for this to happen , So we use css3 To achieve , In fact, about this way of realization, the author before css3 Advanced techniques are detailed in the article , We used Child node selector , The specific implementation is as follows :

.imgItem {
margin-right: 6px;
margin-bottom: 10px;
&:nth-last-child(1):first-child {
margin-right: 0;
width: 100%;
}
&:nth-last-child(2):first-child,
&:nth-last-child(3):first-child,
&:nth-last-child(4):first-child,
&:first-child:nth-last-child(n+2) ~ div {
width:calc(50% - 6px);
height: 200px;
overflow: hidden;
}
&:first-child:nth-last-child(n+5),
&:first-child:nth-last-child(n+5) ~ div {
width: calc(33.33333% - 6px);
height: 150px;
overflow: hidden;
}
}

In the above code, we have a picture ,2-4 A picture ,5 The above pictures are set in different sizes , In this way, we can meet our needs , One more thing to note , When users upload images of different sizes , There is a possibility of inconsistency between high and low , At this time, in order to show consistency , We can use img In style object-fit attribute , It's kind of like background-size, We can img The note serves as a container , How to fill the container with contents , Use it completely object-fit To set up , The specific properties are as follows :

  • fill The replaced content just fills the content box of the element . The entire object will fill the box completely . If the aspect ratio of the object does not match the content box , Then the object will be stretched to fit the content box
  • contain The replaced content will be scaled , To maintain the aspect ratio of an element as it fills its content box . The whole object fills the box while keeping its aspect ratio , So if the aspect ratio doesn't match the aspect ratio of the frame , The object will be added “ Black edge ”
  • cover The replaced content fills the entire content box of the element while maintaining its aspect ratio . If the aspect ratio of the object does not match the content box , The object will be clipped to fit the content box
  • scale-down The size of the content and none or contain One of them is the same , It depends on who gets the smaller object size between them
  • none The replaced content will keep its original size

So in order to keep the picture consistent , We set it up like this img Style of label :

img {
  width100%;
  height100%;
  object-fit: cover;
}

4. utilize FP Create a circle of friends form

FP Is an open-source form configuration platform , It is mainly used to customize and analyze various form models , The interface is as follows : Through this platform, you can customize various form templates and analyze form data . Here, we only need to configure a simple friend circle publishing function , as follows : Due to the author's computer data loss, resulting in part of the code loss , If you are interested, you can learn something about .

5. Use rc-viewer see / rotate / Zoom in and out of your circle of friends

Another important function for the circle of friends is to be able to view every dynamic picture , Similar to the image viewer of wechat circle of friends , Here the author uses the third-party open source library rc-viewer To achieve , The specific code is as follows :

<RcViewer options={{title: 0navbar: 0toolbar: 0}} ref={imgViewRef}>
  <div className={styles.imgBox}>
    {
      item.imgUrls.map((item, i) => {
        return <div className={styles.imgItem} key={i}>
          <img src={item} alt=""/>
        </div>
      })
    }
  </div>   
</RcViewer>

From the above code, we only need to be in RcViewer Component to write we need to view the picture structure on the line , It provides many configuration options to use , Here I am option Configuration of the title,navbar,toolbar Are all 0, It means not showing these features , Because the mobile end only needs to have the basic view , The zoom , Switch the picture function , Lightweight as much as possible . The effect is as follows : When we click on a picture in action , We can see the big picture of it , And switch through gestures .

6. be based on axios + formdata Realize file upload function

File upload , In addition to using antd Of upload Components , We can also combine http Request library and formdata To achieve , In order to support multi image upload and ensure the timing , We use async await function , The specific code is as follows :

const onSubmit = async () => {
    // ... something code
    const formData = new FormData()
    for(let i=0; i< files.length; i++) {
      formData.delete('file')
      formData.append('file', files[i].file)
      try{
        const res = await req({
          method'post',
          url'/files/upload/tx',
          data: formData,
          headers: {
              'Content-Type''multipart/form-data'
          }
        });
        // ... something co
      }catch(err) {
        Toast.fail(' Upload failed '2);
      }
    }

among req It is based on axios Packaged http Request Library , Support simple requests / The response to intercept , Interested friends can refer to the source code of the author .

7. ZXCMS Introduce

ZXCMS It is a commercial version developed by the author CMS, You can quickly build your own community , Blog, etc , And integrated form customization platform , Configuration center , Data distribution center and other functions , Later it will expand H5 Visual building platform and PC End station building platform , Become a more powerful open source system . The design framework is as follows :

The specific interface is as follows :

A community platform configured by the author : Article details page : Community support comments , Search articles and other functions . The following describes the background management system : A brief introduction , Later, the author will give a special article to introduce the specific implementation and source code design .

8. Source code address

Due to the author's computer data loss , Only part of the source code can be found , So you can refer to the following address :

Open source is not easy , Welcome to support ~

Last

If you want to learn more H5 game , webpack,node,gulp,css3,javascript,nodeJS,canvas Data visualization Wait for front-end knowledge and actual combat , Welcome to the public 《 Interesting front end 》 Join our technology group to learn and discuss , Explore the frontline... Together .

More recommendations

版权声明
本文为[Xu Xiaoxi]所创,转载请带上原文链接,感谢
https://qdmana.com/2021/05/20210531210707048g.html

  1. HTML + CSS + JavaScript to achieve cool Fireworks (cloud like particle text 3D opening)
  2. HTML + CSS + JavaScript realizes 520 advertising love tree (including music), which is necessary for programmers to express themselves
  3. Solve the problem of Web front-end deployment server (it can be deployed online without a server)
  4. HTML + CSS + JS make wedding countdown web page template (520 / Tanabata Valentine's Day / programmer advertisement)
  5. What else can driverless minibus do besides "Park connection"?
  6. Cloud native leads the era of all cloud development
  7. NRM mirror source management tool
  8. Bring it to you, flex Jiugong
  9. Lolstyle UI component development practice (II) -- button group component
  10. Deconstruction assignment in ES6
  11. Luo 2 peerless Tang clan was officially launched. The official gave a key point, and the broadcast time was implied
  12. 20初识前端HTML(1)
  13. 当新零售遇上 Serverless
  14. 20 initial knowledge of front-end HTML (1)
  15. When new retail meets serverless
  16. [golang] - go into go language lesson 5 type conversion
  17. [golang] - go into go language lesson 6 conditional expression
  18. HTML5(八)——SVG 之 path 详解
  19. HTML5 (8) -- detailed explanation of SVG path
  20. 需要开通VIP以后页面内容才能复制怎么办?控制台禁用javascript即可
  21. Web前端|CSS入门教程(超详细的CSS使用讲解,适合前端初学者)
  22. 实践积累 —— 用Vue3简单写一个单行横向滚动组件
  23. Serverless 全能选手,再下一城
  24. What if you need to open a VIP to copy the page content? Just disable JavaScript on the console
  25. Web front end | CSS introductory tutorial (super detailed CSS explanation, suitable for front-end beginners)
  26. Practice accumulation - write a single line horizontal scroll component simply with vue3
  27. Dili Reba is thin again. She looks elegant and high in a strapless hollow skirt, and her "palm waist" is beautiful to a new height
  28. Serverless all-round player, next city
  29. The difference between MySQL semi synchronous replication and lossless semi synchronous replication
  30. Vue表单设计器的终极解决方案
  31. The ultimate solution for Vue form designer
  32. Nginx从理论到实践超详细笔记
  33. Yu Shuxin's red backless swimsuit is split to the waist and tail, with a concave convex figure and excessive color matching, and his face is white to dazzling
  34. Nginx ultra detailed notes from theory to practice
  35. 【动画消消乐|CSS】086.炫酷水波浪Loading过渡动画
  36. typecho全站启用https
  37. CCTV has another popular employee. The off-site interpretation is very professional, and the appearance ability is no less than that of Wang Bingbing
  38. [animation Xiaole | CSS] 086. Cool water wave loading transition animation
  39. Enable HTTPS in Typecho
  40. 50天用JavaScript完成50个web项目,我学到了什么?
  41. 根据JavaScript中原生的XMLHttpRequest实现jQuery的Ajax
  42. What have I learned from completing 50 web projects with JavaScript in 50 days?
  43. "My neighbor doesn't grow up" has hit the whole network. There are countless horse music circles, and actor Zhou Xiaochuan has successfully made a circle
  44. 根据JavaScript中原生的XMLHttpRequest实现jQuery的Ajax
  45. Implement the Ajax of jQuery according to the native XMLHttpRequest in JavaScript
  46. Implement the Ajax of jQuery according to the native XMLHttpRequest in JavaScript
  47. 30 + women still wear less T-shirts and jeans. If they wear them like stars, they will lose weight
  48. 数栈技术分享前端篇:TS,看你哪里逃~
  49. Several stack technology sharing front end: TS, see where you escape~
  50. 舍弃Kong和Nginx,Apache APISIX 在趣链科技 BaaS 平台的落地实践
  51. Abandon the landing practice of Kong and nginx, Apache apisik on the baas platform of fun chain technology
  52. 浪迹天涯king教你用elementui做复杂的表格,去处理报表数据(合并表头,合并表体行和列)
  53. 前端HTML两万字图文大总结,快来看看你会多少!【️熬夜整理&建议收藏️】
  54. Wandering around the world king teaches you to use elementui to make complex tables and process report data (merge header, merge table body rows and columns)
  55. 路由刷新数据丢失 - vuex数据读取的问题
  56. Front end HTML 20000 word graphic summary, come and see how much you can【 Stay up late to sort out & suggestions]
  57. Route refresh data loss - vuex data reading problem
  58. Systemctl系统启动Nginx服务脚本
  59. Systemctl system startup nginx service script
  60. sleepless