Bootstrap:更优雅的方式在滚动上移动div

时间:2015-02-19 06:07:04

标签: javascript jquery html css twitter-bootstrap

当我向下滚动浏览器时,我正试图让div保持在右上角。在桌面上查看它看起来不错。但是当在平板电脑上观看时,这是一团糟。是否有一种更优雅的方式来定位div,使用户在所有屏幕中向下滚动时看到。 我正在使用Bootstrap。这是DEMO  这是HTML引导代码:

<!DOCTYPE html>
<html lang="en" ng-app="ShopCartApp">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="../../favicon.ico">

    <title>Starter Template for Bootstrap</title>

    <!-- Bootstrap core CSS -->
   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
   <style type="text/css">
    body {
  padding-top: 50px;
}
.starter-template {
  padding: 40px 15px;
  text-align: center;
}
#divTotal{
  background-color: green;

}
   </style>


  </head>

  <body>



    <div class="container">

      <div class="row">
        <div class="col-md-8">
            <div class="panel panel-primary">
                <div class="panel-heading">
                    <h3 class="panel-title">Panel title</h3>
                </div>
                <div class="panel-body">
                    <div class="radio">
                        <label><input type="radio" name="optradio">Option 1</label>
                    </div>
                     <div class="radio">
                        <label><input type="radio" name="optradio">Option 1</label>
                    </div>
                    <div class="radio">
                        <label><input type="radio" name="optradio">Option 1</label>
                    </div>
                </div>
            </div>

            <div class="panel panel-primary">
              <div class="panel-heading">
                  <h3 class="panel-title">Panel title</h3>
              </div>
              <div class="panel-body">
                    <div class="radio">
                        <label><input type="radio" name="optradio1">Option 1</label>
                    </div>
                     <div class="radio">
                        <label><input type="radio" name="optradio1">Option 1</label>
                    </div>
                    <div class="radio">
                        <label><input type="radio" name="optradio1">Option 1</label>
                    </div>  
              </div>
            </div>

            <div class="panel panel-primary">
              <div class="panel-heading">
                  <h3 class="panel-title">Panel title</h3>
              </div>
              <div class="panel-body">
                    <div class="radio">
                        <label><input type="radio" name="optradio2">Option 1</label>
                    </div>
                     <div class="radio">
                        <label><input type="radio" name="optradio2">Option 1</label>
                    </div>
                    <div class="radio">
                        <label><input type="radio" name="optradio2">Option 1</label>
                    </div>
              </div>
            </div>

            <div class="panel panel-primary">
              <div class="panel-heading">
                  <h3 class="panel-title">Panel title</h3>
              </div>
              <div class="panel-body">
                    <div class="radio">
                        <label><input type="radio" name="optradio3">Option 1</label>
                    </div>
                     <div class="radio">
                        <label><input type="radio" name="optradio3">Option 1</label>
                    </div>
                    <div class="radio">
                        <label><input type="radio" name="optradio3">Option 1</label>
                    </div>
              </div>
            </div>
        </div>

        <div style="position:fixed;top:20px;float:right;right:0px;" class="col-md-4">
            <div class="panel panel-primary">
              <div id="divTotal" class="panel-heading">
                  <h3 class="panel-title">Total</h3>
              </div>
              <div class="panel-body">
                  <h2>Total</h2>
              </div>
          </div>
        </div>
      </div><!-- /.row  -->


    </div><!-- /.container -->


   <script type="text/javascript" src="angular.js"></script>
  </body>
</html>

2 个答案:

答案 0 :(得分:0)

<div class="container">

  <div class="row">
    <div style="position:fixed;top:20px;float:left;left:0px;"class="col-sm-8">

答案 1 :(得分:0)

据我所知,没有CSS解决方案可以为旧移动浏览器实现position: fixed容器。您可以编写一个javascript来调整容器在每个滚动事件上的位置,但这在iOS上不能很好地工作,因为iOS仅在滚动完成后才会触发onscroll事件,而不是在滚动时触发。这意味着容器在滚动后跳转到固定位置而不是停留在那里。

然而,有一些高级JS库使用虚假滚动容器模拟滚动行为,并绕过所描述的问题,例如: http://cubiq.org/iscroll-5