0

您好,我似乎多年来一直在努力让右侧边栏既被卡在较大的显示器上,又在较小的显示器上脱离画布。我几乎在我的测试站点上,但我担心我无法链接到它,因为 SE 不喜欢指向网站的链接,但我有太多的代码,我很难用它来制作一个 jsfiddle。我努力了。

假设我可以接受人们可以提供的任何指示。那也将不胜感激。这是指向我的网站的链接(非 seo,因为我不使用该网站)也许可以帮助我解决我出错的地方。谢谢。 http://kompressaur.com/index.html

一切正常,直到我到达移动断点。然后出现的切换导航按钮不想做任何事情。由于我的灵魂已被摧毁,所有的帮助都值得赞赏。我花了将近 7 年的时间才走到这一步,看起来……它仍然很差。

谢谢

:(

4

1 回答 1

0

所以我认为你最好的选择是实际使用css position sticky和stickyfill polyfill,因为你可以非常简单地使用css为移动设备定制它,而且你不需要担心你的标题大小,它会也停下来看看你的页脚。

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

所以我手头有一点时间,我想我会整理一些东西。我想我会制作整个页面,以便您可以复制并粘贴它以查看它的工作。我刚刚链接到引导程序、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 位置粘性的浏览器。

您可以在这里找到并阅读有关 polyfill 的信息https://github.com/wilddeer/stickyfill

于 2016-07-19T06:13:14.500 回答