gpt4 book ai didi

javascript - 使用 slider 更改两个 block 的宽度比例

转载 作者:搜寻专家 更新时间:2023-10-30 22:21:10 27 4
gpt4 key购买 nike

我正在尝试设计一个组件,您可以在其中通过左右移动 slider 来更改两个 block 的宽度比例: enter image description here

codpen和演示:

.outer {
display: flex;
flex-direction: row;
}

.block {
height: 200px;
width: -webkit-calc(50% - 5px);
width: -moz-calc(50% - 5px);
width: calc(50% - 5px);
}

.block-1 {
background-color: red;
}

.block-2 {
background-color: green;
}

.slider {
line-height: 100%;
width: 10px;
background-color: #dee2e6;
border: none;
cursor: e-resize;
}
<div id="app">
<div class="outer">
<div class="block block-1">
Block 1
</div>
<div class="slider">
S<br>l<br>i<br>d<br>e<br>r
</div>
<div class="block block-2">
Block 2
</div>
</div>
</div>

我试过使用 draggable-vue-directive并根据 slider 位置更改 block 的宽度。

但是,它并没有很好地工作,因为 draggable-vue-directive 将 slider 设置为 position:fixed,这反过来又弄乱了 block 对齐。

如何在不设置 position:fixed 的情况下使 .slider block 水平拖动?

如何在 slider 移动时正确调整 Block1Block2 的大小?

注意:我没有使用 jQuery

最佳答案

你可以调整你的 flexboxresize - 缺点 slider 不是很可定制:

  • resize: horizo​​ntal 添加到其中一个 flex 元素
  • flex: 1 添加到另一个 flex 元素(这样 this flex 元素将在调整大小时响应另一个 flex 元素的变化宽度自动调整)

请看下面的演示:

.outer {
display: flex;
flex-direction: row;
}

.block {
height: 100px;
width: 50%; /* 50% would suffice*/
}

.block-1 {
background-color: red;
resize: horizontal; /* resize horizontal */
overflow: hidden; /* resize works for overflow other than visible */
}

.block-2 {
background-color: green;
flex: 1; /* adjust automatically */
}
<div id="app">
<div class="outer">
<div class="block block-1">
Block 1
</div>
<div class="block block-2">
Block 2
</div>
</div>
</div>


所以我们将使用 vanilla JS 而不是上面的 resize 解决方案:

  • 使用 mousedown 监听器注册 mousemove 监听器更新 block-1 宽度(和 < em>重置 mouseup 事件)
  • 还考虑 min-width: 0 override block-2min-width: auto元素

请看下面的演示:

let block = document.querySelector(".block-1"),
slider = document.querySelector(".slider");

slider.onmousedown = function dragMouseDown(e) {
let dragX = e.clientX;
document.onmousemove = function onMouseMove(e) {
block.style.width = block.offsetWidth + e.clientX - dragX + "px";
dragX = e.clientX;
}
// remove mouse-move listener on mouse-up
document.onmouseup = () => document.onmousemove = document.onmouseup = null;
}
.outer {
display: flex;
flex-direction: row;
}

.block {
height: 100px;
width: 50%; /* 50% would suffice*/
}

.block-1 {
background-color: red;
}

.block-2 {
background-color: green;
flex: 1; /* adjust automatically */
min-width: 0; /* allow flexing beyond auto width */
overflow: hidden; /* hide overflow on small width */
}

.slider {
line-height: 100%;
width: 10px;
background-color: #dee2e6;
border: none;
cursor: col-resize;
user-select: none; /* disable selection */
text-align: center;
}
<div id="app">
<div class="outer">
<div class="block block-1">
Block 1
</div>
<div class="slider">
S<br>l<br>i<br>d<br>e<br>r
</div>
<div class="block block-2">
Block 2
</div>
</div>
</div>


解决方案

您可以轻松地将上述内容适应 Vue,无需为此使用任何自定义 Vue 插件 - 更改是:

  • @mousedown slider 上的监听器,触发 slider

  • 使用refs更新 block-1

    的宽度

请看下面的演示:

new Vue({
el: '#app',
data: {
block1W: '50%'
},
methods: {
drag: function(e) {
let dragX = e.clientX;
let block = this.$refs.block1;
document.onmousemove = function onMouseMove(e) {
block.style.width = block.offsetWidth + e.clientX - dragX + "px";
dragX = e.clientX;
}
// remove mouse-move listener on mouse-up
document.onmouseup = () => document.onmousemove = document.onmouseup = null;
}
}
});
.outer {
display: flex;
flex-direction: row;
}

.block {
height: 100px;
width: 50%; /* 50% would suffice*/
}

.block-1 {
background-color: red;
}

.block-2 {
background-color: green;
flex: 1; /* adjust automatically */
min-width: 0; /* allow flexing beyond auto width */
overflow: hidden; /* hide overflow on small width */
}

.slider {
line-height: 100%;
width: 10px;
background-color: #dee2e6;
border: none;
cursor: col-resize;
user-select: none; /* disable selection */
text-align: center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<div class="outer">
<div class="block block-1" ref="block1" :style="{'width': block1W}">
Block 1
</div>
<div class="slider" @mousedown="drag">
S<br>l<br>i<br>d<br>e<br>r
</div>
<div class="block block-2">
Block 2
</div>
</div>
</div>

关于javascript - 使用 slider 更改两个 block 的宽度比例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55559527/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com