Home >Web Front-end >Front-end Q&A >JavaScript implements fan interface
With the popularity of social media, having a group of fans has become a goal for many people. For some social media platforms, having a fan interface allows users to better manage their fans. In this article, we will introduce how to use JavaScript to implement a simple fan interface.
In order to achieve a good-looking and easy-to-use fan interface, we need to plan the layout and functions of the interface first. The fan interface generally includes the following functions:
Before implementing the above function, we need to obtain the user's fan list first. For different social media platforms, the way to obtain a follower list may be different. Generally speaking, this can be achieved using interfaces or crawlers.
Code example for obtaining fan list via interface:
function getFansList(userId, pageNum) { // 调用接口获取粉丝列表 const url = `https://example.com/api/getFansList?userId=${userId}&pageNum=${pageNum}`; return fetch(url).then(response => response.json()); }
The getFansList function in the code can obtain the fan list of the corresponding user based on the passed in userId and pageNum parameters. The data returned by the interface is generally an array, and each element contains information such as the fan's avatar, nickname, and follow status.
After obtaining the fan list, we need to render the data to the interface. In JavaScript, you can use DOM operations to render the interface.
Code example:
function renderFansList(list) { // 获取ul元素 const ul = document.querySelector('#fansList'); // 清空ul元素 ul.innerHTML = ''; // 遍历粉丝列表,创建li元素并添加到ul元素中 for (let i = 0; i < list.length; i++) { const fan = list[i]; const li = document.createElement('li'); li.innerHTML = ` <img src="${fan.avatar}"> <div>${fan.nickname}</div> <button class="${fan.isFollowing ? 'following' : 'follow'}">${fan.isFollowing ? '已关注' : '关注'}</button> `; ul.appendChild(li); } }
The renderFansList function in the code uses the ES6 template string syntax to render the fan's avatar, nickname, follow status and other information into the li element, and add it to in the ul element.
In order to facilitate users to find and filter fans, we can add search and filter functions. The search function can search based on fans' nicknames, and the filtering function can filter based on fans' attention status.
Code example:
const input = document.querySelector('#searchInput'); const btnFilter = document.querySelectorAll('.filter-btn'); // 添加搜索功能 function searchFans(keyword, list) { const result = list.filter(fan => fan.nickname.includes(keyword)); renderFansList(result); } input.addEventListener('input', function(e) { const keyword = e.target.value.trim(); if (keyword) { searchFans(keyword, fansList); } else { renderFansList(fansList); } }); // 添加过滤功能 function filterFans(status, list) { const result = list.filter(fan => { if (status === 'all') { return true; } else if (status === 'following') { return fan.isFollowing; } else { return !fan.isFollowing; } }); renderFansList(result); } for (let i = 0; i < btnFilter.length; i++) { btnFilter[i].addEventListener('click', function(e) { const status = e.target.dataset.status; filterFans(status, fansList); }); }
The searchFans and filterFans functions in the code are used to search and filter fans respectively. When adding the search function, we listen to the input event to obtain keywords and call the searchFans function to filter the fan list. When adding the filtering function, we listen to the click event of filter-btn and filter based on different data-status attribute values.
Finally, we need to add batch operation function to facilitate users to unfollow multiple fans at one time. In order to achieve batch operations, we can add a checkbox to each li element and add an "Unfollow" button at the bottom of the interface.
Code example:
const btnBatch = document.querySelector('#batchBtn'); // 添加复选框和批量操作功能 function addCheckbox(list) { const ul = document.querySelector('#fansList'); for (let i = 0; i < list.length; i++) { const li = ul.children[i]; const input = document.createElement('input'); input.type = 'checkbox'; input.value = i; li.insertBefore(input, li.firstChild); } // 监听“取消关注”按钮的点击事件 btnBatch.addEventListener('click', function() { const inputs = document.querySelectorAll('input[type="checkbox"]'); for (let i = 0; i < inputs.length; i++) { const input = inputs[i]; if (input.checked) { const fan = list[input.value]; cancelFollowing(fan.id).then(() => { fan.isFollowing = false; input.checked = false; li = input.parentNode; li.querySelector('button').className = 'follow'; li.querySelector('button').textContent = '关注'; }); } } }); }
The addCheckbox function in the code adds a checkbox to each li element and adds an "Unfollow" button at the bottom. After the addition is completed, we listen to the click event of btnBatch, traverse all check boxes, and if the check box is selected, call the cancelFollowing function to cancel the corresponding fan's attention, and update the follow status and button text of the li element.
At this point, we have completed a simple fan interface. Although it is complicated to implement the fan interface using JavaScript, it is not difficult to implement it. You only need to be proficient in DOM operations and asynchronous programming.
The above is the detailed content of JavaScript implements fan interface. For more information, please follow other related articles on the PHP Chinese website!