vue 一个div中三个子div水平布局,一个子div屏幕中间,其他两个子div 依次靠右

一个div 宽度100% 高度10%,其中包含三个子div横向排列, 第一个子div,背景色蓝色,宽度10%,位置是显示屏水平居中,其他两个子div,宽度都是10%,一个背景色黑色,一个背景色灰色 依次水平方向靠右,并且最右边的子div,距离右边距20px,

1 第一种

<div style="width: 100%; height: 10%;">
  <div style="background-color: blue; width: 10%; margin: 0 auto;"></div>
  <div style="background-color: black; width: 10%; float: right;"></div>
  <div style="background-color: gray; width: 10%; float: right; margin-right: 20px;"></div>
</div>

2 第二种

<template>
  <div class="parent">
    <div class="child-blue">sssss</div>
    <div class="child-black">dddd</div>
    <div class="child-gray">ttt</div>
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
  props: {
    msg: String
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="less">
.parent {
  width: 100%;
  height: 10%;

  .child-blue {
    display: block;
    background-color: blue;
    width: 10%;
    margin: 0 auto;//通过 margin: 0 auto; 实现水平居中。

    /**
    当一个元素样式属性里有dumargin:0 auto时,并且父元素的宽度是确定的,
    意思是这个元素处于其父元素的居中位置,并且这个元素的上下外边距为0
    即:上下外边距为0,左右自动,实际效果为左右居中 */
  }

  .child-black {
    background-color: black;
    width: 10%;
    float: right;
    margin-left: 10px;
  }

  .child-gray {
    background-color: gray;
    width: 10%;
    float: right;
    margin-left: 10px;
    margin-right: 20px;
  }
}
</style>

可以使用 Vue3 + TypeScript + CSS3 实现水平放置3个子 div,其中一个 div 可以拖拽改变宽度的效果。下面是一个简单的实现示例,供你参考: ```html <template> <div class="container"> <div class="left" ref="left" :style="{ width: leftWidth + 'px' }"> <!-- 左侧 div,宽度可变 --> </div> <div class="middle"></div><!-- 中间 div,固定宽度 --> <div class="right"></div><!-- 右侧 div,固定宽度 --> <div class="separator" ref="separator" @mousedown="startDrag"></div><!-- 分隔符,用于拖拽 --> </div> </template> <script lang="ts"> import { ref } from 'vue'; export default { setup() { const leftWidth = ref(200); // 左侧 div 的初始宽度 const startDrag = (e: MouseEvent) => { const separator = e.target as HTMLElement; // 获取分隔符元素 const container = separator.parentElement as HTMLElement; // 获取容器元素 const left = container.querySelector('.left') as HTMLElement; // 获取左侧 div 元素 const startX = e.clientX; // 记录鼠标初始位置 const startWidth = left.offsetWidth; // 记录左侧 div 初始宽度 const onMouseMove = (e: MouseEvent) => { const diff = e.clientX - startX; // 计算鼠标移动距离 leftWidth.value = startWidth + diff; // 更新左侧 div 宽度 }; const onMouseUp = () => { document.removeEventListener('mousemove', onMouseMove); document.removeEventListener('mouseup', onMouseUp); }; document.addEventListener('mousemove', onMouseMove); document.addEventListener('mouseup', onMouseUp); }; return { leftWidth, startDrag, }; }, }; </script> <style> .container { display: flex; height: 300px; } .left { height: 100%; background-color: #f0f0f0; flex-grow: 0; flex-shrink: 0; } .middle { width: 400px; height: 100%; background-color: #e0e0e0; flex-grow: 0; flex-shrink: 0; } .right { width: 200px; height: 100%; background-color: #d0d0d0; flex-grow: 0; flex-shrink: 0; } .separator { width: 10px; height: 100%; background-color: #c0c0c0; flex-grow: 0; flex-shrink: 0; cursor: col-resize; } </style> ``` 在上面的代码中,我们使用了 Vue3 的 `ref` 函数来创建了两个引用类型变量 `leftWidth` 和 `separator`,分别用于记录左侧 div 的宽度和分隔符元素。 在 `startDrag` 函数中,我们首先获取了分隔符、容器和左侧 div 的元素,并记录了鼠标初始位置和左侧 div 初始宽度。然后,我们使用 `addEventListener` 函数添加了 `mousemove` 和 `mouseup` 事件的监听器,分别用于实现拖拽效果。在 `mousemove` 事件中,我们计算了鼠标移动距离,并根据移动距离更新了左侧 div 的宽度。在 `mouseup` 事件中,我们移除了事件监听器,完成了拖拽效果的实现。 最后,我们使用 CSS3 的 flex 布局和一些基本样式,实现了水平放置3个子 div,并在第一个 div 中添加了拖拽效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值