带有Affix& amp;的Bootstrap右侧边栏Offcanvas

时间:2016-07-19 00:05:59

标签: twitter-bootstrap affix off-canvas-menu

你好ive似乎在几年内一直在努力让右侧边栏卡在较大的显示器上然后在较小的显示器上进行扫描。我几乎在我的测试网站上,但我担心我无法链接到它,因为SE不喜欢链接到网站,但我有太多的代码和我努力做一个jsfiddle与它。我试过了。

假设我可以采取人们可以提供的任何指针。那也是值得赞赏的。这是我的网站的链接(非seo,因为我不使用该网站)可能有助于帮助我在哪里出错。谢谢。 http://kompressaur.com/index.html

一切正常,直到我到达移动断点。然后出现的切换导航按钮不想做任何事情。当我的灵魂被摧毁时,一切都有所帮助。我花了差不多7年的时间来实现这一目标,看起来还是很差劲。

感谢

:(

1 个答案:

答案 0 :(得分:0)

所以我认为你最好的选择是实际使用css位置粘贴以及stickyfill polyfill,因为你可以非常简单地使用css为移动设备定制这个,而且你也不必担心你的标题大小它也会停止你的页脚。

您可以在此处阅读有关css位置的信息https://www.sitepoint.com/css-position-sticky-introduction-polyfills/

所以我有一点时间在我的手上,并认为我会把一些东西放在一起。我想我会创建整个页面,以便您可以复制并粘贴它以使其正常工作。我只是链接到用于bootstrap,jquery和stickyfill polyfill的cdn,但你可以转到url并复制它们,这样你就可以在本地服务器上运行它们。

这里应该看起来与您的页面完全相同,但有一些差异会复制并粘贴整个页面,看看它是如何工作的,然后您可以根据自己的喜好进行自定义:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
<style>
body{padding-top:50px; overflow-x:hidden;}

#masthead{
  min-height:200px;
  padding:40px 0;
}
footer{
  height:1000px;
  background:#efefef;
  margin-top:40px;
}



@media screen and (min-width:768px){
  #sidebar-wrapper{
    position: -webkit-sticky;
    position: sticky;
    top: 70px;
    /*the following gives a smoother scrooll in webkit broswers*/
    -webkit-backface-visibility:hidden;
    backface-visibility:hidden; 
    -webkit-transform: translateZ(0);
    transform: translateZ(0);
  }
  .sidebar-toggle{display:none; }
}
@media screen and (max-width:768px){
  #sidebar{
    position:fixed;
    top:20px;bottom:20px;right:-90%;left:auto;
    width:90%;
    background:#fff;
    padding:20px;
    -ms-transition: all 500ms ease-in-out;
    -webkit-transition: all 500ms ease-in-out;
    -moz-transition: all 500ms ease-in-out;
    -o-transition: all 500ms ease-in-out;
    transition: all 500ms ease-in-out;
    z-index:99999;
    /*the following gives a smoother scrooll in webkit broswers*/
    -webkit-backface-visibility:hidden;
    backface-visibility:hidden; 
    -webkit-transform: translateZ(0);
    transform: translateZ(0);
  }
  .sidebar-open #sidebar{
    right:0;
  }
  .sidebar-open .sidebar-overlay{
    position:fixed;
    top:0;left:0;
    width:100%;
    height:100%;
    background:rgba(0,0,0,0.7);
    z-index:9999;
  }
  #sidebar .sidebar-toggle{
    width:100%;
    height:50px;
    border:none;
    background:#efefef;
    outline:0;
    font-weight:bold;
  }
}
</style>
</head>
<body>

<nav class="navbar navbar-default navbar-fixed-top" role="banner">
  <div class="container">
    <div class="navbar-header">
      <button class="navbar-toggle" type="button" data-toggle="collapse" data-target=".navbar-collapse">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a href="/" class="navbar-brand">Bootply</a>
    </div>
    <nav class="collapse navbar-collapse" role="navigation">
      <ul class="nav pull-left">
        <button type="button" class="btn btn-primary navbar-btn" data-toggle="modal" data-target="#mylinks">Links</button>      
        <button type="button" class="btn btn-danger navbar-btn"  data-toggle="modal" data-target="#myModal">Open Small</button>  
        <button type="button" class="btn btn-primary btn-md navbar-btn" data-toggle="modal" data-target="#myModal">Open Small</button>       
        <button type="button" class="btn btn-danger btn-md navbar-btn" data-toggle="modal" data-target="#myModal">Open Small</button>
      </ul>
      <ul class="nav navbar-nav pull-right">
        <li>
          <a href="#sec">Get Started</a>
        </li>
        <li>
          <a href="#sec">Edit</a>
        </li>
        <li>
          <a href="#sec">Visualize</a>
        </li>
        <li>
          <a href="#sec">Prototype</a>
        </li>
      </ul>
    </nav>
  </div>
</nav>


<div id="masthead">  
  <div class="container">
    <div class="row">
      <div class="col-md-7">
        <h1>Bootstrap Sidebar
          <p class="lead">With Css Sticky and Off Canvas</p>
        </h1>
      </div>
      <div class="col-md-5">
        <div class="well well-lg"> 
          <div class="row">
            <div class="col-sm-6">
              <img src="http://placehold.it/180x100" class="img-responsive">
            </div>
            <div class="col-sm-6">
              nothing here
            </div>
          </div>
        </div>
      </div>
    </div> 
  </div><!--/container-->
