2016-03-26 204 views
0

我試圖創建一個單一的滾動網頁由添加按鈕

$('a.page-scroll').on('click', function(event) { 
 
    var $anchor = $(this); 
 
    $('html, body').stop().animate({ 
 
    scrollTop: $($anchor.attr('href')).offset().top - 60 
 
    }, 1000); 
 
    event.preventDefault(); 
 
});
* { 
 
    margin-top: 0; 
 
    margin-bottom: 0; 
 
} 
 
#page1 { 
 
    height: 1000px; 
 
    background-color: #000; 
 
    color: white; 
 
} 
 
#page2 { 
 
    height: 1000px; 
 
    color: white; 
 
    background-color: #18121e; 
 
    padding-top: 140px; 
 
    border-top: 100px; 
 
} 
 
#page3 { 
 
    height: 650px; 
 
    color: white; 
 
    background-color: #233237; 
 
    background-size: cover; 
 
    padding-top: 200px; 
 
} 
 
body { 
 
    padding-top: 0px; 
 
} 
 
#page1 p { 
 
    padding-top: 100px; 
 
    font-size: 2.5rem; 
 
} 
 
#page1 p #highlight { 
 
    color: pink !important; 
 
} 
 
#page2 h3 { 
 
    padding-bottom: 80px; 
 
    line-height: 1.5; 
 
} 
 
#page3 h3 { 
 
    padding-bottom: 100px; 
 
    line-height: 1.5; 
 
} 
 
.btn { 
 
    color: white; 
 
    background-color: #000 !important; 
 
} 
 
