adminLayout.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. <script>
  2. export default {
  3. name: "adminLayout",
  4. props: {
  5. headerHeight: {
  6. default: '35px'
  7. },
  8. sideBarWidth: {
  9. default: '270px'
  10. },
  11. sideBarHeight: {
  12. default: '35px'
  13. },
  14. bgColor: {default: '#fff'},
  15. sideBarColor: {default: '#bebebe'},
  16. headerBgColor: {default: '#086fb6'},
  17. // 文本颜色, 主要文本颜色 次要文本颜色
  18. textColor: {default: '#333'},
  19. mainTextColor: {default: '#333'},
  20. textColorHover: {default: '#fff'},
  21. mainTextColorHover: {default: '#fff'},
  22. btnColor: {default: '#086fb6'},
  23. btnColorHover: {default: '#086fb6'},
  24. btnTextColor: {default: '#fff'},
  25. btnTextColorHover: {default: '#fff'},
  26. sidebarMenus: {
  27. default(){
  28. return []
  29. }
  30. },
  31. interactSideBar: {
  32. default: true
  33. },
  34. logoTitle: {
  35. default: ''
  36. },
  37. headerMenus: {
  38. default(){
  39. return []
  40. }
  41. },
  42. headerMenuKey: {
  43. default: ''
  44. },
  45. userName: {
  46. default: ''
  47. },
  48. btnLink: {
  49. type: Boolean,
  50. default: false
  51. },
  52. activeKey: {
  53. default: ''
  54. },
  55. },
  56. data(){
  57. return {
  58. isShowSideBar: true,
  59. cssText: '',
  60. sideMenus: [],
  61. }
  62. },
  63. beforeMount() {
  64. this.init_computedStyle();
  65. },
  66. watch:{
  67. sidebarMenus(){
  68. this.init_comSideBar();
  69. },
  70. },
  71. mounted() {
  72. this.init_comSideBar();
  73. },
  74. methods:{
  75. init_comSideBar(){
  76. // 循环遍历菜单
  77. this.sideMenus = this.sidebarMenus.map((item)=>{
  78. let newItem = {...item};
  79. newItem.isShow = false;
  80. return newItem;
  81. });
  82. },
  83. switchHandle(){
  84. if(!this.interactSideBar){return console.log('不允许交互sidebar');}
  85. this.isShowSideBar = !this.isShowSideBar;
  86. this.init_computedStyle();
  87. },
  88. switchSideBarMenuHandle(item){
  89. item.isShow = !item.isShow;
  90. },
  91. /**
  92. * 计算dom元素样式
  93. */
  94. init_computedStyle(){
  95. let cssText = `
  96. --headerHeight: ${this.headerHeight};
  97. --sideBarWidth: ${this.isShowSideBar?this.sideBarWidth:this.sideBarHeight};
  98. --baseSideBarWidth: ${this.sideBarWidth};
  99. --sideBarHeight: ${this.sideBarHeight};
  100. --bg-color: ${this.bgColor};
  101. --sideBarBgColor: ${this.sideBarColor};
  102. --header-color: ${this.headerBgColor};
  103. --text-color: ${this.textColor};
  104. --main-text-color: ${this.mainTextColor};
  105. --text-color-hover: ${this.textColorHover};
  106. --main-text-color-hover: ${this.mainTextColorHover};
  107. --btn-color: ${this.btnColor};
  108. --btn-color-hover: ${this.btnColorHover};
  109. --btn-text-color: ${this.btnTextColor};
  110. --btn-text-color-hover: ${this.btnTextColorHover};
  111. `;
  112. this.cssText = cssText;
  113. },
  114. }
  115. }
  116. </script>
  117. <template>
  118. <!-- 管理员控制台布局 永久性头部,可伸缩侧边栏-->
  119. <div class="lay-con" :style="cssText">
  120. <!-- 头部横条 -->
  121. <div class="lay-header" >
  122. <div class="lay-logo">
  123. <slot name="logo">
  124. <h1>{{logoTitle}}</h1>
  125. </slot>
  126. </div>
  127. <div class="lay-menus">
  128. <div class="menus">
  129. <span
  130. v-for="item in headerMenus"
  131. :key="item.key"
  132. :class="`menu ${headerMenuKey===item.key?'':''}`"
  133. >
  134. <a :href="item.href">{{item.text}}</a>
  135. </span>
  136. </div>
  137. <div class="bell">
  138. <slot name="bell"> </slot>
  139. </div>
  140. <div class="user">
  141. <slot name="user">
  142. <span>{{userName}}</span>
  143. </slot>
  144. </div>
  145. </div>
  146. </div>
  147. <!-- 内容部分-->
  148. <div class="lay-box" >
  149. <div :class="`lay-sideBar ${!isShowSideBar?'lay-sideBar-hide':''}`" >
  150. <!-- 侧边按钮 -->
  151. <div class="lay-sideBar-control" @click="switchHandle">
  152. <svg-icon :icon-class="isShowSideBar?'prev':'next'"/>
  153. </div>
  154. <!-- 侧边栏,带标题,icon,下拉菜单-->
  155. <div v-for="(item,i) in sideMenus"
  156. :key="'sideBar-'+i"
  157. :class="`bar-menu-item ${item.isShow?'bar-menu-item-show':''}`"
  158. >
  159. <div class="menu-title">
  160. <span class="icon"> <svg-icon :icon-class="item.icon"/> </span>
  161. <span class="text">{{ item.title }}</span>
  162. <span class="icon option" @click="switchSideBarMenuHandle(item)">
  163. <svg-icon :icon-class="item.isShow?'arrow-up':'arrow-down'"/>
  164. </span>
  165. </div>
  166. <div class="sub-menu-box">
  167. <div v-for="sub in item.child"
  168. :key="sub.key"
  169. :class="`menu-item ${activeKey===sub.key?'menu-items-show':''}`"
  170. >
  171. <span>{{sub.title}}</span>
  172. </div>
  173. </div>
  174. </div>
  175. </div>
  176. <div class="lay-content" >
  177. <slot></slot>
  178. </div>
  179. </div>
  180. </div>
  181. </template>
  182. <style scoped>
  183. .lay-con {
  184. --headerHeight: v-bind(headerHeight);
  185. --sideBarWidth: v-bind(sideBarWidth);
  186. --sideBarHeight: v-bind(sideBarHeight);
  187. --baseSideBarWidth: v-bind(sideBarWidth);
  188. --bg-color: v-bind(bgColor);
  189. --sideBarBgColor: v-bind(sideBarColor);
  190. --header-color: v-bind(headerBgColor);
  191. --text-color: v-bind(textColor);
  192. --main-text-color: v-bind(mainTextColor);
  193. --text-color-hover: v-bind(textColorHover);
  194. --main-text-color-hover: v-bind(mainTextColorHover);
  195. --btn-color: v-bind(btnColor);
  196. --btn-color-hover: v-bind(btnColorHover);
  197. --btn-text-color: v-bind(btnTextColor);
  198. --btn-text-color-hover: v-bind(btnTextColorHover);
  199. width: 100vw;
  200. height: 100vh;
  201. overflow: hidden;
  202. background-color: var(--sideBarBgColor);
  203. position: relative;
  204. }
  205. .lay-header{
  206. background-color: var(--header-color);
  207. display: flex;
  208. width: 100%;
  209. height: var(--headerHeight);
  210. flex-direction: row;
  211. }
  212. .lay-header > *{
  213. width: 50%;
  214. height: 100%;
  215. display: flex;
  216. align-items: center;
  217. }
  218. .lay-logo{
  219. margin-left: 15px;
  220. margin-right: 15px;
  221. box-sizing: border-box;
  222. }
  223. .lay-menus{
  224. justify-content: end;
  225. }
  226. .lay-menus .menus{
  227. display: flex;
  228. padding: 0 5px;
  229. }
  230. .lay-menus .menus .menu{
  231. padding: 5px 15px;
  232. }
  233. .lay-menus .menus .menu:hover{
  234. color: var(--text-color-hover);
  235. }
  236. .lay-box {
  237. width: 100%;
  238. height:calc(100% - var(--headerHeight));
  239. display: flex;
  240. }
  241. .lay-box > *{
  242. transition: width .7s;
  243. height: 100%;
  244. position: relative;
  245. }
  246. .lay-sideBar{
  247. width: var(--sideBarWidth);
  248. }
  249. .lay-content{
  250. width: calc(100% - var(--sideBarWidth));
  251. overflow: hidden;
  252. background-color: #d5d5d5;
  253. }
  254. .lay-sideBar-control{
  255. position: absolute;
  256. width: 25px;
  257. height: 25px;
  258. left: 100%;
  259. top: 0px;
  260. border-radius: 3px;
  261. border: 1px solid black;
  262. z-index: 999;
  263. cursor: pointer;
  264. display: flex;
  265. justify-content: center;
  266. align-items: center;
  267. background-color: var(--sideBarBgColor);
  268. }
  269. .lay-sideBar-control:hover{
  270. border-color: #f0ad4e;
  271. background-color: #f0ad4e;
  272. color: #fff;
  273. }
  274. .bar-menu-item{
  275. width: 100%;
  276. height: auto;
  277. position: relative;
  278. margin-top: 5px;
  279. }
  280. .bar-menu-item .menu-title{
  281. display: flex;
  282. width: 100%;
  283. height: var(--sideBarHeight);
  284. align-items: center;
  285. box-sizing: border-box;
  286. padding-left: 5px;
  287. padding-right: 5px;
  288. cursor: default;
  289. overflow: hidden;
  290. }
  291. .bar-menu-item .menu-title:hover{
  292. color: var(--btn-text-color-hover);
  293. }
  294. .bar-menu-item .menu-title .icon{
  295. width: var(--sideBarHeight);
  296. height: var(--sideBarHeight);
  297. display: flex;
  298. justify-content: center;
  299. align-items: center;
  300. color: var(--text-color);
  301. flex-shrink: 0;
  302. }
  303. .bar-menu-item .menu-title .icon *{
  304. color: inherit;
  305. }
  306. .bar-menu-item .menu-title .option{
  307. cursor: pointer;
  308. color: var(--text-color);
  309. }
  310. .bar-menu-item .menu-title .option:hover{
  311. color: var(--btn-text-color-hover);
  312. }
  313. .bar-menu-item .menu-title .text{
  314. width: calc(100% - var(--sideBarHeight) - var(--sideBarHeight));
  315. display: flex;
  316. align-items: center;
  317. }
  318. .bar-menu-item .sub-menu-box{
  319. width: 100%;
  320. height: 0;
  321. overflow: hidden;
  322. transition: height .7s;
  323. }
  324. .bar-menu-item .sub-menu-box .menu-item{
  325. width: 100%;
  326. height: calc(var(--sideBarHeight) - 5px);
  327. display: flex;
  328. align-items: center;
  329. box-sizing: border-box;
  330. padding-left: calc(var(--sideBarHeight) + 5px);
  331. cursor: pointer;
  332. }
  333. .bar-menu-item .sub-menu-box .menu-item:hover{
  334. color: var(--btn-text-color-hover);
  335. background-color: var(--btn-color-hover);
  336. }
  337. .bar-menu-item-show{
  338. height: auto;
  339. }
  340. .bar-menu-item-show .sub-menu-box{
  341. height: auto;
  342. }
  343. .lay-sideBar-hide .lay-sideBar{
  344. padding-top: 50px;
  345. }
  346. .lay-sideBar-hide .bar-menu-item{
  347. box-sizing: border-box;
  348. margin: 10px 0;
  349. border-top: 1px solid var(--sideBarBgColor);
  350. }
  351. .lay-sideBar-hide .bar-menu-item .menu-title {
  352. padding-left: 0;
  353. padding-right: 0;
  354. }
  355. .lay-sideBar-hide .bar-menu-item .sub-menu-box{
  356. position: absolute;
  357. display: none;
  358. top: 0;
  359. left: 100%;
  360. height: auto;
  361. width: calc(var(--baseSideBarWidth) - var(--sideBarWidth));
  362. padding: 10px 0;
  363. box-shadow: 1px 1px 3px darkgray;
  364. z-index: 9999;
  365. background-color: var(--sideBarBgColor);
  366. }
  367. .lay-sideBar-hide .bar-menu-item:hover{
  368. border-top: 1px solid var(--bg-color);
  369. }
  370. .lay-sideBar-hide .bar-menu-item:hover .sub-menu-box{
  371. display: block;
  372. }
  373. </style>