我有两个javascript块,如果一个是可见的,那么它的工作原理和其他的一样。如果两个代码块都在同一个.js文件中,或者一个在.js文件中,而另一个在index.html中的脚本标记中,则只有一个页面滚动有效。如果只放入折叠代码而不是滚动代码,则导航栏会很好地折叠。我不能让他们彼此玩得很好。
的index.html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Portfolio</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
<!-- <link rel="stylesheet" href="../css/app.css" /> -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
</head>
<body>
<app-root>Loading...</app-root>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="app/nav.js"></script>
<script src="app/nav1.js"></script>
<script>
$(document).on('click', '.navbar-collapse.in', function (e) {
console.log("navbar");
if ($(e.target).is('a')) {
$(this).collapse('hide');
}
});
$(document).ready(function(){
$(function() {
$('a[href*="#"]:not([href="#"])').click(function() {
console.log("scroll");
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
}, 800);
return false;
}
}
});
});
});
</script>
</body>
</html>
app.component.html
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container-fluid">
<!-- 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="#">Logo</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
</ul>
<ul class="nav navbar-nav">
<li class="divider-vertical"></li>
<li><a href="#1">Section1</a></li>
<li class="divider-vertical"></li>
<li><a href="#2">Section2</a></li>
<li class="divider-vertical"></li>
<li><a href="#3">Section 3</a></li>
<li class="divider-vertical"></li>
<li><a href="#4">Section 4</a></li>
<li class="divider-vertical"></li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
<div id="1" class="frontpage">
</div>
<div id="2" class="portfolio">
<img class="line" src="port/images/line.png" alt="Line"/>
<div id="portfolioTitle">
Portfolio
</div>
<img class="line1" src="port/images/line.png" alt="Line"/>
</div>
<div class="container">
<div class="row">
<div class="col-md-4 col-sm-4 col-lg-4">
<a href="#"><img src="port/images/pic1.png" class="portImage1"></a>
</div>
<div class="col-md-4 col-sm-4 col-lg-4">
<img src="port/images/pic2.png" class="portImage1"/>
</div>
<div class="col-md-4 col-sm-4 col-lg-4">
<img src="port/images/pic3.png" class="portImage1"/>
</div>
</div>
</div>
<div id="3" style="min-height:1000px;background:blue;"></div>
<div id="4" style="min-height:1000px;background:orange;"></div>