vue實(shí)現(xiàn)伸縮菜單功能
本文實(shí)例為大家分享了vue實(shí)現(xiàn)伸縮菜單的具體代碼,供大家參考,具體內(nèi)容如下
先看效果:
點(diǎn)擊圖片按鈕可調(diào)整菜單寬度
伸縮后
頁面結(jié)構(gòu)一共分為三部分,加上一個伸縮按鈕,在你的項(xiàng)目對應(yīng)的部分都加上class名。
我這里定義的分別是box、left、mid、resize(按鈕類名)
html
頁面結(jié)構(gòu)劃分完成之后,完善一下樣式(直接復(fù)制,菜單類名換成你的)
/*拖拽區(qū)div樣式*/ .resize { ? ? cursor: col-resize; ? ? position: relative; ? ? // top: 45%; ? ? top: 400px; ? ? background-color: #d6d6d6; ? ? border-radius: 5px; ? ? margin-top: -10px; ? ? width: 10px; ? ? height: 50px; ? ? background-size: cover; ? ? background-position: center; ? ? font-size: 32px; ? ? color: white; } /*拖拽區(qū)鼠標(biāo)懸停樣式*/ .resize:hover { ? ? color: #444444; } //左側(cè)菜單設(shè)置百分比寬度,方便拖拽自適應(yīng) .main_menu { ? ? width:22%; /*右側(cè)初始化寬度*/ ? ? height: 100%; ? ? background: #BF334E!important; ? ? box-shadow: 0px 0px 0px 0px rgba(0, 0, 0, 0.11); }
methods里面的拖拽函數(shù):
// 拖拽事件 dragControllerDiv() { ? ? ? ? ? ? var resize = document.getElementsByClassName('resize') ? ? ? ? ? ? var left = document.getElementsByClassName('left') ? ? ? ? ? ? var mid = document.getElementsByClassName('mid') ? ? ? ? ? ? var box = document.getElementsByClassName('box') ? ? ? ? ? ? for (let i = 0; i < resize.length; i++) { ? ? ? ? ? ? ? ? // 鼠標(biāo)按下事件 ? ? ? ? ? ? ? ? resize[i].onmousedown = function (e) { ? ? ? ? ? ? ? ? ? ? //顏色改變提醒 ? ? ? ? ? ? ? ? ? ? resize[i].style.background = '#818181' ? ? ? ? ? ? ? ? ? ? var startX = e.clientX ? ? ? ? ? ? ? ? ? ? resize[i].left = resize[i].offsetLeft ? ? ? ? ? ? ? ? ? ? // 鼠標(biāo)拖動事件 ? ? ? ? ? ? ? ? ? ? document.onmousemove = function (e) { ? ? ? ? ? ? ? ? ? ? ? ? console.log('鼠標(biāo)按下') ? ? ? ? ? ? ? ? ? ? ? ? var endX = e.clientX ? ? ? ? ? ? ? ? ? ? ? ? var moveLen = resize[i].left + (endX - startX - 270) // (endx-startx)=移動的距離。resize[i].left+移動的距離=左邊區(qū)域最后的寬度 ? ? ? ? ? ? ? ? ? ? ? ? var maxT = box[i].clientWidth - resize[i].offsetWidth - 270// 容器寬度 - 左邊區(qū)域的寬度 = 右邊區(qū)域的寬度 ? ? ? ? ? ? ? ? ? ? ? ? console.log(moveLen,maxT) ? ? ? ? ? ? ? ? ? ? ? ? if (moveLen < 32) moveLen = 270 // 左邊區(qū)域的最小寬度為32px ? ? ? ? ? ? ? ? ? ? ? ? if (moveLen > maxT - 150) moveLen = maxT - 650 //右邊區(qū)域最小寬度為150px ? ? ? ? ? ? ? ? ? ? ? ? resize[i].style.left = moveLen // 設(shè)置左側(cè)區(qū)域的寬度 ? ? ? ? ? ? ? ? ? ? ? ? for (let j = 0; j < left.length; j++) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? console.log( left[j].style) ? ? ? ? ? ? ? ? ? ? ? ? ? ? left[j].style.width = moveLen + 'px' ? ? ? ? ? ? ? ? ? ? ? ? ? ? mid[j].style.width = box[i].clientWidth - moveLen - 10 + 'px' ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? // 鼠標(biāo)松開事件 ? ? ? ? ? ? ? ? ? ? document.onmouseup = function (evt) { ? ? ? ? ? ? ? ? ? ? ? ? console.log('鼠標(biāo)收起') ? ? ? ? ? ? ? ? ? ? ? ? //顏色恢復(fù) ? ? ? ? ? ? ? ? ? ? ? ? resize[i].style.background = '#d6d6d6' ? ? ? ? ? ? ? ? ? ? ? ? document.onmousemove = null ? ? ? ? ? ? ? ? ? ? ? ? document.onmouseup = null ? ? ? ? ? ? ? ? ? ? ? ? resize[i].releaseCapture && resize[i].releaseCapture() //當(dāng)你不在需要繼續(xù)獲得鼠標(biāo)消息就要應(yīng)該調(diào)用ReleaseCapture()釋放掉 ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? resize[i].setCapture && resize[i].setCapture() //該函數(shù)在屬于當(dāng)前線程的指定窗口里設(shè)置鼠標(biāo)捕獲 ? ? ? ? ? ? ? ? ? ? return false ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? },
mounted里面調(diào)用:
mounted() { ? ? ? ? ?this.dragControllerDiv() ? ? },
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue3數(shù)據(jù)更新,頁面沒有同步更新的問題及解決
這篇文章主要介紹了Vue3數(shù)據(jù)更新,頁面沒有同步更新的問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03使用idea創(chuàng)建vue項(xiàng)目的圖文教程
Vue.js是一套構(gòu)建用戶界面的框架,只關(guān)注視圖層,它不僅易于上手,還便于與第三方庫或既有項(xiàng)目整合,下面這篇文章主要給大家介紹了關(guān)于使用idea創(chuàng)建vue項(xiàng)目的相關(guān)資料,需要的朋友可以參考下2022-08-08在vue中,v-for的索引index在html中的使用方法
下面小編就為大家分享一篇在vue中,v-for的索引index在html中的使用方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03利用vue + element實(shí)現(xiàn)表格分頁和前端搜索的方法
眾所周知Element 是一套 Vue.js 后臺組件庫,它能夠幫助你更輕松更快速地開發(fā)后臺項(xiàng)目。下面這篇文章主要給大家介紹了關(guān)于利用vue + element實(shí)現(xiàn)表格分頁和前端搜索的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-12-12vue如何使用router.meta.keepAlive對頁面進(jìn)行緩存
這篇文章主要介紹了vue如何使用router.meta.keepAlive對頁面進(jìn)行緩存問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05Vue3中watch監(jiān)聽器及源碼學(xué)習(xí)
本文主要介紹了Vue3中watch監(jiān)聽器及源碼學(xué)習(xí),Watch偵聽器在Vue3中特性進(jìn)行了一些改變和優(yōu)化,下面來詳解的介紹一下基本使用,具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01