.btn:hover { 
 
    color: gray; 
 
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.nicescroll/3.6.7/jquery.nicescroll.min.js"></script> 
 
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" /> 
 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<nav class="navbar navbar-default navbar-fixed-top"> 
 
    <div class="container"> 
 
    <!-- Brand and toggle get grouped for better mobile display --> 
 
    <div class="navbar-header"> 
 
     <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false"> 
 
     <span class="sr-only">Toggle navigation</span> 
 
     <span class="icon-bar"></span> 
 
     <span class="icon-bar"></span> 
 
     <span class="icon-bar"></span> 
 
     </button> 
 
     <a class="navbar-brand" href="#">Rashid Naushad</a> 
 
    </div> 
 

 
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> 
 
     <ul class="nav navbar-nav navbar-right"> 
 
     <li><a class="page-scroll" href="#about">About</a> 
 
     </li> 
 
     <li><a class="page-scroll" href="#portfolio">Portfolio</a> 
 
     </li> 
 
     <li><a class="page-scroll" href="#contact">Contact</a> 
 
     </li> 
 
     </ul> 
 
    </div> 
 
    <!-- /.navbar-collapse --> 
 
    </div> 
 
    <!-- /.container-fluid --> 
 
</nav> 
 
<!--<div class="container-fluid">--> 
 
<div> 
 
    <div id="page1"> 
 
    <a id="about"></a> 
 
    <br> 
 
    <br> 
 
    <br> 
 
    <p class="text-center">Hi ! 
 
     <br>I'm a self taught web developer based in India. 
 
     <br>I'm an undergraduate student pursuing economics who just loves to <span id="highlight">code</span>. 
 
     <br>I dream of combining my knowledge of programming, economics, and 
 
     <br>financial markets for the betterment of the society. 
 
     <br>Languages and Frameworks: 
 
     <br>HTML5, CSS3, Bootstrap3, 
 
     <br>Javascript, jQuery, Angular.js, Meteor.js, 
 
     <br>Node.js, React,js, 
 
     <br>Python, php.. 
 
     <br>Tools & expertise: Git, Responsive Web Design.</p> 
 
    </div> 
 

 
    <div id="page2"> 
 
    <a id="portfolio"></a> 
 
    <h3 class="text-center">Here is my latest portfolio, <br>I'll be adding more as I learn and grow..</h3> 
 
    <!--<div class="row"> 
 
     <div class="thumbnail img img-responsive"> 
 
     <p data-height="268" data-theme-id="0" data-slug-hash="VaKMBJ" data-default-tab="result" data-user="rashidnaushad" data-preview="true" class="codepen">See the Pen <a href="http://codepen.io/rashidnaushad/pen/VaKMBJ/">Simple Javascript App</a> by Rashid Naushad (<a href="http://codepen.io/rashidnaushad">@rashidnaushad</a>) on <a href="http://codepen.io">CodePen</a>.</p> 
 
     <script async src="//assets.codepen.io/assets/embed/ei.js"></script> 
 
     </div> 
 

 
     <div class="thumbnail img img-responsive"> 
 
     <p data-height="268" data-theme-id="0" data-slug-hash="pyPjae" data-default-tab="result" data-user="rashidnaushad" data-preview="true" class="codepen">See the Pen <a href="http://codepen.io/rashidnaushad/pen/pyPjae/">Tribute to Raghuram Rajan</a> by Rashid Naushad (<a href="http://codepen.io/rashidnaushad">@rashidnaushad</a>) on <a href="http://codepen.io">CodePen</a>.</p> 
 
     <script async src="//assets.codepen.io/assets/embed/ei.js"></script> 
 
     </div> 
 
    </div> 
 
    <div class="row"> 
 
     <div class="thumbnail"> 
 
     <img class="img img-responsive img-rounded" src="http://cleantallahassee.com/wp-content/uploads/2015/09/coming-soon.jpg" alt="Coming Soon!" /> 
 
     <div class="caption"> 
 
      <h5 class="text-center">Coming Soon!</h5> 
 
     </div> 
 
     </div> 
 

 
     <div class="thumbnail"> 
 
     <img class="img img-responsive img-rounded" src="http://cleantallahassee.com/wp-content/uploads/2015/09/coming-soon.jpg" alt="Coming Soon!" /> 
 
     <div class="caption"> 
 
      <h5 class="text-center">Coming Soon!</h5> 
 
     </div> 
 
     </div> 
 
    </div>--> 
 
    </div> 
 

 
    <div id="page3"> 
 
    <a id="contact"></a> 
 
    <h3 id="contact-text" class="text-center">Think I can be of any help to you?<br>Wanna collaborate?<br>Shoot me a message, now!</h3> 
 
    <a href="https://github.com/rashidnaushad" class="btn btn-lg col-lg-2 col-lg-offset-2 col-md-2 col-md-offset-2 col-sm-8 col-sm-offset-2 col-xs-8 col-xs-offset-2" target="_blank">GitHub</a> 
 
    <div class="hidden-lg hidden-md"> 
 
     <br> 
 
    </div> 
 
    <a href="https://www.freecodecamp.com/rashidnaushad" class="btn btn-lg col-lg-2 col-lg-offset-1 col-md-2 col-md-offset-1 col-sm-8 col-sm-offset-2 col-xs-8 col-xs-offset-2" target="_blank">freeCodeCamp</a> 
 
    <div class="hidden-lg hidden-md"> 
 
     <br> 
 
    </div> 
 
    <a href="mailto:[email protected]" class="btn btn-lg col-lg-2 col-lg-offset-1 col-md-2 col-md-offset-1 col-sm-8 col-sm-offset-2 col-xs-8 col-xs-offset-2" target="_blank">Gmail</a> 
 
    </div> 
 
</div>

第三頁上的按鈕三頁,倒塌時不留空間之間隱蔽空間在之間。我如何在sm全部和xs商場屏幕尺寸的按鈕之間添加空格?我嘗試在按鈕之間插入
,但這不起作用。 這裏是JsFiddle

回答

3

添加到您的CSS

@media screen and (max-width: 990px) { 

    .btn ~ .btn { 
    margin-top: 10px; 
    } 

} 

Updated fiddle

示例代碼段

$('a.page-scroll').on('click', function(event) { 
 
    var $anchor = $(this); 
 
    $('html, body').stop().animate({ 
 
    scrollTop: $($anchor.attr('href')).offset().top - 60 
 
    }, 1000); 
 
    event.preventDefault(); 
 
});
* { 
 
    margin-top: 0; 
 
    margin-bottom: 0; 
 
} 
 
#page1 { 
 
    height: 1000px; 
 
    background-color: #000; 
 
    color: white; 
 
} 
 
