vue+elementui的导航滑块组件

工作需要做一个带滑块效果的导航栏,初步想法是用element的导航组件去做,后面摸坑结合各位大佬的博客初步实现效果,话不多说,直接上代码,记录一下爬坑之旅

  1 <template>
  2   <div class="y-nav">
  3     <el-row class="nav">
  4       <el-menu
  5         :default-active="$route.path"
  6         class="el-menu-demo"
  7         mode="horizontal"
  8         @select="handleSelect"
  9         text-color="#fff"
 10       >
 11         <el-menu-item index="/index">
 12           <router-link
 13             to="/"
 14             @mouseover.native="itransition($event )"
 15             @mouseout.native="outtransition($event)"
 16           >首页</router-link>
 17         </el-menu-item>
 18         <el-menu-item index="/leaderboard/leaderlist">
 19           <router-link
 20             :to="{name:'leaderboard'}"
 21             @mouseover.native="itransition($event )"
 22             @mouseout.native="outtransition($event)"
 23           >排行榜</router-link>
 24         </el-menu-item>
 25         <el-menu-item index="/library">
 26           <router-link
 27             :to="{name:'library'}"
 28             @mouseover.native="itransition($event )"
 29             @mouseout.native="outtransition($event)"
 30           >书库</router-link>
 31         </el-menu-item>
 32         <el-menu-item index="/recharge">
 33           <router-link
 34             to="/recharge"
 35             @mouseover.native="itransition($event )"
 36             @mouseout.native="outtransition($event)"
 37           >充值</router-link>
 38         </el-menu-item>
 39         <el-menu-item index="/vip">
 40           <router-link
 41             :to="{name:'vip'}"
 42             @mouseover.native="itransition($event )"
 43             @mouseout.native="outtransition($event)"
 44           >VIP专区</router-link>
 45         </el-menu-item>
 46         <el-menu-item index="/institution">
 47           <router-link
 48             to="/institution"
 49             @mouseover.native="itransition($event )"
 50             @mouseout.native="outtransition($event)"
 51           >院校</router-link>
 52         </el-menu-item>
 53         <el-menu-item index="/community">
 54           <router-link
 55             to="/community"
 56             @mouseover.native="itransition($event )"
 57             @mouseout.native="outtransition($event)"
 58           >社区</router-link>
 59         </el-menu-item>
 60         <el-menu-item index="/author">
 61           <router-link
 62             to="/author"
 63             @mouseover.native="itransition($event )"
 64             @mouseout.native="outtransition($event)"
 65           >作者福利</router-link>
 66         </el-menu-item>
 67 
 68         <div class="moveblock" ref="moveblock"></div>
 69       </el-menu>
 70     </el-row>
 71   </div>
 72 </template>
 73 <script>
 74 export default {
 75   data() {
 76     return {};
 77   },
 78   // created() {
 79   //   console.log(document.querySelector(".el-menu>.is-active").offsetLeft);
 80   // },
 81   mounted() {
 82     //获取menu宽度
 83     let menuwidth = document.querySelector(".el-menu-item.is-active>a")
 84       .offsetWidth;
 85     //获取li
 86     let liwidth = document.querySelectorAll(".el-menu li");
 87     //背景
 88     let moveblock = document.querySelector(".moveblock");
 89     //设置背景的宽度
 90     // moveblock.style.width = liwidth[0].offsetWidth + "px";
 91     let selfLeft = document.querySelector(".el-menu .is-active").offsetLeft;
 92 
 93     moveblock.style.width = menuwidth + "px";
 94 
 95     let winwidth = document.body.clientWidth;
 96     // console.log("base:" + selfLeft);
 97     moveblock.style.left = selfLeft + 35 + "px";
 98   },
 99   methods: {
100     handleSelect(key, keyPath) {
101       console.log(key, keyPath);
102     },
103     itransition($event) {
104       //为悬浮元素添加hover
105       // $event.currentTarget.className = "router-link-active hover";
106       //获取元素的left值
107       let left = $event.currentTarget.getBoundingClientRect().left;
108       //获取移动元素
109       let moveblock = document.querySelector(".moveblock");
110       //获取可视区域的宽度
111       let winwidth = document.body.clientWidth;
112       if (winwidth > 1200) {
113         moveblock.style.left = left - (winwidth - 1200) / 2 + "px";
114       } else {
115         moveblock.style.left = left + "px";
116       }
117     },
118     outtransition($event) {
119       // $event.currentTarget.className = "router-link-active";
120       //获取移动元素
121       let moveblock = document.querySelector(".moveblock");
122       //获取is-active的offsetLeft
123       let selfLeft = document.querySelector(".el-menu .is-active").offsetLeft;
124       // let selfLeft = this.$refs.moveblock.offsetLeft;
125       moveblock.style.left = selfLeft + 35 + "px";
126     }
127   }
128 };
129 </script>
130 <style lang="sass">
131     @import "@/assets/css/public/header/nav.scss";
132 </style>
一、element组件的is-active :default-active="$route.path" 导航的is-active,当时纠结了好久,百度了好久看到这个办法,还是不行,后来才注意到要结合 <el-menu-item index="/index"> 获取当前的路由路径和index中的数据进行对比,添加is-active   二、关于刷新后的元素offsetleft问题    最初的想法是
let selfLeft = document.querySelectorAll(".el-menu .is-active").offsetLeft;
 刷新后发现selfLeft的值为undefined,怀疑是mounted中无法获取刷新后元素的dom,先后试验了$nextTick 和setTimeout均无法解决问题,后将整体代码放到created()中仍未解决问题,    在created()中直接打印document.querySelectorAll(".el-menu .is-active").offsetLeft;也是现实undefined,document.querySelectorAll(".el-menu .is-active")为一个数组结构,果断换用
