javascript - 动态改变弹出窗口的位置

标签 javascript html css

我有一个 divbuttons 当你点击一篇文章时它会出现。但是我很难在我的文章 div 中限制这个弹出窗口,因为目前如果你点击文章的边缘 -> 弹出按钮没有像你想象的那样正确显示。

JSfiddle 链接:https://jsfiddle.net/kpfax14n/35/

  $('.tweets_pane').mouseup(function(event) {
    console.log("Mouse up");
    if (!getRightClick(event) ){
      console.log("Show buttons");
      $('.entity_types').css({
        display: 'block',
        position: 'absolute',
        left: (event.pageX+10) + 'px',
        top: (event.pageY+15) + 'px'
      });
    }
  });

最佳答案

您需要将 window.width 与您的 div 宽度进行比较

var left_value = event.pageX+10,
    window_width = $(window).width();
console.log('left_value = ' +left_value + ' | '+ 'window_width = '+ window_width);
    if ((left_value + 290) > window_width){ // 280 = 250px (width) + 6px (borders) + 24px (padding) + 10 (for extreme)
    left_value = window_width - 320;
  }
  $('.entity_types').css({
    display: 'block',
    position: 'absolute',
    left: left_value + 'px',
    top: (event.pageY+15) + 'px'
  }); 

Updated Fiddle V2

原来的 fiddle 不工作是因为你忘了添加 jQuery

编辑:高度也是如此。

关于javascript - 动态改变弹出窗口的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49774523/

相关文章:

javascript - Jquery 加载不适用于加载的内容

html - 为什么当浏览器窗口重新调整大小时 div 宽度没有改变?

java - 将 Html.fromHtml 与 android 8 十六进制数字颜色资源一起使用

javascript - 在 .load() 内容上运行 JS 函数

javascript:获取关联数组中包含重复数据的键

javascript - 在 AngularJS 的另一个 Controller 中更新变量

javascript - 浏览器中出现错误,提示找不到变量?

html - @media 查询和图像交换

html - 将 flexbox 元素与 flex-grow 元素对齐

html - 在其他 HTML 标签中使用 "link_to"