</div><!--/masthead-->


<div class="container">
  <div class="row">
    <div class="col-sm-9">
      <button type="button" class="btn btn-primary sidebar-toggle">Open Sidebar</button>

      <h2 id="sec0">Content</h2>
        <p>
          At Bootply we like to build simple Bootstrap templates that utilize the code Bootstap CSS without a lot of customization. Sure you can 
          find a lot of Bootstrap themes and inspiration, but these templates tend to be heavy on customization.</p>

        <hr>
        <p>
          Rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae 
          dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia cor magni dolores 
          eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, 
          sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. 
          Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut.              
          Rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae 
          dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia cor magni dolores 
          eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, 
          sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. 
          Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut!</p>

        <h2 id="sec1">Content</h2>
        <p>
          Rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae 
          dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut.
        </p>
        <div class="row">
          <div class="col-md-6">
            <div class="panel panel-default">
              <div class="panel-heading"><h3>Hello.</h3></div>
              <div class="panel-body">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis pharetra varius quam sit amet vulputate. 
                Quisque mauris augue, molestie tincidunt condimentum vitae, gravida a libero. Aenean sit amet felis 
                dolor, in sagittis nisi. Sed ac orci quis tortor imperdiet venenatis. Duis elementum auctor accumsan. 
                Aliquam in felis sit amet augue.
              </div>
            </div>
          </div>
          <div class="col-md-6">
            <div class="panel panel-default">
              <div class="panel-heading"><h3>Hello.</h3></div>
              <div class="panel-body">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis pharetra varius quam sit amet vulputate. 
                Quisque mauris augue, molestie tincidunt condimentum vitae, gravida a libero. Aenean sit amet felis 
                dolor, in sagittis nisi. Sed ac orci quis tortor imperdiet venenatis. Duis elementum auctor accumsan. 
                Aliquam in felis sit amet augue.
              </div>
            </div>
          </div>  
        </div>

        <hr>

        <h2 id="sec2">Section 2</h2>
        <p>
          Rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae 
          dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia cor magni dolores 
          eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, 
          sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. 
          Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut!
        </p>
        <div class="row">
          <div class="col-md-4"><img src="http://placehold.it/300x300" class="img-responsive"></div>
          <div class="col-md-4"><img src="http://placehold.it/300x300" class="img-responsive"></div>
          <div class="col-md-4"><img src="http://placehold.it/300x300" class="img-responsive"></div>
        </div>

        <hr>

        <h2 id="sec3">Section 3</h2>
        <p>
          Images are responsive sed @mdo but sum are more fun peratis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, 
          totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae 
          dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia cor magni dolores 
          eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, 
          sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. 
          Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut.
        </p>
        <p>
          Fos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, 
          sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. 
          Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut.
        </p>

        <p>
          Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, 
          totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae 
          dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia cor magni dolores 
          eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, 
          sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. 
          Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut.
        </p>


        <h2 id="sec4">Section 4</h2>
        <p>
          Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, 
          totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae 
          dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia cor magni dolores 
          eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, 
          sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. 
          Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut.
        </p>

        <p>
          Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, 
          totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae 
          dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia cor magni dolores 
          eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, 
          sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. 
          Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut.
        </p>


    </div>

    <div class="sidebar-overlay"></div>
    <div class="col-sm-3" id="sidebar-wrapper">
      <ul class="nav nav-pills  nav-stacked" id="sidebar">
        <button class="sidebar-toggle">Close Sidebar</button>
        <li class="active"><a href="#sec0">Section 0</a></li>
        <li><a href="#sec1">Section 1</a></li>
        <li><a href="#sec2">Section 2</a></li>
        <li><a href="#sec3">Section 3</a></li>
        <li><a href="#sec4">Section 4</a></li>
      </ul>
    </div><!--/left-->



  </div>
</div>

<footer></footer>


<!-- JavaScripts -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/stickyfill/1.1.4/stickyfill.min.js"></script>
<script>
$(document).ready(function(){
  $('#sidebar-wrapper').Stickyfill();
});


$('body').scrollspy({ target: '#sidebar-wrapper', offset:70});
$('#sidebar a').click(function() {
  if($('body').hasClass("sidebar-open")){
    $('body').removeClass("sidebar-open");
  }
  if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') || location.hostname == this.hostname) {
    var target = $(this.hash);
    target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
    if (target.length) {
      $('html,body').animate({
        scrollTop: target.offset().top- 50
      }, 1000);
      return false;
    }
  }
});

$('.sidebar-toggle, .sidebar-overlay').click(function(){
  $('body').toggleClass("sidebar-open");
});
</script>
</body>
</html>

首先,我使用jquery来切换侧栏的开放性。这样,您可以在单击侧栏切换按钮时自定义侧边栏和叠加。然后在较大的屏幕上,您可以使用粘贴到侧边栏包装器的位置,这将使您在滚动时跟随它。然后polyfill将处理不支持css位置粘滞的浏览器。

您可以在https://github.com/wilddeer/stickyfill

找到并阅读有关填充的信息