动态更改弹出窗口的位置

时间:2018-04-11 11:57:34

标签: javascript html css

我有div buttons,当您点击文章时会显示div。但是我很难将这个弹出窗口限制在我的文章 $('.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' }); } }); 中,因为如果你点击文章的边缘当前 - >弹出窗口按钮无法正确显示。

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

a

1 个答案:

答案 0 :(得分:1)

您需要将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

原来的小提琴没有用,因为你忘了添加jQuery

修改:也适用于高度。

相关问题