首页 web前端 Vue.js 如何使用 Vue 实现仿知乎日报的页面设计?

如何使用 Vue 实现仿知乎日报的页面设计?

Jun 25, 2023 pm 12:08 PM
vue 页面设计 知乎日报

Vue.js 是一种基于 MVVM 模式的前端框架,通过数据绑定和组件化的方式将数据和 UI 页面进行了解耦,使 Web 开发更加高效和简单。知乎日报是一种新闻客户端,有着美观的 UI 设计,功能强大的交互性以及内容多样性。在本篇文章中,我们将通过 Vue 技术实现一个仿知乎日报的页面设计。

  1. 搭建环境

在开始之前,我们需要先安装 Node.js 和 Vue-cli。安装 Node.js 后,使用命令行工具在终端运行以下命令安装 Vue-cli:

1

$ npm install -g vue-cli

登录后复制

在安装完毕后,使用 Vue-cli 创建一个基于 webpack 模板的项目:

1

$ vue init webpack vue-zhihudaily

登录后复制

此时,我们可以看到 create a new project 问你若干问题(项目名称、描述、作者、是否需要 eslint 代码规范等)之后,会在当前目录下创建一个名为 vue-zhihudaily 的文件夹作为项目根目录。

  1. 页面布局

在仿知乎日报中,主要分为首页、文章列表页和文章详情页三个页面,在这里我们以首页为例。在 src 文件夹中,创建一个 views 文件夹存放界面文件。创建 Home.vue 文件,代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

<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>

登录后复制

在这里,我们使用 flex 布局将页面垂直居中。其中,banner 用于展示轮播图,daily-list 用于显示文章列表。

  1. 使用组件

为了方便复用和维护,我们使用 Vue 组件化来构建界面。在 src 文件夹中,创建一个 components 文件夹存放组件文件。在其中,创建一个名为 DailyItem.vue 的文件:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

<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 用于显示文章列表中的信息,其中包括文章缩略图、标题和日期。在这里,我们使用 props 属性将文章信息传入组件。在 Home.vue 中使用 DailyItem.vue 组件,将 daily-list 替换成:

1

2

3

<div class="daily-list">

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

</div>

登录后复制

当有多篇日报时,该组件会自动地为每一篇日报渲染一个 DailyItem.vue。在父级组件 home 通过 props 将 dailyList 传入子组件 DailyItem.vue。

  1. 轮播图实现

仿知乎日报的轮播图是该页面的重要组成部分。在 src 文件夹中,创建一个名为 Banner.vue 的组件:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

<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>

登录后复制

在 Banner.vue 中,我们使用了 Swiper 第三方库来构建轮播图。在 mounted 钩子函数中调用 swiper.slideTo(0) 来实现初始页面为第一张轮播图。

在 Home.vue 中使用 Banner.vue 组件:

1

2

3

<div class="banner">

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

</div>

登录后复制

此处使用 props 将 bannerList 传入 Banner.vue 组件中。

  1. 数据获取

在仿知乎日报中,首页需要展示文章列表和轮播图。我们使用 axios 库向知乎日报 API 发起 GET 请求,获取轮播图和文章列表的数据。在 src 文件夹下,创建一个名为 api 的文件夹,并在其中创建一个 zhihudaily.js 文件:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

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

  }

}

登录后复制

在 Home.vue 中调用 api 中的方法,将获取到的数据传入对应的 props 中:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

<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>

登录后复制

通过 async/await 语法,我们可以异步地获取需要的数据,使页面效率更高。

  1. 结语

在这篇文章中,我们用 Vue 技术实现了一个仿知乎日报的页面设计,涉及了组件、轮播图、数据获取等知识点。组件化开发使得开发者可以更好地维护和扩展代码,使用第三方库(如 Swiper、axios)可以快速地实现功能,使得开发效率更高。

不断拓展自己的知识库,开阔视野,不断探索,才能在 Web 开发的道路上走得更远。

以上是如何使用 Vue 实现仿知乎日报的页面设计?的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门文章

仓库:如何复兴队友
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 周前 By 尊渡假赌尊渡假赌尊渡假赌

热门文章

仓库:如何复兴队友
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 周前 By 尊渡假赌尊渡假赌尊渡假赌

热门文章标签

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

vue中echarts怎么用 vue中echarts怎么用 May 09, 2024 pm 04:24 PM

vue中echarts怎么用

vue中的export default的作用 vue中的export default的作用 May 09, 2024 pm 06:48 PM

vue中的export default的作用

vue中map函数的用法 vue中map函数的用法 May 09, 2024 pm 06:54 PM

vue中map函数的用法

vue中event和$event区别 vue中event和$event区别 May 08, 2024 pm 04:42 PM

vue中event和$event区别

vue中export与export default区别 vue中export与export default区别 May 08, 2024 pm 05:27 PM

vue中export与export default区别

vue中onmounted作用 vue中onmounted作用 May 09, 2024 pm 02:51 PM

vue中onmounted作用

vue中的onmounted对应react哪个生命周期 vue中的onmounted对应react哪个生命周期 May 09, 2024 pm 01:42 PM

vue中的onmounted对应react哪个生命周期

vue中的钩子是什么 vue中的钩子是什么 May 09, 2024 pm 06:33 PM

vue中的钩子是什么

See all articles