【发布时间】:2015-11-22 05:04:18
【问题描述】:
对不起,标题很血腥,不知道如何更简洁。
我想做的事:
我有一个模式,我想从多个按钮(动态创建)中调用它并定位在按钮附近。使用 jQuery,我得到了单击按钮的 .offset(),然后将其分配给模式。
引导: http://www.bootply.com/lEUtR2IkeU
JS:
$('.model-open-btn').click(function(){
$('#pageJumpModal').show().height( '1px' ); //#pageJumpModal is the container that's hidden, .offset has problems with hidden elements, so show it first.
var offset = $(this).offset(); //Get the button's offset
$('#pageJumpModal > .modal-dialog').offset({top: offset.top, left: offset.left}); //Set the modal's position.
$('#pageJumpModal').height( 'auto' ).hide(); //Reset the container's height.
});
问题:
第一次单击任何一个按钮时,它都可以正常工作(好吧,我宁愿它显示在上面,但现在已经足够了),但是第二次显示模态时,它的顶部位置增加了 13px。
我确定有些事情我没有考虑,但我迷路了。我尝试了 .position(),只是获取/设置 css 顶部和左侧属性,以及许多其他的东西。是时候转向比我更聪明的头脑了。
【问题讨论】:
标签: jquery twitter-bootstrap-3 modal-dialog css-position offset