Bootstrap响应式前端框架笔记十八——导航滚动监听

Bootstrap响应式前端框架笔记十八——导航滚动监听

    Bootstrap框架中提供了十分方便的方法来使用导航关联内容快,并且开发者可以监听滚动进行导航按钮的切换,示例如下:

<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<link rel="stylesheet" href="../bower_components/bootstrap/dist/css/bootstrap.css" />
		<script type="text/javascript" src="../bower_components/jquery/dist/jquery.js"></script>
		<script type="text/javascript" src="../bower_components/bootstrap/dist/js/bootstrap.js"></script>
		<style>
			.scrollspy-example {
				position: relative;
				height: 200px;
				margin-top: 10px;
				overflow: auto
			}
		</style>
		<title>滚动监听</title>
	</head>

	<body class="container">
		<br />
		<br />
		<!--导航栏-->
		<nav id="navbar" class="navbar navbar-default navbar-static">
			<div class="navbar-header">
				<button class="navbar-toggle" type="button" data-toggle="collapse" data-target=".navigationShow">
					<span class="icon-bar"></span>
            			<span class="icon-bar"></span>
            			<span class="icon-bar"></span>
				</button>
				<a class="navbar-brand" href="#">列表</a>
			</div>
			<!--导航容器-->
			<div class="navbar-collapse navigationShow">
				<!--导航-->
				<ul class="nav navbar-nav">
					<li class="active">
						<a href="#One">第一项</a>
					</li>
					<li class="">
						<a href="#Two">第二项</a>
					</li>
					<li class="dropdown">
						<a href="#" id="navbarDrop1" class="dropdown-toggle" data-toggle="dropdown">菜单 <span class="caret"></span></a>
						<ul class="dropdown-menu">
							<li class="">
								<a href="#one">菜单1</a>
							</li>
							<li class="">
								<a href="#two">菜单2</a>
							</li>
							<li class="divider"></li>
							<li class="">
								<a href="#three">菜单3</a>
							</li>
						</ul>
					</li>
				</ul>
			</div>
		</nav>
		<!--内容div 使用data-spy="scroll"来进行滚动监听-->
		<div data-spy="scroll" data-target="#navbar" class="scrollspy-example">
			<!--id需要对应-->
			<h4 id="One">第一项</h4>
			<p>Ad leggings keytar, brunch id art party dolor labore. Pitchfork yr enim lo-fi before they sold out qui. Tumblr farm-to-table bicycle rights whatever. Anim keffiyeh carles cardigan. Velit seitan mcsweeney's photo booth 3 wolf moon irure. Cosby sweater lomo jean shorts, williamsburg hoodie minim qui you probably haven't heard of them et cardigan trust fund culpa biodiesel wes anderson aesthetic. Nihil tattooed accusamus, cred irony biodiesel keffiyeh artisan ullamco consequat.</p>
			<h4 id="Two">第二项</h4>
			<p>Veniam marfa mustache skateboard, adipisicing fugiat velit pitchfork beard. Freegan beard aliqua cupidatat mcsweeney's vero. Cupidatat four loko nisi, ea helvetica nulla carles. Tattooed cosby sweater food truck, mcsweeney's quis non freegan vinyl. Lo-fi wes anderson +1 sartorial. Carles non aesthetic exercitation quis gentrify. Brooklyn adipisicing craft beer vice keytar deserunt.</p>
			<h4 id="one">one</h4>
			<p>Occaecat commodo aliqua delectus. Fap craft beer deserunt skateboard ea. Lomo bicycle rights adipisicing banh mi, velit ea sunt next level locavore single-origin coffee in magna veniam. High life id vinyl, echo park consequat quis aliquip banh mi pitchfork. Vero VHS est adipisicing. Consectetur nisi DIY minim messenger bag. Cred ex in, sustainable delectus consectetur fanny pack iphone.</p>
			<h4 id="two">two</h4>
			<p>In incididunt echo park, officia deserunt mcsweeney's proident master cleanse thundercats sapiente veniam. Excepteur VHS elit, proident shoreditch +1 biodiesel laborum craft beer. Single-origin coffee wayfarers irure four loko, cupidatat terry richardson master cleanse. Assumenda you probably haven't heard of them art party fanny pack, tattooed nulla cardigan tempor ad. Proident wolf nesciunt sartorial keffiyeh eu banh mi sustainable. Elit wolf voluptate, lo-fi ea portland before they sold out four loko. Locavore enim nostrud mlkshk brooklyn nesciunt.</p>
			<h4 id="three">three</h4>
			<p>Ad leggings keytar, brunch id art party dolor labore. Pitchfork yr enim lo-fi before they sold out qui. Tumblr farm-to-table bicycle rights whatever. Anim keffiyeh carles cardigan. Velit seitan mcsweeney's photo booth 3 wolf moon irure. Cosby sweater lomo jean shorts, williamsburg hoodie minim qui you probably haven't heard of them et cardigan trust fund culpa biodiesel wes anderson aesthetic. Nihil tattooed accusamus, cred irony biodiesel keffiyeh artisan ullamco consequat.</p>
			<p>Keytar twee blog, culpa messenger bag marfa whatever delectus food truck. Sapiente synth id assumenda. Locavore sed helvetica cliche irony, thundercats you probably haven't heard of them consequat hoodie gluten-free lo-fi fap aliquip. Labore elit placeat before they sold out, terry richardson proident brunch nesciunt quis cosby sweater pariatur keffiyeh ut helvetica artisan. Cardigan craft beer seitan readymade velit. VHS chambray laboris tempor veniam. Anim mollit minim commodo ullamco thundercats.
			</p>
		</div>
	</body>
</html>

效果如下所示:

    开发者也可以对导航标签的切换事件进行监听,方法如下:

$('#navbar').on('activate.bs.scrollspy',function(e){
		console.log("滚动导航改变");
})

   另外,本篇博客中所有的实例代码及显示效果,在如下地址中,需要的可以自行对照学习。

http://zyhshao.github.io/bootStrapDemo/scrollJS.html

前端学习新人,有志同道合的朋友,欢迎交流与指导,QQ群:541458536

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏Google Dart

AngularDart Material Design 切换 顶

用户可以点击切换按钮来更改状态。 通常,当您有一个ON / OFF选项时,您可以使用切换按钮。 如果您要一组中的多个选项中选中/取消选中,请改用material...

8720
来自专栏韩东吉的Unity杂货铺

零基础入门 13: UGUI Text

前几篇介绍了UGUI里的Image,今天来说下UGUI 的Text,显示文本的组件。因为有了之前代码创建Image的铺垫,所以对Text的使用就都在这篇介绍了。

17820
来自专栏24K纯开源

ChartDirector应用笔记(三)

前言 继上篇文章(Simple bar chart)推出之后,本篇文章继续ChartDirector的使用。在这篇Blog中,博主实现的是soft lighti...

259100
来自专栏Android-薛之涛

Android-ConstraintLayout详解(下)

我们接着上一篇说一说ConstraintLayout的其他属性,如果没看过上一篇:Android-ConstraintLayout详解 先罗列一下此次的会议内...

23430
来自专栏happyJared

IDEA快捷键拆解系列(十三):Window篇

  以下是关于Window导航项及其每一子项的拆解介绍,其中,加粗部分的选项是博主认为比较重要的。

37110
来自专栏增长技术

ProgressBar

自定义view实现下载进度的展示 控件完全自定义,字体大小,圆环宽度,实心或者空心,圆环的宽度,圆环的颜色,进度字体的粗细,字体颜色等等都可以自定义

21930
来自专栏达摩兵的技术空间

css3媒体查询简介

<link rel="stylesheet" media="min-width=900" />

9650
来自专栏小白课代表

软件分享 | ZoomIt 4.5 演示辅助工具使用教程

ZoomIt是一款非常实用的投影演示辅助软件。它源自Sysinternals公司,后来此公司被微软收购,因此,有些网友也称ZoomIt为微软放大镜。ZoomIt...

13610
来自专栏木头编程 - moTzxx

PHP 开发学习[3] —— 文字水印,缩略图,图片水印实现类

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011415782/article/de...

20630
来自专栏cnblogs

bootstrap源码分析之form、navbar

一、表单(Form) 源码文件: _form.scss mixins/_form.scss 1、按层次结构分:form-group -> form-contr...

24970

扫码关注云+社区

领取腾讯云代金券