search
HomeWeb Front-endVue.jsHow to implement horizontal scrolling list in Vue?

Vue.js is a very popular front-end framework that allows for rapid development and is very scalable. When we need to implement a horizontal scrolling list, Vue.js also provides some very convenient components and instructions, which allow us to achieve a beautiful horizontal scrolling effect without requiring too much code.

This article will introduce a simple method to implement a horizontal scrolling list using Vue.js and some third-party libraries.

Preparation

First, we need to introduce some third-party libraries: Vue, Vue-Swiper and Swiper. Among them, Vue-Swiper is a carousel component suitable for Vue.js, and Swiper is a very powerful carousel plug-in library.

In our Vue.js project, these libraries can be installed in the following ways:

# 通过 npm 安装
npm install vue vue-swiper swiper --save

Implementation method

Next, we need to follow the following steps to implement our Horizontal scrolling list:

1. Introduction of components and libraries

We need to introduce the three libraries we just installed into the JavaScript file of Vue.js:

import Vue from 'vue';
import VueSwiper from 'vue-swiper';
import 'swiper/css/swiper.css';
import Swiper from 'swiper';

At the same time , we also need to introduce CSS styles into the HTML file of Vue.js:

<link rel="stylesheet" href="/node_modules/swiper/css/swiper.css">

2. Create a container

We need to create a container to contain the entire scrolling list. In the HTML file of Vue.js, we can create a container like the following:

<div class="swiper-container">
  <div class="swiper-wrapper">
    <div class="swiper-slide" v-for="(item, index) in items" :key="index">{{ item }}</div>
  </div>
</div>

In the above HTML example, we define a scrolling list container .swiper-container, A .swiper-wrapper that contains all scroll list items, and .swiper-slide for each scroll list item. We use the v-for directive to loop through each list item, and the array items contains all the list item data.

3. Initialize Swiper

Next, we need to initialize Swiper in the mounted hook function in the JavaScript file of Vue.js:

export default {
  data() {
    return {
      items: ['item1', 'item2', 'item3', 'item4', 'item5', 'item6', 'item7']
    }
  },
  mounted() {
    new Swiper('.swiper-container', {
      slidesPerView: 'auto',
      spaceBetween: 30,
      scrollbar: {
        el: '.swiper-scrollbar',
        hide: false,
      },
    });
  }
}

In the above JavaScript example , we create a Swiper instance and pass the Swiper container selector .swiper-container as a parameter to the constructor of Swiper.

We also specify the view amount of each list item slidesPerView to auto, which automatically calculates the width of each list item. At the same time, we defined the interval between list items as 30, and enabled the scroll bar function.

4. Style settings

Finally, we need to add some styles to our scrolling list. In our Vue.js CSS file, we can define the style like this:

.swiper-container {
  width: 100%;
  height: 100px;
  overflow: hidden;
  position: relative;
}

.swiper-wrapper {
  position: relative;
  width: fit-content;
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
}

.swiper-slide {
  padding: 20px;
  background-color: #f5f5f5;
}

In the above CSS code, we define the width of the container .swiper-container as 100%, height is 100px. We use the overflow: hidden attribute to hide the overflow part. We also define the styles of .swiper-wrapper and .swiper-slide through the position: relative attribute, as well as the background color of the scrolling list items.

Conclusion

Through the above steps, we can use Vue.js and Swiper components to implement a complete horizontal scrolling list. As we can see, the process is relatively simple and with just some minor configuration and styling we can easily implement a horizontally scrolling list.

The above is the detailed content of How to implement horizontal scrolling list in Vue?. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
如何使用Python从列表中删除方括号如何使用Python从列表中删除方括号Sep 05, 2023 pm 07:05 PM

Python是一款非常有用的软件,可以根据需要用于许多不同的目的。Python可以用于Web开发、数据科学、机器学习等许多其他需要自动化处理的领域。它具有许多不同的功能,可以帮助我们执行这些任务。Python列表是Python的一个非常有用的功能之一。顾名思义,列表包含您希望存储的所有数据。它基本上是一组不同类型的信息。删除方括号的不同方法许多时候,用户会遇到列表项显示在方括号中的情况。在本文中,我们将详细介绍如何去掉这些括号,以便更好地查看您的列表。字符串和替换函数删除括号的最简单方法之一是在

制作 iPhone 上 iOS 17 提醒应用程序中的购物清单的方法制作 iPhone 上 iOS 17 提醒应用程序中的购物清单的方法Sep 21, 2023 pm 06:41 PM

