使用Bootstrap 3开发响应式网站实践01,前期准备、导航区域等
“使用Bootstrap 3开发响应式网站实践”系列,将使用Bootstrap 3.2制作一个自适应网站,无论是在电脑、平板,还是手机上,都呈现比较好的效果。在电脑浏览器上的最终效果如下:

在手机上的呈现效果如下:

本篇主要包括:
□ 前期准备
□ 引入Bootstrap,jQuey文件等
□ 搭建页面主体结构
□ 导航区域
□ 前期准备
→使用Visual Studio 2012创建一个空的ASP.NET网站
→把下载下来的Bootstrap 3中的css,fonts,js文件夹拷贝到网站根目录下
→在网站根目录下创建images文件夹,用来存放所有的图片
→使用NuGet安装最新版的jQuery
→在网站根目录下创建index.html文件
□ 引入Bootstrap,jQuey文件等
→在css文件夹下创建site.css
→在js文件夹下创建site.js
→把Bootstrap、jQuery等引入index.html文件中
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><title></title><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1,user-scalable=no"><link href="css/bootstrap.min.css" rel="stylesheet" /><link href="css/site.css" rel="stylesheet" /></head><body><script src="Scripts/jquery-2.1.1.min.js"></script><script src="js/bootstrap.min.js"></script><script src="js/site.js"></script></body></html>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1,user-scalable=no">
这时响应式布局的一个标签,以上表示浏览器的宽度和屏幕宽度保持一致。比如,屏幕宽度为320px,那么浏览器的宽度自适应为320px。如果网站不是针对响应式开发的,就没有必要使用这个标签。其它属性包括:
○ width: 视觉宽度
○ device-width: 屏幕宽度
○ heigh: 视觉高度
○ device-height: 屏幕高度
○ initial-scale: 初始放大比例,设置为1.0时不放大
○ minimum-scale: 最小的放大比例,设置为1.0时不放大
○ maximum-scale: 最大的放大比例,设置为1.0时不放大
○ user-scalable: 是否允许放大,设置为no时不允许放大
另外,需要在site.css设置如下:
@-ms-viewport{width: device-width;}
□ 搭建页面主体结构
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><title></title><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1,user-scalable=no"><link href="css/bootstrap.min.css" rel="stylesheet" /><link href="css/site.css" rel="stylesheet" /></head><body><div class="container" id="main"><div class="navbar navbar-fixed-top"></div><!--导航区域结束--><div class="carousel" id="myCarousel"></div><!--图片展示区域结束--><div class="row" id="bigCallout"></div><!--标注区域结束--><div class="row" id="featureHeading"></div><!--主要功能标题区域结束--><div class="row" id="features"></div><!--主要功能区域结束--><div class="row" id="moreInfo"></div><!--更多信息区域结束--><div class="row" id="moreProducts"></div><!--更多产品区域结束--></div><!--container结束--><footer></footer><!--页脚结束--><script src="Scripts/jquery-2.1.1.min.js"></script><script src="js/bootstrap.min.js"></script><script src="js/site.js"></script></body></html>
○ row属性值:代表单独一行,是Grid的父级元素。
□ 导航区域
→到http://bootswatch.com/下载Theme,并保存到网站根目录下的css文件夹
→下载某个主题,命名为simplex.bootstrap.min.css,并放到如下位置:
<link href="css/bootstrap.min.css" rel="stylesheet" /><link href="css/simplex.bootstrap.min.css" rel="stylesheet" /><link href="css/site.css" rel="stylesheet" />
→导航区域html如下:
<div class="navbar navbar-fixed-top"><div class="container"><a class="navbar-brand" href="/"><img src="data:images/logo.gif" alt="logo"/></a><div class="navbar-collapse collapse"><ul class="nav navbar-nav"><li class="active"><a href="#">主页</a></li><li><a href="#">服务</a></li></ul><form class="navbar-form pull-left"><input type="text" id="searchInput" class="form-control" placeholder="搜索"/><button type="submit" class="btn btn-default"><span class="glyphicon glyphicon-search"></span></button></form><ul class="nav navbar-nav pull-right"><li><a href="#"><span class="glyphicon glyphicon-user"></span>设置</a></li></ul></div><!--nav-collapse结束--></div><!--container结束--></div><!--导航区域结束-->
○ navbar属性值:导航区域、导航条
○ navbar-fixed-top属性值:把导航区域固定在页面顶部
○ 把导航区域的所有元素放在一个<div class="container">中
○ navbar-brand属性值:一般应用在网站logo上
○ navbar-collapse和collapse属性值:是为响应式设计的,当希望一块区域在页面宽度缩小到一定程度时自己适应,就把该块区域包裹在<div class="navbar-collapse collapse">中
○ nav和navbar-nav属性值:用在ul上,可以把a标签加入到导航区域
○ navbar-form属性值:用在form上,可以把form标签加入到导航区域
○ pull-left属性值:漂浮到左边
○ form-control属性值:用在表单元素上
○ glyphicon和glyphicon-search属性值:用在span上,显示Icon
当页面足够宽的时候,显示<div class="navbar-collapse collapse">包裹区域。