let selfLeft = document.querySelector(".el-menu .is-active").offsetLeft;
 出现数据,至此组件效果基本实现,代码结构稍显混乱,感觉有很大的优化空间,望各位大佬提出建议~  


原文地址:https://www.cnblogs.com/Keyrt/p/10910511.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


el-menu 有个属性 :default-active="curActive"  el-menu-item有个属性:index=“home”这2个属性值对上号就自动定位了data(){ return{ curActive:"home" }; },
基础用法1<el-inputv-model="input1"palcehoder="请输入"></el-input>23varMain={4data(){5return{6input1:''7}8}9}10varCtor=Vue.extend(Main)11newCtor().$mount('#app&#03
 1.安装element-uinpminstallelement-ui-S 2.在main.js中importElementUIfrom'element-ui'import'element-ui/libheme-chalk/index.css'Vue.use(ElementUI)注意index.css的路径不要出错:在node_modules文件夹里node_modules\element-ui\lib\theme-c
layout布局通过基础的24分栏,可进行快速布局基础布局使用单一分栏创建基础的栅格布局,通过span属性指定每栏的大小<el-col:span="8"></el-col>1<el-row>2<el-col:span="8"><divclass="grid-contentbg-purple"></div>&
 今天做一个选择年份的功能,直接调用了ElementUI里面的DatePicker组件,官网上有该组件的用法介绍,讲得很清楚。 我的代码: 官网说明: 奇怪的事情发生了,我明明按照例子写了  value-format="yyyy" 可是获得的值却一直还是Date对象 仔细检查后,我突然发现我的v-model
  that.end 即为结束日期
vueelementUitree懒加载使用详情2018年11月21日11:17:04开心大表哥阅读数:3513 https://blog.csdn.net/a419419/article/details/84315751 背景:vue下使用elementUI文档:http://element-cn.eleme.io/#/zh-CN/componentree#tree-shu-xing-kong-jian需求:只保存二
环境搭建说明:1、全局安装angluar脚手架npminstall-g@angular/cli2、初始化项目(支持scss)ngnew项目名称--style=scss//进入项目cd项目名称运行代码可以是:serve或者npminstall(安装依赖)npmstart(运行)3、安装elementnpm
1、在写埋点监控项目的时候,需求是表格里面的数据后台传递过来为0,但是要求显示的时候为—,在elementUI判断,将prop去掉在下面加上<templateslot-scope="scope"><emplate>里面在写vue的判断,因为通过Scopedslot可以获取到row,column,$index和store(table内容的状态管理)的数据。2、
<el-table-columnprop="pubArea"//表格data中对应的字段column-key="pubArea"//过滤条件变化时根据此key判断是哪个表头的过滤label="报修类型"align="center"width=
1.npminstallbabel-plugin-component-D   然后.babelrc替换plugins 文件就在根目录下  2.组件中英文及自定义中英文importVueI18nfrom'vue-i18n'importenLocalefrom'element-ui/lib/locale/lang/en'importzhLocalefrom'element-ui/lib/locale/lang/zh-C
 <el-treeref="tree":data="menu.treeData":props="menu.defaultProps":filter-node-method="filterNode":expand-on-click-node=&quot
2019独角兽企业重金招聘Python工程师标准>>>使用vue+elementui的tree组件,elementui官网elementui的tree组件问题描述:tree层级过多时左右不可滚动问题解决:修改overflow属性值.el-tree-node>.el-tree-node_children{overflow:visible;} 其他相关链接:css--ov
首先elementUI的导航栏中的选中项的高亮显示时的字体颜色可以在属性中设置,但是高亮时的背景颜色不能设置,所以要自己修改高亮的背景颜色.el-menu-item.is-active{background-color:#00b4aa!important;} 在使用elementUI构建vue项目的时候会遇到页面刷新的时候子路由会保
1.首先创建Vue项目(使用vue-cli进行创建)创建项目文章指导地址: https://blog.csdn.net/assiduous_me/article/details/892086492.ElementUI:一套为开发者、设计师和产品经理准备的基于Vue2.0的桌面端组件库 链接地址:http://element.eleme.io/#/zh-CN3.正式开
使用vue开发项目,用到elementUI,根据官网的写法,我们可以自定义主题来适应我们的项目要求,下面来介绍一下两种方法实现的具体步骤,(可以参考官方文档自定义主题官方文档),先说项目中没有使用scss编写,用主题工具的方法(使用的较多)第一种方法:使用命令行主题工具使用vue-cli安装完项目并引
每页显示的序号都是一样的:<el-table:data="tableData"highlight-current-row@current-change="handleCurrentChange"><el-table-columntype="index"width="50"></el-table-column><el-table>根据
  记录一下自己踩的坑,控制element内的table的某列显示隐藏不能用v-show,而是要用v-if具体网上百度了,看一下v-show和v-if的区别吧猜测:由于el-table-column会生成多行标签元素,根据v-show是不支持template语法的,推断v-show不能显示隐藏多个元素 
样式重置单vue文件中重置全局重置多写一个style不加scrop,可能会影响全局样式待补充table单价数量小计3252?在ElementUI中,我需要获取row内的数据并进行计算,需要用到v-slot<el-table-columnlabel="小计"><templatev-slot="scope">{{scop
工作需要做一个带滑块效果的导航栏,初步想法是用element的导航组件去做,后面摸坑结合各位大佬的博客初步实现效果,话不多说,直接上代码,记录一下爬坑之旅1<template>2<divclass="y-nav">3<el-rowclass="nav">4<el-menu5:default-active="$route.