用vue来显示一组类轮播图片实战

vue-images

用一个简单的 lightbox组件来显示一组图片


特性

  • 适应手机

  • 显示带有缩略图导航

  • 响应式设计 适应各类屏幕

马上上手

  • 导入模块:

// Install using npm
npm install vue-images --save

// Include stylesheet
require('vue-images/dist/vue-images.css')

// In ES6 module
import vueImages from 'vue-images'
  • 导入script标签:
<link rel="stylesheet" href="../node-modules/vue-images/dist/vue-images.css" charset="utf-8">
<script src="../node-modules/vue-images/dist/vue-images.js"></script>
new Vue({
  el: '#app',
  data () {
    return {
      images: [...],
      ...
    }
  },
  components: {
    vueImages: vueImages.default
  }
})

属性值

属性 类型 默认值 描述
images array undefined Required. An array of objects containing valid src and srcset values of img element
modalclose bool true Allow users to exit the lightbox by clicking the backdrop
keyinput bool true Supports keyboard input - esc, ←, and →
mousescroll bool true Supports mouse scroll
showclosebutton bool true Optionally display a close X button in top right corner
showcaption bool true Optionally display a caption under the image
imagecountseparator string ' of ' Custom separator for the image count
showimagecount bool true Optionally display image index, e.g., "3 of 20"
showthumbnails bool false Optionally display thumbnails beneath the Lightbox

说明

本文为翻译https://littlewin-wang.github.io/vue-images/example/ 文章
欢迎加qq群610334712 交流vue的技术和下载完整的项目代码
附带部分核心代码

核心代码

html

<!DOCTYPE html>
<head>
  <meta charset="utf-8">
  <title>vue-images</title>
  <meta name="viewport" content="initial-scale=1.0,user-scalable=no,maximum-scale=1,width=device-width">
  <meta name="keywords" content="vue,vuejs,vue component,lightbox,vue lightbox,vue images,image,images,ui,javascript">
  <meta name="description" content="A simple, responsive Lightbox component for displaying an array of images.">
  <meta property="og:locale" content="zh-cn">
  <meta property="og:title" content="vue-images">
  <meta property="og:description" content="A simple, responsive Lightbox component for displaying an array of images.">
  <meta property="og:url" content="https://littlewin-wang.github.io/vue-images/example/">
  <meta property="og:site_name" content="vue-images">
  <meta property="og:type" content="article">
  <link rel="stylesheet" href="style.css">
  <link rel="stylesheet" href="../dist/vue-images.css">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<div class="page-wrapper">
  <div class="container">
    <nav class="left-col">
      <ul class="page-nav">
        <li class="page-nav__item">
          <a class="page-nav__link" href="#examples">Examples</a>
        </li>
        <li class="page-nav__item">
          <a class="page-nav__link" href="#getting-started">Getting started</a>
        </li>
        <li class="page-nav__item">
          <a class="page-nav__link" href="#options">Options</a>
        </li>
        <li class="page-nav__item">
          <a class="page-nav__link" href="#license">License</a>
        </li>
        <li class="page-nav__item">
          <a class="page-nav__link" href="#help">Help</a>
        </li>
      </ul>
    </nav>
    <div class="right-col">
      <div class="page-content">
        <header class="page-header">
          <h1 class="page-header__title">vue images</h1>
          <p class="page-header__subtitle">A simple, responsive Lightbox component for <a href="https://vuejs.org/" target="_blank">Vue.js</a> to display an array of images.</p>
        </header>
        <div class="page-subheader">
          <a href="https://github.com/littlewin-wang/vue-images" class="page-subheader__link" target="_blank">Code and Docs on GitHub</a>
                        <span class="page-subheader__button">
              <a class="github-button" id="github-stars-button" href="https://github.com/littlewin-wang/vue-images" data-icon="octicon-star" data-style="mega" data-count-href="/littlewin-wang/vue-images/stargazers" data-count-api="/repos/littlewin-wang/vue-images#stargazers_count" data-count-aria-label="# stargazers on GitHub" aria-label="Star littlewin-wang/vue-images on GitHub">Star</a>
                        </span>
        </div>
        <div class="page-body">
          <section id="examples" class="section-examples">
            <h2>Examples</h2>
            <div id="example">
              <div style="margin-bottom: 20px;">
                <p>Photos courtesy of <a href="https://unsplash.com/" target="_blank">Unsplash</a>.</p>                    <p>Support your keyboard to navigate <kbd>left</kbd> <kbd>right</kbd> <kbd>esc</kbd>. Support mouse scroll to navigate. Support mouse touch move to navigate.</p>
                <p>Also, try resizing your browser window.</p>
              </div>
              <div id="demo">
                <vue-images :imgs="images"
                            :modalclose="modalclose"
                            :keyinput="keyinput"
                            :mousescroll="mousescroll"
                            :showclosebutton="showclosebutton"
                            :showcaption="showcaption"
                            :imagecountseparator="imagecountseparator"
                            :showimagecount="showimagecount"
                            :showthumbnails="showthumbnails">
                </vue-images>
              </div>
            </div>
          </section>

          <section id="getting-started" class="section-getting-started">
            <h2>Getting Started</h2>
            <p>1. Import using module:</p>
            <pre>// Install using npm
npm install vue-images --save

// Include stylesheet
require('vue-images/dist/vue-images.css')

// In ES6 module
import vueImages from 'vue-images'</pre>
            <p>2. Import using script tag:</p>
                            <pre><code>&lt;link rel="stylesheet" href="../node-modules/vue-images/dist/vue-images.css" charset="utf-8"&gt;
&lt;script src="../node-modules/vue-images/dist/vue-images.js"&gt;&lt;/script&gt;</code></pre>

            <pre>
new Vue({
  el: '#demo',
  data () {
    return {
      images: [...],
      ...(other parameters)
    }
  },
  components: {
    vueImages: vueImages.default
  }
})</pre>

          </section>

          <section id="options" class="section-options">
            <h2>Options</h2>
            <div class="options-table-container">
              <table class="options-table">
                <thead>
                <tr>
                  <th align="left">Property</th>
                  <th align="left">Type</th>
                  <th align="left">Default</th>
                  <th align="left">Description</th>
                </tr>
                </thead>
                <tbody>
                <tr>
                  <td align="left"><a href="#images">images</a></td>
                  <td align="left">array</td>
                  <td align="left">undefined</td>
                  <td align="left">Required. An array of objects containing valid src and srcset values of img element</td>
                </tr>
                <tr>
                  <td align="left">modalclose</td>
                  <td align="left">bool</td>
                  <td align="left">true</td>
                  <td align="left">Allow users to exit the lightbox by clicking the backdrop</td>
                </tr>
                <tr>
                  <td align="left">keyinput</td>
                  <td align="left">bool</td>
                  <td align="left">true</td>
                  <td align="left">Supports keyboard input - <kbd>esc</kbd>, <kbd>←</kbd>, and <kbd>→</kbd></td>
                </tr>
                <tr>
                  <td align="left">mousescroll</td>
                  <td align="left">bool</td>
                  <td align="left">true</td>
                  <td align="left">Supports mouse scroll</td>
                </tr>
                <tr>
                  <td align="left">showclosebutton</td>
                  <td align="left">bool</td>
                  <td align="left">true</td>
                  <td align="left">Optionally display a close <kbd>X</kbd> button in top right corner</td>
                </tr>
                <tr>
                  <td align="left">showcaption</td>
                  <td align="left">bool</td>
                  <td align="left">true</td>
                  <td align="left">Optionally display a caption under the image</td>
                </tr>
                <tr>
                  <td align="left">imagecountseparator</td>
                  <td align="left">string</td>
                  <td align="left">' of '</td>
                  <td align="left">Custom separator for the image count</td>
                </tr>
                <tr>
                  <td align="left">showimagecount</td>
                  <td align="left">bool</td>
                  <td align="left">true</td>
                  <td align="left">Optionally display image index, e.g., "3 of 20"</td>
                </tr>
                <tr>
                  <td align="left">showthumbnails</td>
                  <td align="left">bool</td>
                  <td align="left">false</td>
                  <td align="left">Optionally display thumbnails beneath the Lightbox</td>
                </tr>
                </tbody>
              </table>
            </div>
            <h2>Images</h2>
            <div class="options-table-container" id="images">
              <table class="options-table">
                <thead>
                <tr>
                  <th align="left">Property</th>
                  <th align="left">Type</th>
                  <th align="left">Default</th>
                  <th align="left">Description</th>
                </tr>
                </thead>
                <tbody>
                <tr>
                  <td align="left">imageUrl</td>
                  <td align="left">string</td>
                  <td align="left">undefined</td>
                  <td align="left"><b>Required.</b> The primary image path</td>
                </tr>
                <tr>
                  <td align="left">caption</td>
                  <td align="left">string</td>
                  <td align="left">undefined</td>
                  <td align="left">Displayed beneath the current image. Great for description or attribution</td>
                </tr>
                </tbody>
              </table>
            </div>
          </section>

          <section id="license" class="section-license">
            <h2>License</h2>
            <p>vue images is free to use for personal and commercial projects under <a target="_blank" href="https://github.com/littlewin-wang/vue-images/blob/master/LICENSE">the MIT license</a>.</p>
            <p>Attribution is not required, but greatly appreciated. It does not have to be user-facing and can remain within the code.</p>
          </section>

          <section id="help" class="section-help">
            <h2>Help</h2>

            <h3>Have a question?</h3>
            <p>Follow the <a target="_blank" href="https://github.com/littlewin-wang/vue-images#start-guide">quick start guide</a> on GitHub to get up and running quickly. Please do not use Github Issues to report personal support requests.</p>

            <h3>Found a bug?</h3>
            <p>If you find a bug, please read the <a target="_blank" href="https://github.com/littlewin-wang/vue-images#change-log">Change Log</a> before you <a target="_blank" href="https://github.com/littlewin-wang/vue-images/issues">report the issue</a>.</p>
          </section>
        </div>
      </div>
    </div>
    <footer class="page-footer">
      <span class="page-footer__copyright--small">Copyright </span>
      <span class="page-footer__copyright--large">&copy; </span>
      <a target="_blank" href="https://github.com/littlewin-wang" target="_blank">Littlewin</a> 2017
    </footer>
  </div>
</div>
<script>
  document.getElementById('github-stars-button').dataset.style = window.innerWidth > 480 ? 'mega': null;
</script>
<script async defer id="github-bjs" src="https://buttons.github.io/buttons.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.9/vue.min.js"></script>
<script src="../dist/vue-images.js"></script>
<script src="index.js"></script>
</body>

js

new Vue({
  el: '#demo',
  data () {
    return {
      images: [
        {
          imageUrl: 'https://images.unsplash.com/photo-1454991727061-be514eae86f7?dpr=2&auto=format&w=1024',
          caption: 'Photo by 1'
        },
        {
          imageUrl: 'https://images.unsplash.com/photo-1455717974081-0436a066bb96?dpr=2&auto=format&w=1024',
          caption: 'Photo by 2'
        },
        {
          imageUrl: 'https://images.unsplash.com/photo-1460899960812-f6ee1ecaf117?dpr=2&auto=format&w=1024',
          caption: 'Photo by 3'
        },
        {
          imageUrl: 'https://images.unsplash.com/photo-1456926631375-92c8ce872def?dpr=2&auto=format&w=1024',
          caption: 'Photo by 4'
        },
        {
          imageUrl: 'https://images.unsplash.com/photo-1452274381522-521513015433?dpr=2&auto=format&w=1024',
          caption: 'Photo by 5'
        },
        {
          imageUrl: 'https://images.unsplash.com/photo-1471145653077-54c6f0aae511?dpr=2&auto=format&w=1024',
          caption: 'Photo by 6'
        },
        {
          imageUrl: 'https://images.unsplash.com/photo-1471005197911-88e9d4a7834d?dpr=2&auto=format&w=1024',
          caption: 'Photo by 7'
        },
        {
          imageUrl: 'https://images.unsplash.com/photo-1470583190240-bd6bbde8a569?dpr=2&auto=format&w=1024',
          caption: 'Photo by 8'
        },
        {
          imageUrl: 'https://images.unsplash.com/photo-1470688090067-6d429c0b2600?dpr=2&auto=format&w=1024',
          caption: 'Photo by 9'
        },
        {
          imageUrl: 'https://images.unsplash.com/photo-1470742292565-de43c4b02b57?dpr=2&auto=format&w=1024',
          caption: 'Photo by 10'
        }
      ],
      modalclose: true,
      keyinput: true,
      mousescroll: true,
      showclosebutton: true,
      showcaption: true,
      imagecountseparator: 'of',
      showimagecount: true,
      showthumbnails: true
    }
  },
  components: {
    vueImages: vueImages.default
  }
})
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,496评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,407评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,632评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,180评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,198评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,165评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,052评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,910评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,324评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,542评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,711评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,424评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,017评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,668评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,823评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,722评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,611评论 2 353

推荐阅读更多精彩内容

  • 转载 :OpenDiggawesome-github-vue 是由OpenDigg整理并维护的Vue相关开源项目库...
    果汁密码阅读 23,117评论 8 124
  • 来源:github.com Vue.js开源项目速查表:https://www.ctolib.com/cheats...
    zhangtaiwei阅读 11,614评论 1 159
  • 深巷总有把话闲,夜来雨落至。高楼瓦低事来杂,素手涤来茶冷人凉。灯红酒绿星光下,已是昨日辉煌。岁月如歌花颜失,梦里何...
    jack署晨阅读 212评论 0 0
  • 我让这个世界更无助 相互的黑暗纠缠 一切还在生长 它们看着我 钢铁也不冰冷 在夏天里灼热 我逃在阴影里 面对不了 ...
    540b05b1417c阅读 167评论 0 1
  • 蓝公主阅读 213评论 0 0