在不同浏览器中的展示情况是不同的。IE8的话是这样的效果:
二、基本思想
见下图网页布局:
三、制作过程
1、同之前《【JavaScript】使用Bootstrap来编写一个在当前网页弹出的对话框,可以关闭,不用跳转,非弹窗》的第一步
因为需要使用Bootstrap,所以先在官网下载组件即可,用于生产环境的Bootstrap版本,Bootstrap3对2并不兼容,建议直接根据其开发文档使用Bootstrap3。本文也是根据Bootstrap3制作。同时,Bootstrap3所提供的JavaScript效果需要到jQuery1.11支持,可以到jQuery官网中下载兼容旧浏览器IE6的jQuery1.11(点击打开链接),而不是不兼容旧浏览器IE6的jQuery2。下载完之后,配置好站点目录。把Bootstrap3直接解压到站点目录,而把jquery-1.11.1.js放到js目录,也就是与bootstrap.js同一目录,站点文件夹的结构大致如下:
2、以下是网页的全代码,下面一部分一部分进行说明:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=no"> <link href="css/bootstrap.css" rel="stylesheet" media="screen"> <script type="text/javascript" src="js/jquery-1.11.1.js"></script> <script type="text/javascript" src="js/bootstrap.js"></script> <title>图片轮播Carousel</title> </head> <body> <div class="container"><div class="page-header"><h1> 图片轮播Carousel</h1></div><div style="width: 640px; height: 480px; margin-right: auto; margin-left: auto;"><div id="carousel" class="carousel slide" data-ride="carousel" data-interval="1000"> <ol class="carousel-indicators"> <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li> <li data-target="#carousel-example-generic" data-slide-to="1"></li> <li data-target="#carousel-example-generic" data-slide-to="2"></li> </ol> <div class="carousel-inner" role="listbox"> <div class="item active"><a href="images/img0.jpg"><img src="images/img0.jpg" alt="img0"></a><div class="carousel-caption"><h3> img0</h3><p> 我是img0的图片说明</p></div> </div> <div class="item"><a href="images/img10.jpg"><img src="images/img10.jpg" alt="img10"></a><div class="carousel-caption"><h3> img10</h3><p> 我是img10的图片说明</p></div> </div> <div class="item"><a href="images/img2.jpg"><img src="images/img2.jpg" alt="img2"></a><div class="carousel-caption"><h3> img2</h3><p> 我是img2的图片说明</p></div> </div> </div> <a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev"> <span class="glyphicon glyphicon-chevron-left"></span> </a> <a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next"> <span class="glyphicon glyphicon-chevron-right"></span> </a></div></div> </div> </body></html>(1)<head>部分
<head><!--声明网页编码,自动适应浏览器的尺寸,要使用bootstrap的css,需要jquery支持,要使用bootstrap的js,标题--> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=no"> <link href="css/bootstrap.css" rel="stylesheet" media="screen"> <script type="text/javascript" src="js/jquery-1.11.1.js"></script> <script type="text/javascript" src="js/bootstrap.js"></script> <title>图片轮播Carousel</title> </head>(2)<body>部分
<div class="page-header"><h1> 图片轮播Carousel</h1></div>之后定义一个未命名的图层div,主要是用来规范图片轮播组件用的。bootstrap的图片轮播组件大小不能对其里面的元素,加入width与height参数进行规定。这样图片轮播组件会失真。同时这个组件要居中,必须在div的style属性中使用margin-right: auto; margin-left: auto;来约束,额外加入align="center"是根本一点效果都没有。
<div style="width: 640px; height: 480px; margin-right: auto; margin-left: auto;"><!--图片轮播组件的名称为carousel,data-ride元素是bootstrap要求存在的,data-interval的值是每隔1000毫秒,也就是1秒换一张图片,此值太小组件会失真--><div id="carousel" class="carousel slide" data-ride="carousel" data-interval="1000"> <!--这里定义有几张图片,如果再多一张图片就再下面多加一项,data-slide-to的值加一,首张图片也就是第0张图片必须要有class="active"否则组件无法工作--> <ol class="carousel-indicators"> <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li> <li data-target="#carousel-example-generic" data-slide-to="1"></li> <li data-target="#carousel-example-generic" data-slide-to="2"></li> </ol> <div class="carousel-inner" role="listbox"> <!--以下是各张的图片的详细编辑,首张图片的class值必须为item active,余下的皆为item--> <div class="item active"> <!--意为点击img0.jpg这张图片就打开img0.jpg的超级链接,如果不需要超级链接,则去掉<a>标签--><a href="images/img0.jpg"><img src="images/img0.jpg" alt="img0"></a><div class="carousel-caption"><!--图片下的文字说明--><h3> img0</h3><p> 我是img0的图片说明</p></div> </div> <div class="item"><a href="images/img10.jpg"><img src="images/img10.jpg" alt="img10"></a><div class="carousel-caption"><h3> img10</h3><p> 我是img10的图片说明</p></div> </div> <div class="item"><a href="images/img2.jpg"><img src="images/img2.jpg" alt="img2"></a><div class="carousel-caption"><h3> img2</h3><p> 我是img2的图片说明</p></div> </div> </div> <!--这里是组件中向左想右的两个按钮,固定存在的框架代码--> <a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev"> <span class="glyphicon glyphicon-chevron-left"></span> </a> <a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next"> <span class="glyphicon glyphicon-chevron-right"></span> </a></div></div>以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。