JavaScript/HTML : How to make a small UI pop out on select

标签 javascript html user-interface google-chrome-extension

我正在制作一个 Chrome 扩展,当用户选择某些文本时,它会与当前页面进行交互。

我们想要它做的是弹出一个小窗口,让用户在不同的选项中进行选择。

类似的事情:

http://img-ipad.lisisoft.com/img/1/5/1526-1-pdf-highlighter.jpg

到目前为止我们所拥有的内容如下:

document.addEventListener('mouseup',boxOption)


function boxOption(){
   var yourSelection = window.getSelection();
   if (yourSelection!=""){
       /* insert popup here */
   }
}

任何帮助将不胜感激

最佳答案

您可以通过将一个元素插入到 DOM 中(或显示 DOM 中的现有元素)来实现弹出窗口,该元素仅具有一个“z-index”属性,将其置于其他元素之上。例如:

// Create a class that encapsulates the menu element
// This particular implementation constructs a new element
// and adds it to the DOM, but you could instead take the
// element as a parameter or have it retrieve an existing element
var PopupMenu = function() {
  this.element = document.createElement('div');
  this.element.className = 'popup-menu';
  document.body.appendChild(this.element);
  // ...
  // set up event listeners for this element
  // ...
};

// The menu is hidden unless it also has the 'enabled' class
PopupMenu.prototype.setVisible = function(isVisible) {
   if (isVisible) {
     this.element.classList.add('enabled');
   } else {
     this.elemnt.classlist.remove('enabled');
   }
};

然后在你的 CSS 中,你可以这样做:

.popup-menu {
  display: none;
}
.popup-menu.enabled {
  display: block;
  /* this just needs to be larger than the z-index of the items it covers */
  z-index: 100;
}

我会将菜单的其余样式/处理程序留给您。

关于JavaScript/HTML : How to make a small UI pop out on select,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26435725/

相关文章:

user-interface - 在 gui 模式下恢复暂停的 emacs session

javascript - 使用ng-repeat、ng-form、ng-submit后如何提交

html - 几乎相同的嵌套选择器的 CSS 规则

java - JButton 扩展以占据整个框架/容器

html - 我怎样才能有响应线 '-----' ?

java - 根据传递给 jsp 的变量更改 div 背景

user-interface - 用冰 rust 画img

javascript - JavaScript 中有多少个参数太多了?

Javascript,将对象列表转换为数组?

javascript - 如何在asp mvc应用程序中构建ajax-chartjs折线图?