当页面宽度缩小到一定程度,<div class="navbar-collapse collapse">包裹区域被隐藏。

把以上ul中<li><a href="#">服务</a></li>改成下拉菜单。
<ul class="nav navbar-nav"><li class="active"><a href="#">主页</a></li><li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">服务<strong class="caret"></strong></a><ul class="dropdown-menu"><li><a href="#">足球</a></li><li><a href="#">篮球</a></li><li><a href="#">乒乓球</a></li><li class="divider"></li><li class="dropdown-header">更多服务...</li><li><a href="#">创建水球</a></li><li><a href="#">创建冰球</a></li></ul></li></ul>

○ dropdown属性值:这里被应用到li上,这样li变成下拉菜单
○ data-toggle属性:这里属性值为dropdown,表示使用dropdown插件
○ dropdown-toggle属性值:这里了被应用到a上,点击a显示,再点击消失
○ caret属性值:这里被应用到strong,表示向下箭头
○ dropdown的选项是一个ul
○ dropdown-menu属性值:这里被应用到ul上,表示dropdown的选项
○ divider属性值:这里被应用到li上,表示分隔线
○ dropdown-header属性值:这里被应用到li上,该项无法被选中
再把以上"我的账户"有关ul中的li改成下拉菜单。
<ul class="nav navbar-nav pull-right"><li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown"><span class="glyphicon glyphicon-user"></span>我的账户<strong class="caret"></strong></a><ul class="dropdown-menu"><li><a href="#"><span class="glyphicon glyphicon-wrench"></span>设置</a></li><li><a href="#"><span class="glyphicon glyphicon-refresh"></span>个人信息</a></li><li><a href="#"><span class="glyphicon glyphicon-briefcase"></span>账单</a></li><li class="divider"></li><li><a href="#"><span class="glyphicon glyphicon-off"></span>退出</a></li></ul></li></ul>

但,现在还有一个问题:当页面宽度缩小到一定程度,<div class="navbar-collapse collapse">包裹区域被隐藏。我们希望在隐藏的时候,出现一个图片,点击它再次出现被隐藏部分。
在导航部分添加一个按钮:
<button type="button" class="navbar-toggle" data-target="#c" data-toggle="collapse" style="background-color: #e3e3e3;color: black">显示/隐藏</button>

