ElementPlus轮播图-Vue3

注意:安装时建议使用手机热点,wifi不稳定,会出现执行不成功的现象。

安装

使用包管理器

# 选择一个你喜欢的包管理器

# NPM
npm install element-plus --save

# Yarn
yarn add element-plus

# pnpm
pnpm install element-plus

引入配置

完整引入

如果你对打包后的文件大小不是很在乎,那么使用完整导入会更方便。

// main.ts
import { createApp } from 'vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import App from './App.vue'

const app = createApp(App)

app.use(ElementPlus)
app.mount('#app')

全局配置

在引入 ElementPlus 时,可以传入一个包含 sizezIndex 属性的全局配置对象。 size 用于设置表单组件的默认尺寸,zIndex 用于设置弹出组件的层级,zIndex 的默认值为 2000

完整引入:

import { createApp } from 'vue'
import ElementPlus from 'element-plus'
import App from './App.vue'

const app = createApp(App)
app.use(ElementPlus, { size: 'small', zIndex: 3000 })

使用

https://element-plus.org/zh-CN/component/overview.html

组件:Carousel走马灯——复制代码。

官网代码

<template>
  <div class="block text-center">
    <span class="demonstration">
      Switch when indicator is hovered (default)
    </span>
    <el-carousel height="150px">
      <el-carousel-item v-for="item in 4" :key="item">
        <h3 class="small justify-center" text="2xl">{{ item }}</h3>
      </el-carousel-item>
    </el-carousel>
  </div>
  <div class="block text-center" m="t-4">
    <span class="demonstration">Switch when indicator is clicked</span>
    <el-carousel trigger="click" height="150px">
      <el-carousel-item v-for="item in 4" :key="item">
        <h3 class="small justify-center" text="2xl">{{ item }}</h3>
      </el-carousel-item>
    </el-carousel>
  </div>
</template>

<style scoped>
.demonstration {
  color: var(--el-text-color-secondary);
}

.el-carousel__item h3 {
  color: #475669;
  opacity: 0.75;
  line-height: 150px;
  margin: 0;
  text-align: center;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
</style>

自己修改后

<template>
  <div class="swiper">
    <div class="block text-center">
      <span class="demonstration">
        Switch when indicator is hovered (default)
      </span>
      <el-carousel height="470px">
        <el-carousel-item v-for="(item, index) in SwiperData" :key="index">
          <!-- 使用 img 标签来显示图片 -->
          <img :src="item.img" alt="slide" />
        </el-carousel-item>
      </el-carousel>
    </div>
    <!-- <div class="block text-center" m="t-4">
    <span class="demonstration">Switch when indicator is clicked</span>
    <el-carousel trigger="click" height="150px">
      <el-carousel-item v-for="item in 4" :key="item">
        <h3 class="small justify-center" text="2xl">{{ item }}</h3>
      </el-carousel-item>
    </el-carousel>
  </div> -->
  </div>
</template>
  <script>
import { ref } from "vue";
export default {
  name: "LayOut",
  // 组件逻辑
  setup() {
    const SwiperData = ref([
      { img: require("@/assets/images/slide1.jpg") },
      { img: require("@/assets/images/slide2.jpg") },
      { img: require("@/assets/images/slide3.jpg") },
    ]);
    return {
      SwiperData,
    };
  },
};
</script>
  
<style>
.swiper {
  width: 590px;
  height: 470px;
  margin: 0 auto;
}
/* 组件样式 */
.demonstration {
  display: none;
  color: var(--el-text-color-secondary);
}

.el-carousel__item h3 {
  color: #475669;
  opacity: 0.75;
  line-height: 150px;
  margin: 0;
  text-align: center;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
</style>

自定义配置

Carousel API

Carousel Attributes

属性名说明类型Default
heightcarousel 的高度string‘’
initial-index初始状态激活的幻灯片的索引,从 0 开始number0
trigger指示器的触发方式enumhover
autoplay是否自动切换booleantrue
interval自动切换的时间间隔,单位为毫秒number3000
indicator-position指示器的位置enum‘’
arrow切换箭头的显示时机enumhover
typecarousel 的类型enum‘’
cardScale 2.7.8当 type 为 card时,二级卡的缩放大小number0.83
loop是否循环显示booleantrue
direction展示的方向enumhorizontal
pause-on-hover鼠标悬浮时暂停自动切换booleantrue
motion-blur 2.6.0添加动态模糊以给走马灯注入活力和流畅性。booleanfalse
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
可以按照以下步骤使用 Element Plus 封装 el-drawer 组件: 1. 安装 Element Plus 在项目中安装 Element Plus,可以使用 npm 或 yarn: ``` npm install element-plus --save ``` 或 ``` yarn add element-plus ``` 2. 创建 Drawer 组件 在 components 目录下创建 Drawer.vue 文件,定义 Drawer 组件的模板和样式: ```html <template> <el-drawer :visible="visible" :direction="direction" :size="size" :before-close="beforeClose" :close-on-click-modal="closeOnClickModal" :close-on-press-escape="closeOnPressEscape" :lock-scroll="lockScroll" :modal="modal" :custom-class="customClass" :with-header="withHeader" :with-footer="withFooter" :show-close="showClose" > <div class="drawer-body"> <slot></slot> </div> </el-drawer> </template> <script> import { defineComponent } from 'vue'; export default defineComponent({ name: 'Drawer', props: { visible: { type: Boolean, default: false, }, direction: { type: String, default: 'rtl', }, size: { type: String, default: '30%', }, beforeClose: Function, closeOnClickModal: { type: Boolean, default: true, }, closeOnPressEscape: { type: Boolean, default: true, }, lockScroll: { type: Boolean, default: true, }, modal: { type: Boolean, default: true, }, customClass: String, withHeader: { type: Boolean, default: true, }, withFooter: { type: Boolean, default: true, }, showClose: { type: Boolean, default: true, }, }, }); </script> <style> .drawer-body { padding: 20px; } </style> ``` 3. 注册 Drawer 组件 在 main.js 中注册 Drawer 组件: ```js import { createApp } from 'vue'; import ElementPlus from 'element-plus'; import 'element-plus/lib/theme-chalk/index.css'; import App from './App.vue'; import Drawer from './components/Drawer.vue'; const app = createApp(App); app.use(ElementPlus); app.component('Drawer', Drawer); app.mount('#app'); ``` 4. 使用 Drawer 组件 在需要使用 Drawer 组件的页面中引入: ```html <template> <div> <el-button @click="openDrawer">打开 Drawer</el-button> <drawer :visible="visible" @update:visible="visible = $event"> <h3 slot="title">这是标题</h3> <p>这是内容</p> </drawer> </div> </template> <script> import { defineComponent, ref } from 'vue'; export default defineComponent({ name: 'App', components: { Drawer: () => import('./components/Drawer.vue'), }, setup() { const visible = ref(false); function openDrawer() { visible.value = true; } return { visible, openDrawer, }; }, }); </script> ``` 这样就可以使用 Element Plus 封装的 Drawer 组件了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小天博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值