6

我正在尝试将 a<div>放置在用户文本选择上方,该文本选择将充当类似于 Mediums 的工具栏。

在此处输入图像描述

虽然我已经成功<div>地将 定位在选择旁边,但我似乎无法让它相对于选择正确居中:

    $(function() {
      // Setup the Event Listener
      $('.article').on('mouseup', function() {
        // Selection Related Variables
        let selection = window.getSelection(),
        getRange      = selection.getRangeAt(0),
        selectionRect = getRange.getBoundingClientRect();

        // Set the Toolbar Position
        $('.toolbar').css({
          top: selectionRect.top - 42 + 'px',
          left: selectionRect.left + 'px'
        });
      });
    });

我可以通过从视口中减去选择的左偏移量来确定选择的中心点,如下所示:

 selectionRect.left - selectionRect.width

但是,我不确定如何使用它来将工具栏的位置设置为相对于选择矩形居中?

我尝试从选择的宽度除以 2 中减去工具栏的左偏移量,但这也不能完全与中心对齐。

JSFiddle

https://jsfiddle.net/e64jLd0o/

4

2 回答 2

8

一种解决方案是将以下内容添加到您的 CSS 中:

.toolbar {
  transform: translateX(-50%);
}

并更新您的脚本以偏移工具栏元素的左侧位置,如下所示:

$('.toolbar').css({
  top: selectionRect.top - 42 + 'px',
  left: ( selectionRect.left + (selectionRect.width * 0.5)) + 'px'
});

这是一个工作片段:

$(function() {
  // Setup the Event Listener
  $('.article').on('mouseup', function() {
    // Selection Related Variables
    let selection = window.getSelection(),
    getRange      = selection.getRangeAt(0),
    selectionRect = getRange.getBoundingClientRect();
     

    // Set the Toolbar Position
    $('.toolbar').css({
      top: selectionRect.top - 42 + 'px',
      left: ( selectionRect.left + (selectionRect.width * 0.5)) + 'px'
    });
  });
});
.article {
  position: relative;
  height: 300px;
  padding: 20px;
}

.toolbar {
  position: absolute;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 169px;
  padding-top: 10px;
  padding-bottom: 10px;
  background: black;
  text-align: center;
  color: white;
  border-radius: 8px;
  
  transform: translateX(-50%);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<!-- Editor -->
<div class="article">
  <p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Tenetur dignissimos facilis id repellat sint deserunt voluptates animi eaque tempore debitis, perferendis repudiandae voluptatem. Eligendi fuga deleniti saepe quod eum voluptas.</p>
</div>

<!-- Toolbar -->
<div class="toolbar">Toolbar</div>

于 2019-04-27T01:36:52.890 回答
4

为了快速编辑,我应用了正确定位 div 所需的更改:

https://jsfiddle.net/yaLvzswu/

我在计算中应用了一些额外的定位数学left

$(function() {
  // Setup the Event Listener
  $('.article').on('mouseup', function() {
    // Selection Related Variables
    let selection = window.getSelection(),
    getRange      = selection.getRangeAt(0),
    selectionRect = getRange.getBoundingClientRect();
        console.log(selectionRect)
    // Set the Toolbar Position
    $('.toolbar').css({
      top: selectionRect.top - 42 + 'px',
      left: (selectionRect.left - ($('.toolbar').width()/2) + (selectionRect.width/2))+ 'px'
    });
  });
});

您的版本将工具栏的左侧应用于所选文本的左侧。由于我们想将工具栏的中心放在选区的中心,所以左边被洗牌两次

  • 从左开始
  • 向左移动工具栏宽度的一半

现在工具栏中心位于选择的左侧。

  • 添加一半的选择宽度

工具栏向右移动一点 - 所选文本的中心。

于 2019-04-27T01:37:53.717 回答