简体   繁体   English

如何在较小的 div 中获取可拖动元素的 position 并将其转换为大 div

[英]How to get the position of a draggable element in a smaller div and translate that to a large div

What i'm basically creating is an electron program that loops a video containing adverts.我基本上创建的是一个 electron 程序,它循环包含广告的视频。 The video spans the entire window.该视频跨越了整个 window。

I have a second, smaller window used to configure things about the video window in real time and one of those things is being able to add a message overlayed on the video.我有第二个较小的 window 用于实时配置有关视频 window 的内容,其中之一是能够添加覆盖在视频上的消息。 Here's an image to show you what I mean: https://i.imgur.com/9SneVOT.png这是一张图片向您展示我的意思: https://i.imgur.com/9SneVOT.png

The blue rectangle on the smaller video in the foreground can be dragged around and represents a message that will be displayed over the video.可以拖动前景中较小视频上的蓝色矩形,表示将在视频上显示的消息。 If I save the coordinates of that rectangle and create an element on the main video in the background the coordinates don't match up due to the different sizes of the windows, so how do I take those coordinates and make them work for the bigger window?如果我保存该矩形的坐标并在背景的主视频上创建一个元素,由于 windows 的大小不同,坐标不匹配,那么我如何获取这些坐标并使它们适用于更大的 window ?

Each pixel in the smaller div equals X pixels in the bigger div.较小 div 中的每个像素等于较大 div 中的 X 个像素。 That's the ratio to multiply with.这就是乘以的比率。 Let's assume mouse movement inside smaller div is like dragging and affecting the bigger div.让我们假设鼠标在较小的 div 内移动就像拖动和影响较大的 div。

 var small = document.querySelector(".small"); var big = document.querySelector(".big"); var rectangle = document.querySelector(".rectangle"); var ratioX = big.clientWidth / small.clientWidth var ratioY = big.clientHeight / small.clientHeight small.addEventListener("mousemove", function(ev) { var mx = ev.offsetX var my = ev.offsetY rectangle.style.left = mx * ratioX + "px" rectangle.style.top = my * ratioY + "px" })
 .small { width: 100px; height: 50px; border: 1px solid black; }.big { width: 300px; height: 150px; border: 1px solid black; position: relative; overflow: hidden; }.rectangle { width: 20px; height: 20px; background: blue; position: absolute; }
 Mouse over this: <div class="small"> </div> Will move this: <div class="big"> <div class="rectangle"> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM