Element-UI组件实现全局回到顶部功能

目录

一 项目创建步骤

二 删除不必要的文件

三 删除不必要的代码

四 安装 element-ui

五 启动项目

六 测试

七 正式编写的代码

八 测试效果

九 源码地址


一 项目创建步骤

1 创建项目

vue create ele-components

2 选择必要组件

? Check the features needed for your project:
(*) Babel
( ) TypeScript
( ) Progressive Web App (PWA) Support
(*) Router
( ) Vuex
(*) CSS Pre-processors
>( ) Linter / Formatter
( ) Unit Testing
( ) E2E Testing

3 路由模式选择history模式

? Please pick a preset: Manually select features
? Check the features needed for your project: Babel, Router, CSS Pre-processors
? Use history mode for router? (Requires proper server setup for index fallback in production) (Y/n) y

4 预处理器选择 node-sass

? Pick a CSS pre-processor (PostCSS, Autoprefixer and CSS Modules are supported by default):
  Sass/SCSS (with dart-sass)
> Sass/SCSS (with node-sass)
  Less
  Stylus

5 选择默认配置文件

> In dedicated config files
  In package.json

6 不保存为预制项目

? Save this as a preset for future projects? (y/N) n

7 使用npm安装依赖

? Pick the package manager to use when installing dependencies:
  Use Yarn
> Use NPM

8 安装过程

Vue CLI v4.3.1
✨  Creating project in F:\vue\ele-components\ele-components.
⚙️  Installing CLI plugins. This might take a while...

二 删除不必要的文件

About.vue

HelloWord.vue

三 删除不必要的代码

1 index.js删除后的样子

import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

  const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

export default router

2 Home.vue删除后的样子

<template>
  <div>
    home
  </div>
</template>
<script>
  
export default {
  name: 'Home',
  components: {
  }
}
</script>

3 APP.vue删除后的样子

<template>
  <div>
    <router-view/>
  </div>
</template>

<style lang="scss">
</style>

四 安装 element-ui

cnpm i -S element-ui

五 启动项目

npm run serve

六 测试

1 浏览器输入  http://localhost:8080/

2 界面呈现如下

七 正式编写的代码

1 main.js

import Vue from 'vue'
import App from './App.vue'
import router from './router'
// 导入ElementUI
import ElementUI from 'element-ui'
// 导入ElementUI的样式
import 'element-ui/lib/theme-chalk/index.css'
// 导入全局组件
import './globalComponents'
// 使用导入ElementUI
Vue.use(ElementUI)
Vue.config.productionTip = false


new Vue({
  router,
  render: h => h(App)
}).$mount('#app')

2 Home.vue

<!--主页面-->
<template>
    <div>
        <el-button type="primary" @click="goTo('/backTopOne')">去往页面1</el-button>
        <el-button type="primary" @click="goTo('/backTopTwo')">去往页面2</el-button>
    </div>
</template>
<script>
    export default {
        name: 
实现Element UI回到顶部功能,可以按照以下步骤进行操作: 1. 在`src/components`目录下创建一个名为`BackTop.vue`的文件,并在其中编写回到顶部组件代码。可以参考以下代码: ```vue <template> <div class="back-top" @click="scrollToTop"> <i class="el-icon-arrow-up"></i> </div> </template> <script> export default { methods: { scrollToTop() { window.scrollTo({ top: 0, behavior: 'smooth', }); }, }, }; </script> <style scoped> .back-top { position: fixed; right: 20px; bottom: 20px; width: 40px; height: 40px; background-color: #ccc; border-radius: 50%; display: flex; justify-content: center; align-items: center; cursor: pointer; } .back-top i { font-size: 20px; } </style> ``` 2. 在`src/globalComponents/index.js`文件中,使用`Vue.component`方法注册全局的`backTop`组件。可以参考以下代码: ```javascript import Vue from "vue"; import BackTop from "../components/backTop/BackTop"; Vue.component('backTop', BackTop); ``` 3. 在需要使用回到顶部的页面或组件中,使用`<back-top></back-top>`标签来引用回到顶部组件。例如,在`App.vue`中的模板中添加该标签。 ```vue <template> <div id="app"> <!-- 其他内容 --> <back-top></back-top> </div> </template> ``` 这样,就可以在使用Element UI的项目中添加回到顶部功能了。请根据您的项目结构和需求进行相应的调整。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Element-UI组件实现全局回到顶部功能](https://blog.csdn.net/chengqiuming/article/details/108154922)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值