|
| 1 | +### This is an excellent lightbox. It is modeled on the jQuery version of the lightbox2. It has a great user experience. :100: |
| 2 | +## 安装 :rocket: |
| 3 | +`npm install rc-lightbox --save` |
| 4 | + |
| 5 | +> tips: 如果你的项目中没有安装 `rc-tween-one` 需要安装一下。 |
| 6 | +## 用法 |
| 7 | + |
| 8 | +`import Lightbox from 'rc-lightbox'` |
| 9 | +``` |
| 10 | +<Lightbox |
| 11 | + imgSource={this.state.lightboxImgData} |
| 12 | + isOpen={this.state.lightboxIsOpen} |
| 13 | + currentImage = {this.state.currentImage} |
| 14 | + onClose={this.closeLightBox} |
| 15 | +/> |
| 16 | +``` |
| 17 | +### 参数说明 :art: |
| 18 | +属性|类型|默认值|说明 |
| 19 | +-|-|-|- |
| 20 | +isOpen|布尔值{Boolean}|false|设置lightbox的状态 |
| 21 | +imgSource|数组{Array}|[]| [{src:'http://'}] |
| 22 | +currentImage|布尔值{Number}|0 | 当前第几张图片 |
| 23 | +onClose|函数{Function}|-|关闭lightbox |
| 24 | + |
| 25 | +#### 示例代码 :tada: |
| 26 | +``` |
| 27 | +import React, { Component, PropTypes } from 'react' |
| 28 | +import { findDOMNode } from 'react-dom' |
| 29 | +import Lightbox from 'rc-lightbox' |
| 30 | +
|
| 31 | +
|
| 32 | +const MockData = [{ |
| 33 | + src:'./src/images/01.jpg', |
| 34 | + title:'图片1', |
| 35 | +},{ |
| 36 | + src:'./src/images/02.jpg', |
| 37 | + title:'图片2', |
| 38 | +},{ |
| 39 | + src:'https://gss0.bdstatic.com/5eR1dDebRNRTm2_p8IuM_a/res/r/image/2016-09-02/9577e9be05aea818907880ac66bdf4a0.jpg' |
| 40 | +},{ |
| 41 | + src:'https://gss0.bdstatic.com/5eR1dDebRNRTm2_p8IuM_a/res/r/image/2016-09-02/177e848bf4f0df538576f5422029c3e6.jpg' |
| 42 | +}] |
| 43 | +
|
| 44 | +const MockData2 = [{ |
| 45 | + src:'http://www.people.com.cn/NMediaFile/2016/0902/MAIN201609021911314859050338728.jpg', |
| 46 | + title:'图片1', |
| 47 | +},{ |
| 48 | + src:'http://www.people.com.cn/NMediaFile/2016/0902/MAIN201609021348521914980960649.jpg', |
| 49 | + title:'图片2', |
| 50 | +},{ |
| 51 | + src:'https://gss0.bdstatic.com/5eR1dDebRNRTm2_p8IuM_a/res/r/image/2016-09-02/9577e9be05aea818907880ac66bdf4a0.jpg' |
| 52 | +},{ |
| 53 | + src:'https://gss0.bdstatic.com/5eR1dDebRNRTm2_p8IuM_a/res/r/image/2016-09-02/177e848bf4f0df538576f5422029c3e6.jpg' |
| 54 | +},{ |
| 55 | + src:'http://www.wallcoo.com/animal/v195_Lively_Dogs/wallpapers/1280x800/Lively_Dogs_wallpaper_MIX88041_wallcoo.com.jpg' |
| 56 | +},{ |
| 57 | + src:'http://www.wallcoo.com/animal/v195_Lively_Dogs/wallpapers/1280x800/Lively_Dogs_wallpaper_MIX88041_wallcoo.com2.jpg' |
| 58 | +}] |
| 59 | +
|
| 60 | +
|
| 61 | +
|
| 62 | +export default class Lightbox2 extends Component{ |
| 63 | +
|
| 64 | + constructor(props, context) { |
| 65 | + super(props, context) |
| 66 | + this.state = { |
| 67 | + lightboxImgData:[], |
| 68 | + lightboxIsOpen:false, |
| 69 | + currentImage:0, |
| 70 | +
|
| 71 | + } |
| 72 | + } |
| 73 | +
|
| 74 | + componentDidMount = ()=>{ |
| 75 | +
|
| 76 | + //findDOMNode().appendChild('<div></div>') |
| 77 | + //document.body.appendChild(findDOMNode(this.refs.mask)) |
| 78 | + } |
| 79 | +
|
| 80 | + closeLightBox=()=>{ |
| 81 | + this.setState({ |
| 82 | + lightboxIsOpen:false |
| 83 | + }) |
| 84 | + } |
| 85 | +
|
| 86 | + showPicModal(imgGrounp){ |
| 87 | + this.setState({ |
| 88 | + lightboxIsOpen:true, |
| 89 | + currentImage:imgGrounp.index || 0, |
| 90 | + lightboxImgData:imgGrounp.imgs, |
| 91 | + }) |
| 92 | + } |
| 93 | +
|
| 94 | + renderImgList = (data)=>{ |
| 95 | + return ( |
| 96 | + data.map((item,index)=>{ |
| 97 | + return ( |
| 98 | + <li |
| 99 | + style={{ |
| 100 | + WebkitTransform: `opacity:${x}`, |
| 101 | + transform: `opacity:${x}`, |
| 102 | + }} |
| 103 | + key={`img_${index}`} |
| 104 | + onClick={this.showPicModal.bind(this,{imgs:data,index})} |
| 105 | + > |
| 106 | + <img src={item.src} /> |
| 107 | + </li> |
| 108 | + ) |
| 109 | + }) |
| 110 | + ) |
| 111 | + } |
| 112 | +
|
| 113 | +
|
| 114 | +
|
| 115 | + render() { |
| 116 | +
|
| 117 | +
|
| 118 | + return ( |
| 119 | + <div className="self-calendar"> |
| 120 | + <button |
| 121 | + onMouseDown={this.handleMouseDown} |
| 122 | + onTouchStart={this.handleTouchStart}> |
| 123 | + Toggle |
| 124 | + </button> |
| 125 | + <ul className="img-list clearfix"> |
| 126 | + { this.renderImgList(MockData) } |
| 127 | + </ul> |
| 128 | + <h2>第二组图片</h2> |
| 129 | + <ul className="img-list clearfix"> |
| 130 | + { this.renderImgList(MockData) } |
| 131 | + </ul> |
| 132 | + <h2>第三组图片</h2> |
| 133 | + <ul className="img-list clearfix"> |
| 134 | + { this.renderImgList(MockData2) } |
| 135 | + </ul> |
| 136 | +
|
| 137 | + { |
| 138 | + this.state.lightboxImgData.length? |
| 139 | + <Lightbox |
| 140 | + imgSource={this.state.lightboxImgData} |
| 141 | + isOpen={this.state.lightboxIsOpen} |
| 142 | + currentImage = {this.state.currentImage} |
| 143 | + onClose={this.closeLightBox} |
| 144 | + /> : null |
| 145 | + } |
| 146 | +
|
| 147 | +
|
| 148 | + </div> |
| 149 | + ); |
| 150 | + } |
| 151 | +} |
| 152 | +``` |
| 153 | +#### DEMO |
| 154 | + |
| 155 | + |
0 commit comments