gpt4 book ai didi

javascript - 绝对定位元素阻止拖动事件

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

我正在尝试制作 slider ,但遇到了一个问题。我面临的问题是绝对元素阻止 slider 拖动事件。我需要以某种方式允许拖动底层图像以通过绝对定位的元素拖动。你知道如何实现吗?提前致谢!Draggable="true"不是解决方案,因为那时该元素变得可拖动(并且可以看到幽灵拖动图像)。这是我的代码:Codepen 如果你喜欢:https://codepen.io/saksija/pen/MWgqNeJ

HTML:

<div id="app">
<div class="slider" ref="slider">
<div class="slider-wrapper" ref="sliderContainer">
<img ref="slide" src="https://image.shutterstock.com/image-photo/really-wide-panoramic-sunrise-shot-260nw-35170.jpg">
<div v-for="id in 2" :class="['subitem', 'o-'+id]"></div>
</div>
</div>
</div>

Vue/Javascript:

var app = new Vue({
el: '#app',
data: () => ({
startX: 0,
lastTranslate: 0,
translate: 0
}),
watch: {
translate(value) {
this.$refs.sliderContainer.style.transform = `translate3d(${value}px, 0px, 0px)`;
}
},
methods: {
dragStart(event) {
this.startX = event.screenX;
},
dragEnd(event) {
this.translate = this.lastTranslate;
},
dragMove(event) {
if (event.screenX === 0) return;

let distance = event.screenX - this.startX;
let newTranslate = this.translate + distance;
let sliderContainer = this.$refs.sliderContainer;

this.lastTranslate = newTranslate;
sliderContainer.style.transform = `translate3d(${newTranslate}px, 0px, 0px)`;
},
},
mounted() {
let slider = this.$refs.slider;
slider.addEventListener('dragstart', this.dragStart);
slider.addEventListener('drag', this.dragMove);
slider.addEventListener('dragend', this.dragEnd);
}
})

CSS:

#app {
position: relative;
width: 100vw;
height: 100vh;
overflow: hidden;
}
.slider {
position: relative;
display: flex;
width: 100%;
height: 100%;
}
.slider-wrapper {
position: relative;
display: block;
height: 100%;
}
.slider img {
position: relative;
display: block;
height: 100%;
}
.subitem {
position: absolute;
top: 20%;
bottom: 20%;
width: 100px;
background-color: red;
user-select: none;
}
.subitem.o-1 {
left: 3%;
}
.subitem.o-2 {
left: 20%;
}

最佳答案

假设 .subitem 不需要与之交互,您可以对它们使用 pointer-events:none;。例如:

.subitem {
position: absolute;
top: 20%;
bottom: 20%;
width: 100px;
background-color: red;
user-select: none;
pointer-events:none;
}

关于javascript - 绝对定位元素阻止拖动事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58032706/

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