vue实现左侧拖拽拉伸,展开收起

需求:1.左侧是个树形结构,有的文字过长展示不全,想通过拖拽显示全部的数据
           2.展开收起

实现图中效果

<div class="catalog-drag">
     <svg t="1687228434888" class="icon" viewBox="0 0 1024 1024" version="1.1"                         xmlns="http://www.w3.org/2000/svg" p-id="1527" width="16" height="30"><path d="M446.464 146.944v727.552c0 15.872-14.336 27.648-31.744 27.648-17.92 0-31.744-12.288-31.744-27.648V146.944c0-15.872 14.336-27.648 31.744-27.648 17.408-0.512 31.744 11.776 31.744 27.648zM644.608 146.944v727.552c0 15.872-14.336 27.648-31.744 27.648-17.92 0-31.744-12.288-31.744-27.648V146.944c0-15.872 14.336-27.648 31.744-27.648 17.408-0.512 31.744 11.776 31.744 27.648zM84.48 525.312c-8.192-7.68-8.192-22.528 0-30.208L180.224 409.6l88.064-78.848c10.24-9.216 24.576 0 24.576 14.848v328.704c0 15.36-14.336 24.576-24.576 14.848l-88.064-78.848-95.744-84.992zM942.592 525.312c8.192-7.168 8.192-22.016 0-29.696l-95.744-84.992L758.784 332.8c-10.24-9.216-24.576 0-24.576 14.848v325.632c0 15.36 14.336 24.064 24.576 14.848l88.064-77.824 95.744-84.992z" fill="#707070" p-id="1528"></path> </svg>
</div>   
 //左侧侧边栏拖拽事件
    handleLeft() {
      console.log("左侧侧边栏拖拽事件");
      var leftBar = document.getElementsByClassName("catalog-drag")[0];
      var leftTree = document.getElementsByClassName("product-catalog-tree")[0];
      // 鼠标按下事件
      leftBar.onmousedown = function () {
        // 颜色提醒
        leftBar.style.backgroundColor = "#99B8FC";
        // 鼠标拖动事件
        document.onmousemove = function (eventMove) {
          let width = eventMove.clientX + 20;
          console.log("width =>", width);
          if (width >= 600) {
            width = 600; // 设置最大拉伸宽度为600
          } else if (width <= 200) {
            // 当拉伸宽度为小于或等于200,最小拉伸宽度为200
            width = 294;
          }
          leftBar.style.width = width + "px";
        };

        // 鼠标松开事件
        document.onmouseup = function () {
          // 颜色恢复
          leftBar.style.backgroundColor = "#fafcff";
          document.onmousemove = null;
          document.onmouseup = null;
          leftBar .releaseCapture && leftResizeBar.releaseCapture();
        };

        leftBar.setCapture && leftBar.setCapture();
        return false;
      };
    },
.catalog-drag {
    position: absolute;
    width: 4px;
    top: 0;
    right: 0;
    height: 100%;
    cursor: col-resize;
    padding-top: calc(45vh - 10px);
    user-select: none;
    transition: all ease 0.3s;
}

.catalog-drag:hover {
    background-color: #99b8fc !important;
}

 .catalog-drag svg {
    position: absolute;
    left: 1;
}

在mounted里面调用就可以了

实现左侧展开收起

    <div @click="handleShowLeft">
       <img v-if="!isShowLeft" src="../../new_/imgs/icon-fold.svg" style="width:14px" />
       <img v-else src="../../new_/imgs/icon-upfold.svg" style="width: 14px" />
    </div>    
    // 左侧展开收起
    handleShowLeft() {
      this.isShowLeft= !this.isShowLeft;
      var leftTree = document.getElementsByClassName(
        "product-catalog-tree"
      )[0];
      if (this.isShowLeft) {
        leftTree .style.width = "26px";
      } else {
        leftTree.style.width = "294px";
        setTimeout(() => {
          //展开后还能继续拖拽
          this.handleLeft();
        }, 100);
      }
    },


 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Vue实现多行文本的展开收起功能可以通过计算多行文本的高度来进行判断,并使用Vue的条件渲染来实现展开收起效果。 首先,在data中定义一个变量,例如`isExpanded`,用于控制文本的展开收起状态,默认值设为`false`。 然后,在模板中使用条件渲染来根据`isExpanded`的值展示不同的内容。 例如,使用`v-if`指令来判断是否展开,如果展开则显示完整文本,否则只显示指定行数的文本。可以使用`v-text`指令来绑定文本内容。通过CSS的`line-clamp`属性来设置文本行数,超出的部分将被省略。 ``` <template> <div> <div v-if="isExpanded"> <div v-text="text"></div> </div> <div v-else> <div class="clamp-line" v-text="text"></div> </div> <button @click="toggleExpand"> {{ isExpanded ? '收起' : '展开' }} </button> </div> </template> <script> export default { data() { return { isExpanded: false, text: "这是一段多行文本" }; }, methods: { toggleExpand() { this.isExpanded = !this.isExpanded; } } }; </script> <style scoped> .clamp-line { display: -webkit-box; overflow: hidden; text-overflow: ellipsis; -webkit-line-clamp: 3; /* 设置为需要显示的行数 */ -webkit-box-orient: vertical; } </style> ``` 以上代码中,点击按钮时会触发`toggleExpand`方法,通过修改`isExpanded`的值来切换展开收起状态。根据`isExpanded`的值,使用不同的模板来渲染文本内容。 按钮的文本内容也根据`isExpanded`的值来显示“展开”或“收起”。点击按钮时,会调用`toggleExpand`方法切换`isExpanded`的值。 通过以上代码和样式设置,就可以实现一个简单的多行文本展开收起的功能。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值