Home Web Front-end Vue.js How to use Vue to implement a page design similar to Zhihu Daily?

How to use Vue to implement a page design similar to Zhihu Daily?

Jun 25, 2023 pm 12:08 PM
vue page design Zhihu Daily

Vue.js is a front-end framework based on the MVVM pattern. It decouples data and UI pages through data binding and componentization, making web development more efficient and simpler. Zhihu Daily is a news client with beautiful UI design, powerful interactivity and content diversity. In this article, we will use Vue technology to implement a page design that mimics Zhihu Daily.

  1. Build the environment

Before we start, we need to install Node.js and Vue-cli. After installing Node.js, use the command line tool to run the following command in the terminal to install Vue-cli:

$ npm install -g vue-cli

After the installation is complete, use Vue-cli to create a project based on the webpack template:

$ vue init webpack vue-zhihudaily

At this point, we can see that after create a new project asks you several questions (project name, description, author, whether eslint code specifications are required, etc.), a folder named vue-zhihudaily will be created in the current directory as a project Root directory.

  1. Page layout

In Zhihu Daily, it is mainly divided into three pages: homepage, article list page and article details page. Here we take the homepage as an example . In the src folder, create a views folder to store the interface files. Create the Home.vue file with the following code:

<template>
  <div class="home">
    <div class="banner"></div>
    <div class="daily-list"></div>
  </div>
</template>

<style scoped>
.home {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.banner {
  width: 100%;
  height: 200px;
  background: linear-gradient(to bottom, #ffffff, #f5f5f5);
}
.daily-list {
  width: 100%;
  height: 100%;
}
</style>

Here, we use flex layout to vertically center the page. Among them, banner is used to display carousel images, and daily-list is used to display article lists.

  1. Using components

In order to facilitate reuse and maintenance, we use Vue componentization to build the interface. In the src folder, create a components folder to store component files. Within it, create a file called DailyItem.vue:

<template>
  <div class="daily-item">
    <div class="thumbnail">
      <img :src="item.images[0]" alt="">
    </div>
    <div class="info">
      <div class="title">{{item.title}}</div>
      <div class="date">{{item.date}}</div>
    </div>
  </div>
</template>

<script>
export default {
  props: ['item']
}
</script>

<style scoped>
.daily-item {
  width: 100%;
  height: 80px;
  display: flex;
  align-items: center;
  margin-bottom: 5px;
  padding: 0 10px;
  box-sizing: border-box;
  background: #ffffff;
}
.daily-item:hover {
  cursor: pointer;
}
.thumbnail {
  width: 80px;
  height: 60px;
  margin-right: 10px;
  overflow: hidden;
}
.thumbnail img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.info {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
}
.title {
  font-size: 16px;
  color: #444444;
  margin-bottom: 5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.date {
  font-size: 12px;
  color: #999999;
}
</style>

DailyItem.vue is used to display information from the article list, including article thumbnails, titles, and dates. Here, we use the props attribute to pass the article information into the component. Use the DailyItem.vue component in Home.vue and replace daily-list with:

<div class="daily-list">
  <daily-item v-for="(item, index) in dailyList" :key="index" :item="item"></daily-item>
</div>

When there are multiple daily reports, this component will automatically render a DailyItem.vue for each daily report. In the parent component home, pass dailyList to the child component DailyItem.vue through props.

  1. Carousel chart implementation

The carousel chart imitating Zhihu Daily is an important part of this page. In the src folder, create a component named Banner.vue:

<template>
  <div class="banner">
    <swiper :options="swiperOption" ref="mySwiper">
      <swiper-slide v-for="(item, index) in bannerList" :key="index">
        <img :src="item.image" alt="">
        <div class="text">{{item.title}}</div>
      </swiper-slide>
      <div class="swiper-pagination" slot="pagination"></div>
    </swiper>
  </div>
</template>

<script>
import { Swiper, SwiperSlide, Pagination } from 'swiper/dist/js/swiper.esm.js'
import 'swiper/dist/css/swiper.css'

export default {
  data () {
    return {
      swiperOption: {
        pagination: {
          el: '.swiper-pagination'
        },
        loop: true,
        autoplay: {
          delay: 3000
        }
      }
    }
  },
  props: ['bannerList'],
  mounted () {
    Swiper.use([Pagination])
    this.$refs.mySwiper.swiper.slideTo(0)
  },
  components: {
    Swiper,
    SwiperSlide,
    Pagination
  }
}
</script>

<style scoped>
.banner {
  width: 100%;
  height: 200px;
  background: linear-gradient(to bottom, #ffffff, #f5f5f5);
}
.swiper-pagination-bullet-active {
  background-color: #ffffff;
}
.text {
  position: absolute;
  bottom: 10px;
  left: 10px;
  font-size: 16px;
  color: #ffffff;
  text-shadow: 1px 1px 0px rgba(0, 0, 0, 0.3);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
</style>

In Banner.vue, we use the Swiper third-party library to build the carousel chart. Call swiper.slideTo(0) in the mounted hook function to realize that the initial page is the first carousel image.

Use the Banner.vue component in Home.vue:

<div class="banner">
  <banner :bannerList="bannerList"></banner>
</div>

Here, use props to pass the bannerList into the Banner.vue component.

  1. Data acquisition

In Zhihu Daily, the homepage needs to display an article list and carousel image. We use the axios library to initiate a GET request to the Zhihu Daily API to obtain the data of the carousel chart and article list. Under the src folder, create a folder named api, and create a zhihudaily.js file in it:

import axios from 'axios'

// 轮播图 API
const bannerApi = 'https://news-at.zhihu.com/api/4/news/latest'

// 文章列表 API
const articleListApi = 'https://news-at.zhihu.com/api/4/news/before/'

export default {
  // 获取轮播图
  async getBanner () {
    const { data } = await axios.get(bannerApi)
    return data.top_stories
  },

  // 获取文章列表
  async getArticleList (date) {
    const { data } = await axios.get(articleListApi + date)
    return data.stories
  }
}

Call the method in the api in Home.vue and pass in the obtained data Among the corresponding props:

<script>
import api from '../api/zhihudaily'
import DailyItem from '../components/DailyItem.vue'
import Banner from '../components/Banner.vue'

export default {
  data () {
    return {
      bannerList: [],
      dailyList: []
    }
  },
  components: {
    DailyItem,
    Banner
  },
  async mounted () {
    this.bannerList = await api.getBanner()
    this.dailyList = await api.getArticleList('')
  }
}
</script>

Through the async/await syntax, we can obtain the required data asynchronously, making the page more efficient.

  1. Conclusion

In this article, we use Vue technology to implement a page design that imitates Zhihu Daily, involving components, carousels, and data acquisition. Waiting for knowledge points. Component-based development allows developers to better maintain and expand code, and uses third-party libraries (such as Swiper, axios) to quickly implement functions, making development more efficient.

Only by continuously expanding your knowledge base, broadening your horizons, and constantly exploring can you go further on the road to web development.

The above is the detailed content of How to use Vue to implement a page design similar to Zhihu Daily?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to add functions to buttons for vue How to add functions to buttons for vue Apr 08, 2025 am 08:51 AM

You can add a function to the Vue button by binding the button in the HTML template to a method. Define the method and write function logic in the Vue instance.

React vs. Vue: Which Framework Does Netflix Use? React vs. Vue: Which Framework Does Netflix Use? Apr 14, 2025 am 12:19 AM

Netflixusesacustomframeworkcalled"Gibbon"builtonReact,notReactorVuedirectly.1)TeamExperience:Choosebasedonfamiliarity.2)ProjectComplexity:Vueforsimplerprojects,Reactforcomplexones.3)CustomizationNeeds:Reactoffersmoreflexibility.4)Ecosystema

Netflix's Frontend: Examples and Applications of React (or Vue) Netflix's Frontend: Examples and Applications of React (or Vue) Apr 16, 2025 am 12:08 AM

Netflix uses React as its front-end framework. 1) React's componentized development model and strong ecosystem are the main reasons why Netflix chose it. 2) Through componentization, Netflix splits complex interfaces into manageable chunks such as video players, recommendation lists and user comments. 3) React's virtual DOM and component life cycle optimizes rendering efficiency and user interaction management.

