使用jQuery获取Boostrap模态位置

时间:2018-01-12 01:42:53

标签: jquery twitter-bootstrap

在我的代码中,如果你单击模态中的按钮我试图让它提醒模态的左/上位置,但它总是返回0。

我不确定为什么浏览器无法返回这些值。

$(window).on('load',function(){
  $('#myModal').modal('show');
});

function getPos() {
  alert("Top: "+ $("#myModal").position().top + " Left: " +$("#myModal").position().left);
}
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js">
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
    
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
<script src="//code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>


<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Modal</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" onclick="getPos()">Click Me</button>
      </div>
    </div>
  </div>
</div>

2 个答案:

答案 0 :(得分:1)

如果你检查适用的实时html和css规则,很容易理解为什么。

模态类创建叠加层并具有以下主要规则:

.modal {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 1050;
    display: none;
    overflow: hidden;
    outline: 0;
}

您想要的是孩子.modal-dialog

的偏移量

$(window).on('load',function(){
  $('#myModal').modal('show');
});

function getPos() {
  $dialog = $("#myModal .modal-dialog")
  alert("Top: "+ $dialog.offset().top + " Left: " +$dialog.offset().left);
}
    
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
<script src="//code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>


<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Modal</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" onclick="getPos()">Click Me</button>
      </div>
    </div>
  </div>
</div>

答案 1 :(得分:0)

你试过吗? 希望它可以帮到你

<强> HTML

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Modal</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" id="clickMe">Click Me</button>
      </div>
    </div>
  </div>
</div>

<强> JS

    $(window).on('load',function(){
      $('#myModal').modal('show');
    });

    $(document).ready(function(){
        $("#clickMe").click(function(e){
           //Offset mouse Position
           var OffsetXpos = e.pageX - $(this).offset().left,
               OffsetYpos = e.pageY - $(this).offset().top;
          //Relative ( to its parent) mouse position 
          var RelativePosX = $(this).position().left,
              RelativePosY = $(this).position().top;
           alert("  Offset_X is: " + OffsetXpos + 
                 ", Offset_Y is: " + OffsetYpos+'\r\n'+
                 "  Relative_X is: " +RelativePosX+
                 ", Relative_Y is: " +RelativePosY+'\r\n'+
                 "  Default_X is: " +e.pageX+
                 ", Default_Y is: " +e.pageY);
        });
});

或,请参阅此处demo