Bootstrap 3 - 在某个div上定位自动加载模式

时间:2015-04-21 18:16:50

标签: javascript html css twitter-bootstrap twitter-bootstrap-3

我有一个模态,在3秒内出现在页面加载状态。

我想更改默认位置,将其置于我选择的某个位置附近(在下面的示例中,在id = zone1的范围内)

<div id="deal-zone">
  <div class="area">
    <span class="thezone" id="zone1" data-toggle="modal" data-target="#myModal"/></span>   
  </div>
</div>

<!-- Here is the modal -->
<div class="modal in" id="notificationModal" role="dialog" aria-labelledby="notificationModalLabel" aria-hidden="true" data-backdrop="false">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">

        <h3 class="modal-title" id="notificationModalLabel" style="color:#DF2943;">
          How to take part in the deal?
        </h3>
      </div>
      <div class="modal-body">   
        <h4 style="margin-top:0px">
        explanations
      </h4>

      </div>

    </div>
  </div>
</div>

<!-- Script that makes the modal autoload on page load -->     
<script type="text/javascript">
  $(window).load(function(){
    $('#notificationModal').modal('show');    
    $('#notificationModal').fadeTo(3000, 500).slideUp(500, function(){
      $(this).remove(); 
    });

  });
</script>

我怎样才能做到这一点?

这是一个JSFIDDLE来帮助:http://jsfiddle.net/DTcHh/6941/:我想把模态放在“HERE”这个词的上方,并且仍然可以阅读这里的单词(就像一个指向“HERE”这个词的工具提示“)

3 个答案:

答案 0 :(得分:1)

这都是关于CSS定位的。这是一个如何实现它的例子。

CSS:

.testbox {
    background: #000000;
    color: #FFFFFF;
    padding: 10px;
    min-height: 200px;
}
.testbox .modal{ 
    color: #333;
}

/* overwriting default behaviour of Modal Popup in Bootstrap  */
body{ 
    overflow: auto !important;
}
.modal{ 
    overflow: hidden; 
}

/* created new class for targetting purpose - named ".modal2", ".modal1" */
.modal2.in{ 
    position: absolute; 
    bottom: 0; 
    right:0; 
    left: auto; 
    top: auto; 
    bottom: 0; 
    overflow: auto;
}

HTML:

<div class="testbox col-lg-6"> 
  <h3><strong>Test Box</strong></h3>

    <blockquote>
      Modal Popup should <br />open inside (or aligned to) <br />this balck DIV area.
    </blockquote>

    <!-- .modal1 -->
    <div class="modal fade bs-modal-sm modal1" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-sm">
            <div class="modal-content"> 
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    <h4 class="modal-title" id="myModalLabel">Default Modal Popup</h4>
                  </div>

                  <div class="modal-body">
                    Modal 1 Popup Content
                  </div>
            </div>
        </div>
    </div>

    <!-- .modal2 -->
    <div class="modal bs-modal-sm col-sm-12 col-xs-12 modal2" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-sm">
            <div class="modal-content"> 
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    <h4 class="modal-title" id="myModalLabel">Bound to <strong>Test Box</strong> DIV</h4>
                  </div>

                  <div class="modal-body">
                    Modal 2 Popup Content 
                  </div>
            </div>
        </div>
    </div>
</div>

答案 1 :(得分:1)

您可以使用.offset()获取您想要定位的元素的位置,然后使用一些数学运算来根据元素的位置更改模态的margin-top在视口中。

<强> BOOTPLY

<强>的jQuery

var location = $('#zone1').offset();

    $('#notificationModal').css("margin-top", location.top - 100);
    $('#notificationModal').modal('show');    
    $('#notificationModal').fadeTo(3000, 500).slideUp(500, function(){
      $(this).remove(); 
    });

<强> HTML

<div class="container">
    <div class="navbar navbar-default">
        <div class="navbar-header">
            <a class="navbar-brand" href="#">Bootstrap 3</a>
        </div>


    <div class="jumbotron">
        <h1>Twitter Bootstrap 3.0</h1>
        <p class="lead">Starter template with CSS and JS included.</p>
        <p><a class="btn btn-lg btn-primary" href="http://yahoo.fr" target="_blank">link</a></p>
      </div>
        <div class="area">
          <span class="thezone" id="zone1" data-toggle="modal" data-target="#myModal">HERE</span> i want to put the modal just above this word and still be able to see the word "HERE"  
               </div>
          <div class="area2">
          <span class="thezone2" id="zone2" data-toggle="modal" data-target="#myModal"></span>   
               </div>

    <div class="modal in" id="notificationModal" role="dialog" aria-labelledby="notificationModalLabel" aria-hidden="true" data-backdrop="false">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">

        <h3 class="modal-title" id="notificationModalLabel" style="color:#DF2943;">
          How to take part in the deal?
           </h3>
      </div>
      <div class="modal-body">   
        <h4 style="margin-top:0px">
        some explanations
      </h4>

      </div>

    </div>
  </div>
</div>
</div></div>


            <div id="push"></div>

<强> CSS

body {
    margin: 10px;
}

答案 2 :(得分:0)

最简单的解决方案就是将它放在CSS中。 这不是很好,但让我们离开这里。也许我们可以用javascript或其他东西使它变得更好/可变。

.modal-dialog {
    position: absolute;
    top: 160px;
}

http://jsfiddle.net/DTcHh/6943/