最近在做网站的时候,想让用户点击缩略图时可以放大显示。自己写又麻烦,于是找了第三方的插件,实现方法如下:

下载Bootstrap-viewer库

下载地址:https://github.com/movingheart/bootstrap-viewer

大家可以用https的方式拉取,这样会更方便一些,命令如下:

git clone https://github.com/movingheart/bootstrap-viewer.git

在网页中配置库

网页内容如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
    <style>
        .viewer{
            width: auto;
            height: 200px;
        }
        .container{
            margin-top: 200px;
        }
    </style>
</head>
<body>
    
    <div class="container">
        <div class="row">
            <img src="./images/peiqi_s.jpg" data-original="./images/peiqi_l.jpg" class="img-reponsive col-md-6 viewer"/>
            <img src="./images/qiaozhi_s.jpg" data-original="./images/qiaozhi_l.jpg"  class="img-reponsive col-md-6 viewer"/>
        </div>
        
    </div>
    
    <script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.js"></script>
    <script src="../dist/bootstrap.viewer.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $('.viewer').bootstrapViewer({
                src: "data-original"
            });//The default image source file path is the src attribute of the img tag.
        })
    </script>
</body>
</html>

注意点

1. 在引入外部文件的时候的顺序:css文件在head中引入,js文件的引入请按照代码中的顺序;

2. 32-38行中的作用是:

    *  找class中有viewer的元素,附加上bootstrapViewer方法,即点击图片放大。

    *  35行的作用时,点击时放大的图片从元素的data-original属性中取,如果不传这个参数,则src取;

 

后话

刚开始在网上找的都是viewer.js的用法,奈何麻烦,代码又多。

况且我用的是bootstrap框架,所以用bootstrap-viewer库啦。

但是呢,用法不全面,最后经过探索,发现可以定制显示用的属性。

于是fork了仓库,添加了例子,以方便后来人。