Vue3DraggableResizable-例子1

本文介绍了一段Vue3代码,展示了如何在网页中使用`Vue3DraggableResizable`组件创建可拖动且可根据内容自适应大小的红色和蓝色矩形元素,实现灵活的DOM布局管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<template>
  <div id="app">
    <div class="parent">
      <DraggableContainer>
        <Vue3DraggableResizable :parent="true">
          <div style="width:200px;height:40px;background:red;">Test</div>
        </Vue3DraggableResizable>
        <Vue3DraggableResizable :parent="true">
          <div style="width:100px;height:40px;background:blue;">Another test</div>
        </Vue3DraggableResizable>
      </DraggableContainer>
    </div>
  </div>
</template>

<script setup>
</script>

<style>
.parent {
  width: 300px;
  height: 300px;
  position: absolute;
  top: 100px;
  left: 100px;
  border: 1px solid #000;
  user-select: none;
}
</style>

### 关于 Vue3 Draggable Resizable 组件保持活跃状态的实现 为了使 `vue3-draggable-resizable` 组件能够保持其活跃状态,可以通过监听事件并设置内部的状态变量来控制组件的行为。以下是具体的实现方式: #### 1. **通过绑定激活状态** 可以在父组件中定义一个布尔类型的属性(如 `isActive`),并通过 `v-model` 或者 `props` 将该属性传递给子组件。当用户触发某些操作(例如点击或者拖动完成)时,更新这个状态。 ```javascript <template> <vue3-draggable-resizable :active="isActive" @activated="onActivated"> <!-- 子组件内容 --> </vue3-draggable-resizable> </template> <script> import { ref } from &#39;vue&#39;; import Vue3DraggableResizable from &#39;vue3-draggable-resizable&#39;; export default { components: { Vue3DraggableResizable }, setup() { const isActive = ref(false); const onActivated = () => { isActive.value = true; }; return { isActive, onActivated }; } }; </script> ``` 此代码片段展示了如何利用 `@activated` 事件动态改变组件的活跃状态[^1]。 --- #### 2. **持久化状态管理** 如果希望在页面刷新或其他生命周期阶段仍然保留组件的活跃状态,则可以借助 Vuex 或 Pinia 进行全局状态管理。下面是一个基于 Vuex 的简单例子: ##### 定义 Store ```javascript // store.js import { createStore } from &#39;vuex&#39;; const store = createStore({ state: { activeComponentId: null, }, mutations: { setActiveComponent(state, id) { state.activeComponentId = id; }, }, }); export default store; ``` ##### 在组件中使用 ```javascript <template> <vue3-draggable-resizable :key="componentKey" :active="isActive" @activated="setActiveState"> <!-- 子组件内容 --> </vue3-draggable-resizable> </template> <script> import { mapMutations, mapState } from &#39;vuex&#39;; export default { computed: { ...mapState([&#39;activeComponentId&#39;]), isActive() { return this.componentKey === this.activeComponentId; }, }, methods: { ...mapMutations([&#39;setActiveComponent&#39;]), setActiveState() { this.setActiveComponent(this.componentKey); }, }, data() { return { componentKey: Math.random().toString(36).substr(2, 9), // 唯一标识符 }; }, }; </script> ``` 这种方式允许跨多个视图共享同一个活动组件的状态信息[^3]。 --- #### 3. **样式高亮显示** 为了让用户直观感受到某个组件处于活跃状态,还可以为其添加特定 CSS 类名或样式规则。比如,在 JavaScript 中修改 DOM 元素类列表即可达到目的。 ```css /* styles.css */ .dragging-active { border-color: green !important; } .resizing-active { background-color: rgba(0, 255, 0, 0.2) !important; } .normal-state { transition: all .3s ease-in-out; } ``` 接着配合逻辑判断条件切换这些外部表现形式: ```html <div class="normal-state" :class="{ draggingActiveClass: isDragging, resizingActiveClass: isResizing }"> </div> ``` 其中 `isDragging`, `isResizing` 是由相应动作产生的标志位[^4]。 --- ### 总结 以上介绍了三种不同的策略帮助开发者解决 `vue3-draggable-resizable` 库中的组件持续维持活跃状态需求:一是直接依赖本地数据模型;二是采用集中式的存储方案保存长期有效记录;三是运用视觉反馈机制增强用户体验感知度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值