vue實現(xiàn)拖動左側導航欄變大變小
更新時間:2022年03月30日 10:02:00 作者:牽手北京99
這篇文章主要為大家詳細介紹了vue實現(xiàn)拖動左側導航欄變大變小,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了vue實現(xiàn)拖動左側導航欄變大變小的具體代碼,供大家參考,具體內(nèi)容如下

<template> ? <div> ? ? <div class="top">頂部導航</div> ? ? <div id="left"> ? ? ? <div id="menu"> ? ? ? ? <span>左側側邊欄</span> ? ? ? </div> ? ? ? <div id="drap-line"></div> ? ? </div> ? ? <div id="right"> ? ? ? 右邊的div ? ? </div> ? </div> </template>
<style type="text/css">
? * {margin: 0;padding: 0;}
? ? html,body {height: 100%;text-align: center;}
? ? body {position: relative;}
? ? .top {width: 100%;height: 88px;background-color: #ccc;}
? ? #left {position: absolute;top: 88px;right: 0;bottom: 0;left: 0;width: 220px;}
? ? #menu {width: 100%;height: 100%;background-color: red;}
? ? #drap-line {position: absolute;top: 0;right: 0;width: 4px;height: 100%;background-color: #999;cursor: e-resize;}
? ? #right {position: absolute;top: 88px;right: 0;bottom: 0;left: 220px;}
</style>
<script>
export default {
? mounted() {
? ? //獲取dom
? ? var drapLine = document.getElementById('drap-line');
? ? var left = document.getElementById('left');
? ? var right = document.getElementById('right');
? ? //設置最大/最小寬度
? ? var max_width = 600,
? ? ? min_width = 100;
? ? //記錄鼠標相對left盒子x軸位置
? ? var mouse_x = 0;
? ? var history_width = localStorage.getItem('sliderWidth');
? ? if (history_width) {
? ? ? left.style.width = history_width;
? ? ? right.style.left = history_width;
? ? }
? ? //綁定鼠標按下事件
? ? drapLine.onmousedown = function (e) {
? ? ? var e = e || window.event;
? ? ? //阻止默認事件
? ? ? e.preventDefault();
? ? ? mouse_x = e.clientX - left.offsetWidth;
? ? ? document.onmousemove = function (e) {
? ? ? ? var e = e || window.event;
? ? ? ? var left_width = e.clientX - mouse_x;
? ? ? ? left_width = left_width < min_width ? min_width : left_width;
? ? ? ? left_width = left_width > max_width ? max_width : left_width;
? ? ? ? left.style.width = left_width + 'px';
? ? ? ? right.style.left = left_width + 'px';
? ? ? };
? ? ? document.onmouseup = function (e) {
? ? ? ? document.onmousemove = null;
? ? ? ? document.onmouseup = null;
? ? ? ? //localStorage設置
? ? ? ? localStorage.setItem('sliderWidth', left.style.width)
? ? ? };
? ? }
? },
? methods: {
? ??
? },
? watch: {
? ??
? },
? data() {
? ? return {
? ? ??
? ? }
? }
}
</script>
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關文章
vite+ts vite.config.ts使用path報錯問題及解決
這篇文章主要介紹了vite+ts vite.config.ts使用path報錯問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10