○ navbar-toggle属性值:这里被应用到button,当点击button出现隐藏部分
○ data-target属性:这里指向隐藏/显示的div
○ data-toggle属性:引用collapse插件
最后,导航部分的完整代码为:
<div class="navbar navbar-fixed-top"><div class="container"><button type="button" class="navbar-toggle" data-target="#c" data-toggle="collapse" style="background-color: #e3e3e3;color: black">显示/隐藏</button><a class="navbar-brand" href="/"><img src="data:images/logo.gif" alt="logo"/></a><div class="navbar-collapse collapse" id="c"><ul class="nav navbar-nav"><li class="active"><a href="#">主页</a></li><li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">服务<strong class="caret"></strong></a><ul class="dropdown-menu"><li><a href="#">足球</a></li><li><a href="#">篮球</a></li><li><a href="#">乒乓球</a></li><li class="divider"></li><li class="dropdown-header">更多服务...</li><li><a href="#">创建水球</a></li><li><a href="#">创建冰球</a></li></ul></li></ul><form class="navbar-form pull-left"><input type="text" id="searchInput" class="form-control" placeholder="搜索"/><button type="submit" class="btn btn-default"><span class="glyphicon glyphicon-search"></span></button></form><ul class="nav navbar-nav pull-right"><li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown"><span class="glyphicon glyphicon-user"></span>我的账户<strong class="caret"></strong></a><ul class="dropdown-menu"><li><a href="#"><span class="glyphicon glyphicon-wrench"></span>设置</a></li><li><a href="#"><span class="glyphicon glyphicon-refresh"></span>个人信息</a></li><li><a href="#"><span class="glyphicon glyphicon-briefcase"></span>账单</a></li><li class="divider"></li><li><a href="#"><span class="glyphicon glyphicon-off"></span>退出</a></li></ul></li></ul></div><!--nav-collapse结束--></div><!--container结束--></div><!--导航区域结束-->
参考资料:
Code a Responsive Website with Bootstrap 3 --by Brad Hussey
http://getbootstrap.com/
http://bootswatch.com/
http://www.tutorialspoint.com/
“使用Bootstrap 3开发响应式网站实践”系列包括:
使用Bootstrap 3开发响应式网站实践01,前期准备、导航区域等
使用Bootstrap 3开发响应式网站实践02,轮播
使用Bootstrap 3开发响应式网站实践03,轮播下方的内容排版
使用Bootstrap 3开发响应式网站实践04,使用Panels展示内容
使用Bootstrap 3开发响应式网站实践05,使用Tab、Modal、Form展示内容,使用Popover、Tooltip展示提示信息
使用Bootstrap 3开发响应式网站实践06,使用ListGroup、Thumbnails展示内容
使用Bootstrap 3开发响应式网站实践07,页脚
使用Bootstrap 3开发响应式网站实践01,前期准备、导航区域等的更多相关文章
- 使用Bootstrap 3开发响应式网站实践07,页脚
页脚部分比较简单,把一个12列的Grid切分. <footer> <div class="container"> <div class="r ...
- 使用Bootstrap 3开发响应式网站实践06,使用ListGroup、Thumbnails展示内容
□ ListGroup展示内容 当希望把同类型的内容以列表.区块展示的时候,ListGroup是不错的选择. <div class="col-sm-6"> <h3 ...
- 使用Bootstrap 3开发响应式网站实践05,使用Tab、Modal、Form展示内容,使用Popover、Tooltip展示提示信息
本篇体验用Tab插件显示内容.Html部分为: <div class="row" id="moreInfo"> <div class=&quo ...
- 使用Bootstrap 3开发响应式网站实践04,使用Panels展示内容
在Bootstrap页面中,通常用Panels来展示主要功能的内容.该部分Html为: <div class="row" id="featureHeading&qu ...
- 使用Bootstrap 3开发响应式网站实践03,轮播下方的内容排版
通常把一些重要信息.需要重点标注的信息放在轮播的下方显示,这部分区域用到了大字体的标题.副标题以及段落文字等. <div class="row" id="bigCa ...
- 使用Bootstrap 3开发响应式网站实践02,轮播
本篇体验图片轮播.html部分为: <div class="carousel slide" id="myCarousel" > <!--Ind ...
- 了解Bootstrap和开发响应式网站
什么是Bootstrap? Bootstrap是Twitter推出的一个开源的用于web前端开发的工具包.它由Twitter的设计师Mark Otto和Jacob Thornton合作开发,是一个CS ...
- 7 个 Bootstrap 在线编辑器用于快速开发响应式网站
Bootstrap 已经使响应式网站开发变得简单很多. 但是如果你不必手动写全部代码,事情会如何呢? 如果你可以自由地选择你想要使用的Bootstrap 组件.并可以把它们拖拽到画布中,事情会如何呢? ...
- Bootstrap 简介: 创建响应式、移动项目的工具
原文链接: Introduction to Bootstrap: A Tool for Building Responsive, Mobile-First Projects 下载: 示例代码Boots ...
随机推荐
- delphi 获取一个字符占用几个字节,方法
- LeetCode699. Falling Squares
On an infinite number line (x-axis), we drop given squares in the order they are given. The i-th squ ...
- **PHP删除数组中特定元素的两种方法array_splice()和unset()
方法一: 复制代码代码如下: <?php$arr1 = array(1,3, 5,7,8);$key = array_search(3, $arr1); if ($key !== false) ...
- Ocelot 配置初始
{ "ReRoutes": [ { /*将用户的请求 /post/1 转发到 localhost/api/post/1*/ /* DownstreamPathTemplate:转到 ...
- CF 577A 分解因数
输入一个n 构成一个n*n的表 这个表里的数 第i行j列的值为i*j 问x在这个表里出现了几次 Sample test(s)input10 5output2input6 12output4input5 ...
- maven:missing artifact jdk.tools:jar:1.7
http://stackoverflow.com/questions/11118070/buiding-hadoop-with-eclipse-maven-missing-artifact-jdk-t ...
- 【Codeforces】113 D. Museum
题解 我们设\(f(i,j)\)是\((i,j)\)这个点期望被经过多少次 我们可以列出方程组来消元,由于终点只会被经过0次或者1次,期望就是概率 对于起点的话我们期望经过次数多加一个1 复杂度\(O ...
- ubuntu编译安装postgresql
闲着没事用源码编译安装了postgresql,遇到了不少故障,记录一下. 1:用./configure配置时发生错误.看信息说是缺少相关包.有什么readline,zlip等. 我配置的很简单,只是配 ...
- hadoop集群的搭建(分布式安装)
集群 计算机集群是一种计算机系统,他通过一组松散集成的计算机软件和硬件连接起来高度紧密地协同完成计算工作. 集群系统中的单个计算机通常称为节点,通常通过局域网连接. 集群技术的特点: 1.通过多台计算 ...
- Gitlab服务器维护
一. 内容 Gitlab服务器的更新 Gitlab服务器备份与恢复 导入Git仓库 二. Gitlab服务器的更新 1. 使用SSH登陆Gitlab服务器 2. 停止后端的unicorn服务器 [ro ...