#page2 { 
 
    height: 1000px; 
 
    color: white; 
 
    background-color: #18121e; 
 
    padding-top: 140px; 
 
    border-top: 100px; 
 
} 
 
#page3 { 
 
    height: 650px; 
 
    color: white; 
 
    background-color: #233237; 
 
    background-size: cover; 
 
    padding-top: 200px; 
 
} 
 
body { 
 
    padding-top: 0px; 
 
} 
 
#page1 p { 
 
    padding-top: 100px; 
 
    font-size: 2.5rem; 
 
} 
 
#page1 p #highlight { 
 
    color: pink !important; 
 
} 
 
#page2 h3 { 
 
    padding-bottom: 80px; 
 
    line-height: 1.5; 
 
} 
 
#page3 h3 { 
 
    padding-bottom: 100px; 
 
    line-height: 1.5; 
 
} 
 
.btn { 
 
    color: white; 
 
    background-color: #000 !important; 
 
} 
 
.btn:hover { 
 
    color: gray; 
 
} 
 

 
@media screen and (max-width: 990px) { 
 

 
    .btn ~ .btn { 
 
    margin-top: 10px; 
 
    } 
 

 
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.nicescroll/3.6.7/jquery.nicescroll.min.js"></script> 
 
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" /> 
 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<nav class="navbar navbar-default navbar-fixed-top"> 
 
    <div class="container"> 
 
    <!-- Brand and toggle get grouped for better mobile display --> 
 
    <div class="navbar-header"> 
 
     <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false"> 
 
     <span class="sr-only">Toggle navigation</span> 
 
     <span class="icon-bar"></span> 
 
     <span class="icon-bar"></span> 
 
     <span class="icon-bar"></span> 
 
     </button> 
 
     <a class="navbar-brand" href="#">Rashid Naushad</a> 
 
    </div> 
 

 
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> 
 
     <ul class="nav navbar-nav navbar-right"> 
 
     <li><a class="page-scroll" href="#about">About</a> 
 
     </li> 
 
     <li><a class="page-scroll" href="#portfolio">Portfolio</a> 
 
     </li> 
 
     <li><a class="page-scroll" href="#contact">Contact</a> 
 
     </li> 
 
     </ul> 
 
    </div> 
 
    <!-- /.navbar-collapse --> 
 
    </div> 
 
    <!-- /.container-fluid --> 
 
</nav> 
 
<!--<div class="container-fluid">--> 
 
<div> 
 
    <div id="page1"> 
 
    <a id="about"></a> 
 
    <br> 
 
    <br> 
 
    <br> 
 
    <p class="text-center">Hi ! 
 
     <br>I'm a self taught web developer based in India. 
 
     <br>I'm an undergraduate student pursuing economics who just loves to <span id="highlight">code</span>. 
 
     <br>I dream of combining my knowledge of programming, economics, and 
 
     <br>financial markets for the betterment of the society. 
 
     <br>Languages and Frameworks: 
 
     <br>HTML5, CSS3, Bootstrap3, 
 
     <br>Javascript, jQuery, Angular.js, Meteor.js, 
 
     <br>Node.js, React,js, 
 
     <br>Python, php.. 
 
     <br>Tools & expertise: Git, Responsive Web Design.</p> 
 
    </div> 
 

 
    <div id="page2"> 
 
    <a id="portfolio"></a> 
 
    <h3 class="text-center">Here is my latest portfolio, <br>I'll be adding more as I learn and grow..</h3> 
 
    <!--<div class="row"> 
 
     <div class="thumbnail img img-responsive"> 
 
     <p data-height="268" data-theme-id="0" data-slug-hash="VaKMBJ" data-default-tab="result" data-user="rashidnaushad" data-preview="true" class="codepen">See the Pen <a href="http://codepen.io/rashidnaushad/pen/VaKMBJ/">Simple Javascript App</a> by Rashid Naushad (<a href="http://codepen.io/rashidnaushad">@rashidnaushad</a>) on <a href="http://codepen.io">CodePen</a>.</p> 
 
     <script async src="//assets.codepen.io/assets/embed/ei.js"></script> 
 
     </div> 
 

 
     <div class="thumbnail img img-responsive"> 
 
     <p data-height="268" data-theme-id="0" data-slug-hash="pyPjae" data-default-tab="result" data-user="rashidnaushad" data-preview="true" class="codepen">See the Pen <a href="http://codepen.io/rashidnaushad/pen/pyPjae/">Tribute to Raghuram Rajan</a> by Rashid Naushad (<a href="http://codepen.io/rashidnaushad">@rashidnaushad</a>) on <a href="http://codepen.io">CodePen</a>.</p> 
 
     <script async src="//assets.codepen.io/assets/embed/ei.js"></script> 
 
     </div> 
 
    </div> 
 
    <div class="row"> 
 
     <div class="thumbnail"> 
 
     <img class="img img-responsive img-rounded" src="http://cleantallahassee.com/wp-content/uploads/2015/09/coming-soon.jpg" alt="Coming Soon!" /> 
 
     <div class="caption"> 
 
      <h5 class="text-center">Coming Soon!</h5> 
 
     </div> 
 
     </div> 
 

 
     <div class="thumbnail"> 
 
     <img class="img img-responsive img-rounded" src="http://cleantallahassee.com/wp-content/uploads/2015/09/coming-soon.jpg" alt="Coming Soon!" /> 
 
     <div class="caption"> 
 
      <h5 class="text-center">Coming Soon!</h5> 
 
     </div> 
 
     </div> 
 
    </div>--> 
 
    </div> 
 

 
    <div id="page3"> 
 
    <a id="contact"></a> 
 
    <h3 id="contact-text" class="text-center">Think I can be of any help to you?<br>Wanna collaborate?<br>Shoot me a message, now!</h3> 
 
    <a href="https://github.com/rashidnaushad" class="btn btn-lg col-lg-2 col-lg-offset-2 col-md-2 col-md-offset-2 col-sm-8 col-sm-offset-2 col-xs-8 col-xs-offset-2" target="_blank">GitHub</a> 
 
    <div class="hidden-lg hidden-md"> 
 
     <br> 
 
    </div> 
 
    <a href="https://www.freecodecamp.com/rashidnaushad" class="btn btn-lg col-lg-2 col-lg-offset-1 col-md-2 col-md-offset-1 col-sm-8 col-sm-offset-2 col-xs-8 col-xs-offset-2" target="_blank">freeCodeCamp</a> 
 
    <div class="hidden-lg hidden-md"> 
 
     <br> 
 
    </div> 
 
    <a href="mailto:[email protected]" class="btn btn-lg col-lg-2 col-lg-offset-1 col-md-2 col-md-offset-1 col-sm-8 col-sm-offset-2 col-xs-8 col-xs-offset-2" target="_blank">Gmail</a> 
 
    </div> 
 
</div>

+0

工作就像一個charm..but什麼母鹿s'.btn〜.btn'確實嗎?從來沒有遇到過這個問題嗎?我現在刪除這個 '

'? – Rashid

+1

的'〜'被稱爲兄弟選擇和目標,在這種情況下,所有'.btn'跟隨一個'.btn',這意味着第一個'.btn'不會得到'餘量-top'如它不」你需要一個。我還用媒體查詢更新了我的答案,所以這隻發生在較小的屏幕上。 – LGSon