How to jump to the div of vue How to jump to the div of vue Apr 08, 2025 am 09:18 AM

There are two ways to jump div elements in Vue: use Vue Router and add router-link component. Add the @click event listener and call this.$router.push() method to jump.

React, Vue, and the Future of Netflix's Frontend React, Vue, and the Future of Netflix's Frontend Apr 12, 2025 am 12:12 AM

Netflix mainly uses React as the front-end framework, supplemented by Vue for specific functions. 1) React's componentization and virtual DOM improve the performance and development efficiency of Netflix applications. 2) Vue is used in Netflix's internal tools and small projects, and its flexibility and ease of use are key.

How to jump a tag to vue How to jump a tag to vue Apr 08, 2025 am 09:24 AM

The methods to implement the jump of a tag in Vue include: using the a tag in the HTML template to specify the href attribute. Use the router-link component of Vue routing. Use this.$router.push() method in JavaScript. Parameters can be passed through the query parameter and routes are configured in the router options for dynamic jumps.

How to implement component jump for vue How to implement component jump for vue Apr 08, 2025 am 09:21 AM

There are the following methods to implement component jump in Vue: use router-link and &lt;router-view&gt; components to perform hyperlink jump, and specify the :to attribute as the target path. Use the &lt;router-view&gt; component directly to display the currently routed rendered components. Use the router.push() and router.replace() methods for programmatic navigation. The former saves history and the latter replaces the current route without leaving records.

How to develop a complete Python Web application? How to develop a complete Python Web application? May 23, 2025 pm 10:39 PM

To develop a complete Python Web application, follow these steps: 1. Choose the appropriate framework, such as Django or Flask. 2. Integrate databases and use ORMs such as SQLAlchemy. 3. Design the front-end and use Vue or React. 4. Perform the test, use pytest or unittest. 5. Deploy applications, use Docker and platforms such as Heroku or AWS. Through these steps, powerful and efficient web applications can be built.

See all articles