bootstrap课程12 滚动监听如何实现(bootstrap方式和自定义方式)
一、总结
一句话总结:通过监听滚动的高,判断滚动的高是否大于元素距离顶端的距离
1、如何知道屏幕滚动的高?
st=$(window).scrollTop();
2、如何知道元素距离顶端的距离?
offset的top属性
pt=$(this).offset().top;
3、bootstrap中如何实现滚动监听?
需要相对定位(relative positioning)
无论何种实现方式,滚动监听都需要被监听的组件是 position: relative;
即相对定位方式。大多数时候是监听 <body>
元素。When scrollspying on elements other than the <body>
, be sure to have a height
set and overflow-y: scroll;
applied.
通过 data 属性调用
To easily add scrollspy behavior to your topbar navigation, add data-spy="scroll"
to the element you want to spy on (most typically this would be the <body>
). Then add the data-target
attribute with the ID or class of the parent element of any Bootstrap .nav
component.
4、如何选中除自己之外的其它元素?
最好就是not方法,not方法里面可以放jquery选择器选择的jquery对象
$('.navbar-nav li').not($('.'+idx)).removeClass('active');
二、滚动监听如何实现(bootstrap方式和自定义方式)
1、相关知识
需要相对定位(relative positioning)
无论何种实现方式,滚动监听都需要被监听的组件是 position: relative;
即相对定位方式。大多数时候是监听 <body>
元素。When scrollspying on elements other than the <body>
, be sure to have a height
set and overflow-y: scroll;
applied.
通过 data 属性调用
To easily add scrollspy behavior to your topbar navigation, add data-spy="scroll"
to the element you want to spy on (most typically this would be the <body>
). Then add the data-target
attribute with the ID or class of the parent element of any Bootstrap .nav
component.
body {
position: relative;
}
<body data-spy="scroll" data-target="#navbar-example">
...
<div id="navbar-example">
<ul class="nav nav-tabs" role="tablist">
...
</ul>
</div>
...
</body>
2、代码
bs滚动监听
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
<style>
*{
font-family: 微软雅黑;
}
body{
padding-top:60px;
position: relative;
}
</style>
<link rel="stylesheet" href="bs/css/bootstrap.min.css">
<script src="bs/js/jquery.min.js"></script>
<script src="bs/js/bootstrap.min.js"></script>
<script src="bs/js/holder.min.js"></script>
</head>
<body data-spy='scroll' data-target='#mynav' data-offset='150'>
<div class="container">
<nav class="navbar navbar-inverse navbar-fixed-top" id='mynav'>
<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="#">Brand</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">
<li class='active'><a href="#linux">linux</a></li>
<li><a href="#php">php</a></li>
<li><a href="#js">js</a></li>
<li><a href="#html5">html5</a></li>
</ul>
</div>
</div>
</nav> <div class="panel panel-primary">
<div class="panel-heading">
<div class="panel-title">
<h1 id='linux'>linux技术</h1>
</div>
</div>
<div class="panel-body">
<img src="holder.js/100%x500" alt="">
<p>linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!</p>
</div>
</div> <div class="panel panel-primary">
<div class="panel-heading">
<div class="panel-title">
<h1 id='php'>php技术</h1>
</div>
</div>
<div class="panel-body">
<img src="holder.js/100%x500" alt="">
<p>php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!</p>
</div>
</div> <div class="panel panel-primary">
<div class="panel-heading">
<div class="panel-title">
<h1 id='js'>js技术</h1>
</div>
</div>
<div class="panel-body">
<img src="holder.js/100%x500" alt="">
<p>js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!</p>
</div>
</div> <div class="panel panel-primary">
<div class="panel-heading">
<div class="panel-title">
<h1 id='html5'>html5技术</h1>
</div>
</div>
<div class="panel-body">
<img src="holder.js/100%x500" alt="">
<p>html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!</p>
</div>
</div>
</div>
</body>
</html>
自定义滚动监听实例
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
<style>
*{
font-family: 微软雅黑;
}
body{
padding-top:60px;
position: relative;
} .list-group{
position: fixed;
top:200px;
}
</style>
<link rel="stylesheet" href="bs/css/bootstrap.min.css">
<script src="bs/js/jquery.min.js"></script>
<script src="bs/js/bootstrap.min.js"></script>
<script src="bs/js/holder.min.js"></script>
</head>
<body data-spy='scroll' data-target='#mynav' data-offset='150'>
<div class="container">
<nav class="navbar navbar-inverse 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="#">Brand</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">
<li class='active linux'><a href="">linux</a></li>
<li class='php'><a href="">php</a></li>
<li class='js'><a href="">js</a></li>
<li class='html5'><a href="">html5</a></li>
</ul>
</div>
</div>
</nav> <div class="row">
<div class="col-md-1">
<div class="list-group">
<a id='linux' href="javascript:" class="list-group-item active">linux</a>
<a id='php' href="javascript:" class="list-group-item ">php</a>
<a id='js' href="javascript:" class="list-group-item ">js</a>
<a id='html5' href="javascript:" class="list-group-item ">html5</a>
</div>
</div> <div class="col-md-11">
<div class="panel panel-primary" idx='linux'>
<div class="panel-heading">
<div class="panel-title">
<h1>linux技术</h1>
</div>
</div>
<div class="panel-body">
<img src="holder.js/100%x500" alt="">
<p>linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!</p>
</div>
</div> <div class="panel panel-primary" idx='php'>
<div class="panel-heading">
<div class="panel-title">
<h1>php技术</h1>
</div>
</div>
<div class="panel-body">
<img src="holder.js/100%x500" alt="">
<p>php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!</p>
</div>
</div> <div class="panel panel-primary" idx='js'>
<div class="panel-heading">
<div class="panel-title">
<h1>js技术</h1>
</div>
</div>
<div class="panel-body">
<img src="holder.js/100%x500" alt="">
<p>js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!js is very much!</p>
</div>
</div> <div class="panel panel-primary" idx='html5'>
<div class="panel-heading">
<div class="panel-title">
<h1>html5技术</h1>
</div>
</div>
<div class="panel-body">
<img src="holder.js/100%x500" alt="">
<p>html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!html5 is very much!</p>
</div>
</div>
</div>
</div> </div>
</body>
<script>
$(window).scroll(function(){
$('.panel').each(function(){
st=$(window).scrollTop();
pt=$(this).offset().top; if(st>=(pt-200)){
idx=$(this).attr('idx');
$('#'+idx).addClass('active');
$('.list-group-item').not($('#'+idx)).removeClass('active'); $('.'+idx).addClass('active');
$('.navbar-nav li').not($('.'+idx)).removeClass('active');
}
});
});
</script>
</html>