您的位置:首页 > Web前端 > BootStrap

bootstrap轮播图

2017-01-13 13:06 169 查看
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>使用carousel</title>
<!-- Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="/stylesheets/bootstrap.min.css">

<!-- jQuery文件 -->
<script src="/scripts/jquery.min.js"></script>

<!-- Bootstrap 核心 JavaScript 文件 -->
<script src="/scripts/bootstrap.min.js"></script>
</head>
<body>
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel" style="margin:10px;">
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<div class="item active">
<a href="javascript:void(0)">
<img src="http://fun.datang.net/uploadpic/276e0c7bb66b46318c3bb9c59cad9411.jpg" style="width:300px;height:300px;" alt="图片一"/></a>
<div class="carousel-caption">
<h4 class="alpha">
<a style="color:white;" href="javascript:void(0)">驴子跳</a>
</h4>
</div>

</div>
<div class="item">
<a href="javascript:void(0)">
<img src="http://img5.imgtn.bdimg.com/it/u=372265704,58471841&fm=21&gp=0.jpg" style="width:300px;height:300px;" alt="图片二"/>
</a>
<div class="carousel-caption">
<h4 class="alpha">
<a style="color:white;" href="javascript:void(0)">MarkDown</a>
</h4>
</div>
</div>
<div class="item">
<a href="javascript:void(0)">
<img src="http://img1.imgtn.bdimg.com/it/u=3318255286,2969027749&fm=23&gp=0.jpg" style="width:300px;height:300px;" alt="图片三"/>
</a>
<div class="carousel-caption">
<h4 class="alpha">
<a style="color:white;" href="javascript:void(0)">BootStrap</a>
</h4>
</div>
</div>
<!-- 控制按钮 -->
<a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
</body>
</html>

解决展示时图片变形的问题

运行上节的代码我们发现插件中的图片发生了变形,分析其原因为:轮换插件中的图片使用的文章中的第一张图片,图片的大小不一,而轮播插件的大小基本是固定的,所以展示的时候图片出现了变形。下面看看怎么解决这个问题:

1.引入Jqthumb.js

在BootStrap中我们找不到解决办法,所以我们需要借助其它工具。Jqthumb插件是专门用来为图片生成缩略图的,它可以从图片中的任何坐标点开始取指定大小的图片区域作为图片的缩略图。你可以点击 https://github.com/pakcheong/jqthumb 来下载它,并将其应用到项目中(假设在当前项目中,jqthumb.js放置在scripts文件夹中):

<script type="text/javascript" src="/scripts/jqthumb.js"></script>

2.在图片加载(onload)的时候调用DrawImage()函数来生成缩略图

DrawImage()函数正是基于jqthumb.js库的,注意该函数一定要写在轮换插件前,因为我们必须在图片加载前生成缩略图。DrawImage()函数代码如下:

<!--导入插件-->

<script type="text/javascript" src="/scripts/jqthumb.js"></script>

<script>

function DrawImage(hotimg)

{

$(hotimg).jqthumb({

classname : 'jqthumb',

width : '100%',//宽度

height : '300px',//高度

position : { y: '50%', x: '50%'},//从图片的中间开始产生缩略图

zoom : '1',//缩放比例

method : 'auto',//提交方法,用于不同的浏览器环境,默认为‘auto’

});

}

</script>

在上述代码中,我们使用了jqthumb,并且传入了相关初始化参数。调用了该函数后,在图片加载的时候,就会按照上述参数产生图片的缩略图,从而解决图片变形问题。由于缩略图是从原始图片的正中间开始往两边取得,所以该缩略图包含了图片的主要内容。(具体使用见右边详细代码)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: