A WeChat mini program version of Zhihu example sharing
This article mainly shares with you a WeChat mini program version of Zhihu from scratch. I hope it can help you develop a WeChat version of Zhihu and get more ideas from it.
Display effect (interface style design and interaction are from iOS 4.8.0 version Zhihu App):
Please move to GitHub for dynamic effects Check.
1. Preparation before starting
Apply for an account: According to the Mini Program registration document, fill in the information and submit the corresponding materials, you can have your own Mini Program account.
Development Tools: WeChat Developer Tools
Data source:
Easy Mock: A data simulation artifact, you can write the return data yourself according to the format you need, And all data is randomly generated.
Mock.js: Easy Mock introduces Mock.js, but only part of the syntax is provided in the document. If you want to write your own mock data more concisely, you can view more specific syntax in Mock.js.
2. Initialize a Mini Program
Create an empty folder
Open the WeChat developer tools and follow the steps in the "Your First Mini Program" document. You can create your own mini program.
Directory structure
weChatApp |___client | |___assets // 存储图片 | |___pages // 页面 | | |___index // 首页 | | |___index.wxml // 页面结构文件 | | |___index.wxss // 样式表文件 | | |___index.js // js文件 | |___utils // 全局公共函数 | |___app.js // 系统的方法处理文件 | |___app.json // 系统全局配置文件 | |___app.wxss // 全局的样式表 | |___config.json // 域名等配置文件 |___project.config.json |___README 小程序配置文件app.json内容 { // 页面路由 "pages": [ "pages/index/index", // 首页 "pages/findMore/findMore", // 想法页(开始起名为发现页面,后来没改/(ㄒoㄒ)/~~) "pages/userCenter/userCenter", // 更多(同上,原来起名为个人中心o(╯□╰)o) "pages/market/market", // 市场 "pages/searchResult/searchResult",// 搜索结果页 "pages/message/message", // 消息列表页 "pages/titleDetail/titleDetail", // 点击标题进入的问题详情页 "pages/contentDetail/contentDetail"// 点击内容进入的回答详情页 ], // 窗口 "window": { "backgroundColor": "#FFF", // 窗口的背景色 "backgroundTextStyle": "dark", // 下拉背景字体、loading 图的样式,仅支持 dark/light "navigationBarBackgroundColor": "#FFF",// 顶部tab背景颜色 "navigationBarTitleText": "知小乎", //顶部显示标题 "navigationBarTextStyle": "black", // 导航栏标题颜色,仅支持 black/white "enablePullDownRefresh": true // 是否开启下拉刷新 }, // tab导航条 "tabBar": { "backgroundColor": "#fff", // 背景颜色 "color": "#999", // 默认文字颜色 "selectedColor": "#1E8AE8", // 选中时文字颜色 "borderStyle": "white", // tabbar上边框的颜色, 仅支持 black/white /** * tab列表,最少2个,最多5个 * selectedIconPath: 选中时图片 * iconPath: 默认图片 * pagePath: 对应页面路由 * text: 对应文案 **/ "list": [{ "selectedIconPath": "assets/home-light.png", "iconPath": "assets/home.png", "pagePath": "pages/index/index", "text": "首页" }, { "selectedIconPath": "assets/find-light.png", "iconPath": "assets/find.png", "pagePath": "pages/findMore/findMore", "text": "想法" }, { "selectedIconPath": "assets/market-light.png", "iconPath": "assets/market.png", "pagePath": "pages/market/market", "text": "市场" }, { "selectedIconPath": "assets/msg-light.png", "iconPath": "assets/msg.png", "pagePath": "pages/message/message", "text": "消息" }, { "selectedIconPath": "assets/more-light.png", "iconPath": "assets/more.png", "pagePath": "pages/userCenter/userCenter", "text": "更多" }] } }
Configure the interface domain name: Because Easy Mock is used to simulate the interface data, you can configure the request legal domain name to https in the mini program management background-development settings-server domain name ://www.easy-mock.com.
3. Problems encountered during development and solutions
1. Mini program renders HTML fragments
After reading the web version of Zhihu, the answer data returned by the interface is A snippet of HTML code, so you can insert an image anywhere in your answer.
Yes, that’s right, it’s the purple one below (╯°□°)╯︵┻━┻
After repeated attempts, I found that the native writing method does not support rendering a piece of HTML code, so I gave up on returning HTML. Code snippets, so there are no pictures in my answer list (ಥ_ಥ)
But I found a custom component during the research: wxParse-WeChat applet rich text parsing component, I haven’t tried it yet. Prepare to try it next time when optimizing the project.
2. Tab switching at the top of the homepage
Implementation ideas
Each clickable tab is bound to the data-index. The index value of the clicked tab is obtained in the outermost bindtap binding method, and then the corresponding tab-content is displayed according to the index value
<view class="tab-wrapper" bindtap="setActive"> <view class="tab-item {{isActive == 0 ? 'tab-item-active' : ''}}" data-index="0">关注</view> <view class="tab-item {{isActive == 1 ? 'tab-item-active' : ''}}" data-index="1">推荐</view> <view class="tab-item {{isActive == 2 ? 'tab-item-active' : ''}}" data-index="2">热榜</view> <view class="tab-item-line" animation="{{animationData}}"></view> </view> <view class="tab-content {{isActive == 0 ? 'show' : 'hide'}}"> // ... </view> <view class="tab-content {{isActive == 1 ? 'show' : 'hide'}}"> // ... </view> <view class="tab-content {{isActive == 2 ? 'show' : 'hide'}}"> // ... </view>
3, pull-up loading and pull-down refresh
Implementation Ideas
Pull-up loading: emmmmmmm...The pull-up loading I mean is more loading after hitting bottom, I am afraid it is different from what everyone understands o(╯□╰)o.
Native method: onReachBottom, after obtaining the new data, concat to the original data list.
Pull down to refresh:
Native method: onPullDownRefresh, convert the original data. After the list is concated to the new data obtained.
It should be noted that every time you operate on the array, you must use setData to reassign the original array, otherwise the data will not be updated (⊙ o ⊙ )!
4. Storage of search history
Implementation ideas
wx.setStorage, wx.getStorage and wx.removeStorage
Storage search history:
Use wx.setStorage, when triggering the search, check whether the search history list contains this field, if so, ignore it, if not, push the field into the array
Display the search history:
Use wx.getStorage to obtain the search history list when displaying the search mask, and display it in a loop. When the length of the search history list is greater than 1, a button to clear the search history is displayed.
Delete. Search history:
Single deletion: Each search history is bound to a deletion event, obtains the index of the changed keyword, deletes the keyword corresponding to the index from the search history list of the channel, and resets it through wx.setStorage Storage.
Delete all: use wx.removeStorage to directly remove the keywords corresponding to the search history
5, swiper carousel component
Carousel on the idea page. Among the components, xxxx people in the original Zhihu App were discussing a vertical text carousel embedded in the carousel module, but the swiper carousel components in the mini program did not support nesting of each other, so this part was not implemented. I had to change my style to write /(ㄒoㄒ)/~~.
6. Scroll to ceiling
When the title bar in the page scrolls to the top, it will be displayed at the top.
Implementation effect
Implementation plan
The entire page is wrapped with
When setting
Copy the same title bar and add the ceiling-style class fixed.
Use wx:if to determine whether the scrolling distance of the current page reaches the required distance. If the required distance is reached, the ceiling-mounted title bar will be rendered.
<view class="find-hot-header fixed" wx:if="{{scrollTop >= 430}}"> <view class="find-hot-title">最近热门</view> </view> <view class="find-hot-header"> <view class="find-hot-title">最近热门</view> </view>
7. Expand and collapse the full text
Display effect
.text-overflow{ height: 85rpx; display: -webkit-box; word-break: break-all; text-overflow: ellipsis; overflow: hidden; -webkit-box-orient: vertical; -webkit-line-clamp:2; }
点击展开全文和收起全文时对showIndex[index]的值取反,对应添加或移除该class。
<view class="find-hot-content {{!showIndex[index] ? 'text-overflow' : ''}}"> {{item.content}} </view> <view wx:if="{{!showIndex[index]}}" class="find-show-all" data-index="{{index}}" bindtap="toggleShow">展开全文</view> <view wx:if="{{showIndex[index]}}" class="find-show-all" data-index="{{index}}" bindtap="toggleShow">收起全文</view>
8、更改switch样式
switch类名如下,一定要加上父类,不然全局的都会被改掉_(:з」∠)_。
父类 .wx-switch-input{ display: inline-block; position: absolute; top: 20rpx; right: 20rpx; width: 84rpx; height: 44rpx; } 父类 .wx-switch-input::before{ width: 80rpx; height: 40rpx; } 父类 .wx-switch-input::after{ width: 40rpx; height: 40rpx; }
四、总结
通过这次小程序的开发,学到了很多东西,虽然遇到了很多问题,但解决问题的过程让我收获的更多,动手实践才是学习的最好方式。
相关推荐:
The above is the detailed content of A WeChat mini program version of Zhihu example sharing. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Quark Netdisk and Baidu Netdisk are very convenient storage tools. Many users are asking whether these two softwares are interoperable? How to share Quark Netdisk to Baidu Netdisk? Let this site introduce to users in detail how to save Quark network disk files to Baidu network disk. How to save files from Quark Network Disk to Baidu Network Disk Method 1. If you want to know how to transfer files from Quark Network Disk to Baidu Network Disk, first download the files that need to be saved on Quark Network Disk, and then open the Baidu Network Disk client. , select the folder where the compressed file is to be saved, and double-click to open the folder. 2. After opening the folder, click "Upload" in the upper left corner of the window. 3. Find the compressed file that needs to be uploaded on your computer and click to select it.