如何在iOS17中的iPhone上制作GroceryList在“提醒事项”应用中创建GroceryList非常简单。你只需添加一个列表,然后用你的项目填充它。该应用程序会自动将您的商品分类,您甚至可以与您的伴侣或扁平伙伴合作,列出您需要从商店购买的东西。以下是执行此操作的完整步骤:步骤1:打开iCloud提醒事项听起来很奇怪,苹果表示您需要启用来自iCloud的提醒才能在iOS17上创建GroceryList。以下是它的步骤:前往iPhone上的“设置”应用,然后点击[您的姓名]。接下来,选择i

Del和remove()在Python中的列表上有什么区别?Del和remove()在Python中的列表上有什么区别?Sep 12, 2023 pm 04:25 PM

在讨论差异之前,让我们先了解一下Python列表中的Del和Remove()是什么。Python列表中的Del关键字Python中的del关键字用于从List中删除一个或多个元素。我们还可以删除所有元素,即删除整个列表。示例使用del关键字从Python列表中删除元素#CreateaListmyList=["Toyota","Benz","Audi","Bentley"]print("List="

使用Python根据列表创建多个目录使用Python根据列表创建多个目录Sep 08, 2023 am 08:21 AM

Python凭借其简单性和多功能性,已成为各种应用程序中最流行的编程语言之一。无论您是经验丰富的开发人员还是刚刚开始编码之旅,Python都提供了广泛的功能和库,使复杂的任务变得易于管理。在本文中,我们将探讨一个实际场景,Python可以通过自动执行基于列表创建多个目录的过程来帮助我们。通过利用Python内置模块和技术的强大功能,我们可以有效地处理此任务,而无需手动干预。在本教程中,我们将深入研究创建多个目录的问题,并为您提供使用Python解决该问题的不同方法。在本文结束时,我们的目标是为您

我们可以在Java列表中插入空值吗?我们可以在Java列表中插入空值吗?Aug 20, 2023 pm 07:01 PM

SolutionYes,Wecaninsertnullvaluestoalisteasilyusingitsadd()method.IncaseofListimplementationdoesnotsupportnullthenitwillthrowNullPointerException.Syntaxbooleanadd(Ee)将指定的元素追加到此列表的末尾。类型参数E&nbsp;&minus;元素的运行时类型。参数e&nbsp;&minus;要追加到此列表的元

如何使用 Vue 实现可折叠列表?如何使用 Vue 实现可折叠列表?Jun 25, 2023 am 08:45 AM

Vue是一款流行的JavaScript库,广泛应用于Web开发领域。在Vue中,我们可以很方便地实现各种组件和交互效果。其中,可折叠列表是一个比较实用的组件,它可以将列表数据分组,提高数据展示的可读性,同时又能够在需要展开具体内容时进行展开,方便用户查看详细信息。本文就将介绍如何使用Vue实现可折叠列表。准备工作在使用Vue实现可折叠列

在Java中从列表中随机选择项目在Java中从列表中随机选择项目Sep 06, 2023 pm 08:33 PM

List是JavaCollection接口的子接口。它是一种线性结构,按照顺序存储和访问每个元素。为了使用list的特性,我们使用实现了list接口的ArrayList和LinkedList类。在本文中,我们将创建一个ArrayList,并尝试随机选择该列表中的项目。在Java中随机选择列表中的项目的程序随机类别我们创建此类的对象来生成伪随机数。我们将自定义该对象并应用我们自己的逻辑从列表中选择任何随机项目。语法RandomnameOfObject=newRandom();Example1的翻译

iOS 17:如何设置和标记多个计时器iOS 17:如何设置和标记多个计时器Sep 19, 2023 pm 03:29 PM

花了这么长时间,但在iOS17中,Apple增加了对多个计时器的支持,并且还通过引入标签使在iPhone上管理多个计时器变得容易。没错。信不信由你,到目前为止,iPhone还没有包括设置多个持续计时器的功能。在iOS17,时钟应用程序最终可以设置多个计时器,这些计时器将同时运行,使您可以跟踪多件事,例如,这在烹饪包含多道菜的餐点时很方便。您不仅可以同时有多个计时器倒计时,还可以标记计时器,这有助于您在计时器列表中识别每个计时器。这样,您将始终知道哪个计时器与什么相关,并且可以保存自定义计时器,而

See all articles

Hot AI Tools

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.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools