Grid 九宫格

在水平和垂直方向,将布局切分成若干等大的区块。

规则

  • 区块中的内容应该是同类元素,eg:都是图片,或者都是图标+文字。

代码演示

基本

  1. import { Grid } from 'antd-mobile';
  2. const data1 = [
  3. {
  4. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  5. text: '名字',
  6. link: 'hehe',
  7. }, {
  8. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  9. text: '名字',
  10. link: 'hehe',
  11. }, {
  12. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  13. text: '名字',
  14. link: 'hehe',
  15. }, {
  16. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  17. text: '名字',
  18. link: 'hehe',
  19. }, {
  20. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  21. text: '名字',
  22. link: 'hehe',
  23. }, {
  24. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  25. text: '名字',
  26. link: 'hehe',
  27. }, {
  28. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  29. text: '名字',
  30. link: 'hehe',
  31. }, {
  32. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  33. text: '名字',
  34. link: 'hehe',
  35. }, {
  36. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  37. text: '名字',
  38. link: 'hehe',
  39. }, {
  40. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  41. text: '名字',
  42. link: 'hehe',
  43. },
  44. ];
  45. const GridExample = React.createClass({
  46. render() {
  47. return (<div>
  48. <Grid data={data1} />
  49. </div>);
  50. },
  51. });
  52. ReactDOM.render(<GridExample />, mountNode);

没有边线

  1. import { Grid } from 'antd-mobile';
  2. const data2 = [
  3. {
  4. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  5. text: '名字',
  6. link: 'hehe',
  7. }, {
  8. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  9. text: '名字',
  10. link: 'hehe',
  11. }, {
  12. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  13. text: '名字',
  14. link: 'hehe',
  15. }, {
  16. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  17. text: '名字',
  18. link: 'hehe',
  19. }, {
  20. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  21. text: '名字',
  22. link: 'hehe',
  23. }, {
  24. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  25. text: '名字',
  26. link: 'hehe',
  27. }, {
  28. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  29. text: '名字',
  30. link: 'hehe',
  31. }, {
  32. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  33. text: '名字',
  34. link: 'hehe',
  35. }, {
  36. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  37. text: '名字',
  38. link: 'hehe',
  39. }, {
  40. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  41. text: '名字',
  42. link: 'hehe',
  43. },
  44. ];
  45. const GridExample = React.createClass({
  46. render() {
  47. return (<div>
  48. <Grid
  49. data={data2}
  50. hasLine={false}
  51. columnNum={3}
  52. />
  53. </div>);
  54. },
  55. });
  56. ReactDOM.render(<GridExample />, mountNode);

走马灯有边线

  1. import { Grid } from 'antd-mobile';
  2. const data3 = [
  3. {
  4. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  5. text: '名字',
  6. link: 'hehe',
  7. }, {
  8. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  9. text: '名字',
  10. link: 'hehe',
  11. }, {
  12. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  13. text: '名字',
  14. link: 'hehe',
  15. }, {
  16. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  17. text: '名字',
  18. link: 'hehe',
  19. }, {
  20. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  21. text: '名字',
  22. link: 'hehe',
  23. }, {
  24. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  25. text: '名字',
  26. link: 'hehe',
  27. }, {
  28. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  29. text: '名字',
  30. link: 'hehe',
  31. }, {
  32. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  33. text: '名字',
  34. link: 'hehe',
  35. }, {
  36. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  37. text: '名字',
  38. link: 'hehe',
  39. }, {
  40. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  41. text: '名字',
  42. link: 'hehe',
  43. },
  44. ];
  45. const GridExample = React.createClass({
  46. render() {
  47. return (<div>
  48. <Grid
  49. data={data3}
  50. columnNum={3}
  51. carouselMaxRow={3}
  52. isCarousel
  53. onClick={(el, index) => { alert(index); }}
  54. />
  55. </div>);
  56. },
  57. });
  58. ReactDOM.render(<GridExample />, mountNode);

走马灯没有边线

  1. import { Grid } from 'antd-mobile';
  2. const data4 = [
  3. {
  4. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  5. text: '名字',
  6. link: 'hehe',
  7. }, {
  8. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  9. text: '名字',
  10. link: 'hehe',
  11. }, {
  12. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  13. text: '名字',
  14. link: 'hehe',
  15. }, {
  16. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  17. text: '名字',
  18. link: 'hehe',
  19. }, {
  20. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  21. text: '名字',
  22. link: 'hehe',
  23. }, {
  24. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  25. text: '名字',
  26. link: 'hehe',
  27. }, {
  28. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  29. text: '名字',
  30. link: 'hehe',
  31. }, {
  32. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  33. text: '名字',
  34. link: 'hehe',
  35. }, {
  36. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  37. text: '名字',
  38. link: 'hehe',
  39. }, {
  40. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  41. text: '名字',
  42. link: 'hehe',
  43. },
  44. ];
  45. const GridExample = React.createClass({
  46. render() {
  47. return (<div>
  48. <Grid
  49. data={data4}
  50. hasLine={false}
  51. isCarousel
  52. />
  53. </div>);
  54. },
  55. });
  56. ReactDOM.render(<GridExample />, mountNode);

自定义

  1. import { Grid } from 'antd-mobile';
  2. const data1 = [
  3. {
  4. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  5. img: 'https://zos.alipayobjects.com/rmsportal/wIjMDnsrDoPPcIV.png',
  6. text: '名字',
  7. link: 'hehe',
  8. }, {
  9. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  10. img: 'https://zos.alipayobjects.com/rmsportal/wIjMDnsrDoPPcIV.png',
  11. text: '名字',
  12. link: 'hehe',
  13. }, {
  14. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  15. img: 'https://zos.alipayobjects.com/rmsportal/wIjMDnsrDoPPcIV.png',
  16. text: '名字',
  17. link: 'hehe',
  18. }, {
  19. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  20. img: 'https://zos.alipayobjects.com/rmsportal/wIjMDnsrDoPPcIV.png',
  21. text: '名字',
  22. link: 'hehe',
  23. }, {
  24. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  25. img: 'https://zos.alipayobjects.com/rmsportal/wIjMDnsrDoPPcIV.png',
  26. text: '名字',
  27. link: 'hehe',
  28. },
  29. ];
  30. const GridExample = React.createClass({
  31. render() {
  32. return (<div>
  33. <Grid
  34. data={data1}
  35. columnNum={3}
  36. hasLine={false}
  37. renderItem={(dataItem, index) => (
  38. <div style={{ margin: '16px', background: '#f7f7f7', textAlign: 'center' }}>
  39. <div style={{ background: 'rgba(0, 0, 0, 0.1)', padding: '8px' }}>
  40. <span>{index + 1}.{dataItem.text}</span>
  41. </div>
  42. <img src={dataItem.img} style={{ width: '80%', margin: '12px' }} />
  43. </div>
  44. )}
  45. />
  46. </div>);
  47. },
  48. });
  49. ReactDOM.render(<GridExample />, mountNode);

Grid九宫格 - 图1

API

Grid

成员说明类型可选值默认值
data传入的菜单数据Array,每个元素必须包含icon,和text两个key[]
onClick点击每个菜单的回调Function(el, index)
columnNum列数number4
hasLine是否有边框booleantrue
isCarousel是否跑马灯,booleanfalse
carouselMaxRow如果是跑马灯, 一页跑马灯需要展示的行数number2
renderItem自定义每个grid的条目的创建Function(el, index), 返回一个React.Element