1. First, we enter NetEase Cloud Music, and then click on the software homepage interface to enter the song playback interface. 2. Then in the song playback interface, find the sharing function button in the upper right corner, as shown in the red box in the figure below, click to select the sharing channel; in the sharing channel, click the "Share to" option at the bottom, and then select the first "WeChat Moments" allows you to share content to WeChat Moments.

Recently, Baidu Netdisk Android client has ushered in a new version 8.0.0. This version not only brings many changes, but also adds many practical functions. Among them, the most eye-catching is the enhancement of the folder sharing function. Now, users can easily invite friends to join and share important files in work and life, achieving more convenient collaboration and sharing. So how do you share the files you need to share with your friends? Below, the editor of this site will give you a detailed introduction. I hope it can help you! 1) Open Baidu Cloud APP, first click to select the relevant folder on the homepage, and then click the [...] icon in the upper right corner of the interface; (as shown below) 2) Then click [+] in the "Shared Members" column 】, and finally check all

Implementing card flipping effects in WeChat mini programs In WeChat mini programs, implementing card flipping effects is a common animation effect that can improve user experience and the attractiveness of interface interactions. The following will introduce in detail how to implement the special effect of card flipping in the WeChat applet and provide relevant code examples. First, you need to define two card elements in the page layout file of the mini program, one for displaying the front content and one for displaying the back content. The specific sample code is as follows: <!--index.wxml-->&l

According to news from this site on October 31, on May 27 this year, Ant Group announced the launch of the "Chinese Character Picking Project", and recently ushered in new progress: Alipay launched the "Chinese Character Picking-Uncommon Characters" mini program to collect collections from the society Rare characters supplement the rare character library and provide different input experiences for rare characters to help improve the rare character input method in Alipay. Currently, users can enter the "Uncommon Characters" applet by searching for keywords such as "Chinese character pick-up" and "rare characters". In the mini program, users can submit pictures of rare characters that have not been recognized and entered by the system. After confirmation, Alipay engineers will make additional entries into the font library. This website noticed that users can also experience the latest word-splitting input method in the mini program. This input method is designed for rare words with unclear pronunciation. User dismantling

Mango TV has various types of movies, TV series, variety shows and other resources, and users can freely choose to watch them. Mango TV members can not only watch all VIP dramas, but also set the highest definition picture quality to help users watch dramas happily. Below, the editor will bring you some free Mango TV membership accounts for users to use, hurry up and take a look Take a look. Mango TV latest member account free sharing 2023: Note: These are the latest member accounts collected, you can log in directly and use them, do not change the password at will. Account number: 13842025699 Password: qds373 Account number: 15804882888 Password: evr6982 Account number: 13330925667 Password: jgqae Account number: 1703

How uniapp can achieve rapid conversion between mini programs and H5 requires specific code examples. In recent years, with the development of the mobile Internet and the popularity of smartphones, mini programs and H5 have become indispensable application forms. As a cross-platform development framework, uniapp can quickly realize the conversion between small programs and H5 based on a set of codes, greatly improving development efficiency. This article will introduce how uniapp can achieve rapid conversion between mini programs and H5, and give specific code examples. 1. Introduction to uniapp unia

Title: To solve the problem that Discuz WeChat shares cannot be displayed, specific code examples are needed. With the development of the mobile Internet, WeChat has become an indispensable part of people's daily lives. In website development, in order to improve user experience and expand website exposure, many websites will integrate WeChat sharing functions, allowing users to easily share website content to Moments or WeChat groups. However, sometimes when using open source forum systems such as Discuz, you will encounter the problem that WeChat shares cannot be displayed, which brings certain difficulties to the user experience.
