iis服务器助手广告广告
返回顶部
首页 > 资讯 > 前端开发 > JavaScript >vue实现拖动左侧导航栏变大变小
  • 808
分享到

vue实现拖动左侧导航栏变大变小

2024-04-02 19:04:59 808人浏览 安东尼
摘要

本文实例为大家分享了Vue实现拖动左侧导航栏变大变小的具体代码,供大家参考,具体内容如下 <template>   <div>     <div cl

本文实例为大家分享了Vue实现拖动左侧导航栏变大变小的具体代码,供大家参考,具体内容如下

<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>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。

--结束END--

本文标题: vue实现拖动左侧导航栏变大变小

本文链接: https://www.lsjlt.com/news/144401.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作