简体   繁体   English

在鼠标 position 上打开模态

[英]Open modal on mouse position

 let modal = document.querySelector('.modal'); let items = document.querySelectorAll('.item'); items.forEach(fe); function fe(item, index){ item.addEventListener("click", function(){ item.querySelector('.modal').classList.toggle('show'); }); }
 .horizontal-scrollable{ display: flex; flex-direction: column; padding: 10px; background-color: #d2d2d2; overflow-x:auto; max-width: 450px; }.item { background-color: #a29f9b; margin: 5px; padding: 0 5px; cursor:pointer; position:relative; }.item2{ min-width:500px }.item3{ min-width:700px }.modal{ display:none; position: absolute; top: 100%; right: 0; background: #4977d0; z-index: 100; }.modal.show{ display:block; }
 <div class="horizontal-scrollable"> <div class="item item1"> <h1>Item 1</h1> <div class="modal"> <p>I am modal.</p> </div> </div> <div class="item item2"> <h1>Item 2</h1> <div class="modal"> <p>I am modal.</p> </div> </div> <div class="item item3"> <h1>Item 3</h1> <div class="modal"> <p>I am modal.</p> </div> </div> <div class="item item4"> <h1>Item 4</h1> <div class="modal"> <p>I am modal.</p> </div> </div> </div>

In the above code snippet, If you click on any item, then it will show a modal on the right side.在上面的代码片段中,如果您单击任何项目,那么它将在右侧显示一个modal As you can see, item 3 is larger in width.如您所见, item 3的宽度较大。 So if you click on item 3 then you have to scroll-right to view the modal.因此,如果您单击第item 3 ,则必须scroll-right才能查看模式。

So I want to show modal on mouse click position.所以我想在鼠标点击 position 时显示模态。 How can I do this?我怎样才能做到这一点?

You can change the style dynamically and get the position X of your controller, then add the style using JS code.您可以动态更改样式并获取 controller 的 position X,然后使用 JS 代码添加样式。
Also, remove the right 0 from your modal class此外,从您的模态 class 中删除右 0

 let modal = document.querySelector('.modal'); let items = document.querySelectorAll('.item'); items.forEach(fe); function fe(item, index){ item.addEventListener("click", function(e){ item.querySelector('.modal').style.left = e.clientX + "px"; item.querySelector('.modal').classList.toggle('show'); }); }
 .horizontal-scrollable{ display: flex; flex-direction: column; padding: 10px; background-color: #d2d2d2; overflow-x:auto; max-width: 450px; }.item { background-color: #a29f9b; margin: 5px; padding: 0 5px; cursor:pointer; position:relative; }.item2{ min-width:500px }.item3{ min-width:700px }.modal{ display:none; position: absolute; top: 100%; background: #4977d0; z-index: 100; }.modal.show{ display:block; }
 <div class="horizontal-scrollable"> <div class="item item1"> <h1>Item 1</h1> <div class="modal"> <p>I am modal.</p> </div> </div> <div class="item item2"> <h1>Item 2</h1> <div class="modal"> <p>I am modal.</p> </div> </div> <div class="item item3"> <h1>Item 3</h1> <div class="modal"> <p>I am modal.</p> </div> </div> <div class="item item4"> <h1>Item 4</h1> <div class="modal"> <p>I am modal.</p> </div> </div> </div>

There are at least two options至少有两种选择

  • The first one will spawn the modal directly at the mouse position using JS第一个将使用 JS 直接在鼠标 position 处生成模态
  • And the second one will spawn the modal on the left side.第二个将在左侧生成模态。 This can be achieved using CSS这可以使用 CSS 来实现

First option第一个选项

So basically you can get the mouse position and set the modal position to it.所以基本上你可以得到鼠标 position 并将模态 position 设置为它。 In detail remove the top/left/right/bottom properties from the modal class in CSS (they aren't needed) and add some lines of JS.从 CSS 中的模态 class 详细删除顶部/左/右/底部属性(不需要它们)并添加一些 JS 行。 Please write me in the comments if you want to align the modal a bit around the mouse cursor... It's just about a few numbers.如果您想在鼠标 cursor 周围稍微对齐模态,请在评论中写信给我......这只是几个数字。

 let modal = document.querySelector('.modal'); let items = document.querySelectorAll('.item'); items.forEach(fe); var mousePos = {}; function fe(item, index){ item.addEventListener("click", function(e){ var rect = e.target.getBoundingClientRect(); // get some poition, scale,... properties of the item mousePos.x = e.clientX - rect.left; // get the mouse position relative to the element mousePos.y = e.clientY - rect.top; item.querySelector('.modal').classList.toggle('show'); item.querySelector('.modal').style.left = mousePos.x + "px"; // set the modal position to the last stored position item.querySelector('.modal').style.top = mousePos.y + "px"; }); }
 .horizontal-scrollable{ display: flex; flex-direction: column; padding: 10px; background-color: #d2d2d2; overflow-x:auto; max-width: 450px; }.item { background-color: #a29f9b; margin: 5px; padding: 0 5px; cursor:pointer; position: relative; }.item2{ min-width:500px }.item3{ min-width:700px }.modal{ display:none; position: absolute; background: #4977d0; z-index: 100; }.modal.show{ display:block; }
 <div class="horizontal-scrollable"> <div class="item item1"> <h1>Item 1</h1> <div class="modal"> <p>I am modal.</p> </div> </div> <div class="item item2"> <h1>Item 2</h1> <div class="modal"> <p>I am modal.</p> </div> </div> <div class="item item3"> <h1>Item 3</h1> <div class="modal"> <p>I am modal.</p> </div> </div> <div class="item item4"> <h1>Item 4</h1> <div class="modal"> <p>I am modal.</p> </div> </div> </div>

Second option第二种选择

The second option uses basic CSS to align the modals on the left side - it is much easier to implement and you just have to remove thee left/right/top/bottom property from the modal class and add left:0 :第二个选项使用基本的 CSS 来对齐左侧的模态 - 它更容易实现,您只需从模态 class 中删除 left/right/top/bottom 属性并添加left:0

 let modal = document.querySelector('.modal'); let items = document.querySelectorAll('.item'); items.forEach(fe); var mousePos = {}; function fe(item, index){ item.addEventListener("click", function(e){ item.querySelector('.modal').classList.toggle('show'); }); }
 .horizontal-scrollable{ display: flex; flex-direction: column; padding: 10px; background-color: #d2d2d2; overflow-x:auto; max-width: 450px; }.item { background-color: #a29f9b; margin: 5px; padding: 0 5px; cursor:pointer; position: relative; }.item2{ min-width:500px }.item3{ min-width:700px }.modal{ display:none; position: absolute; background: #4977d0; left: 0; z-index: 100; }.modal.show{ display:block; }
 <div class="horizontal-scrollable"> <div class="item item1"> <h1>Item 1</h1> <div class="modal"> <p>I am modal.</p> </div> </div> <div class="item item2"> <h1>Item 2</h1> <div class="modal"> <p>I am modal.</p> </div> </div> <div class="item item3"> <h1>Item 3</h1> <div class="modal"> <p>I am modal.</p> </div> </div> <div class="item item4"> <h1>Item 4</h1> <div class="modal"> <p>I am modal.</p> </div> </div> </div>

Hope I could help:D希望我能帮上忙:D

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

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