看见一些代码的产物,会觉得非常的漂亮感谢无私开源的程序员们你们是最可爱的人儿~~

  1. //index.jsx
  2. require('./app/lib/common.css');
  3. import React from 'react';
  4. import ReactDOM from 'react-dom';
  5. import Search from './app/components/search.jsx';
  6. import Header from './app/components/header.jsx';
  7. import Otherapp from './app/components/otherapp.jsx';
  8. import Spike from './app/components/spike.jsx';
  9. import More from './app/components/more.jsx';
  10. import Like from './app/components/like.jsx';
  11. ReactDOM.render(
  12. <div>
  13. <Search />
  14. <Header source="http://localhost:3000/data/swiper" />
  15. <Otherapp source="http://localhost:3000/data/otherapp" />
  16. <Spike source="http://localhost:3000/data/spike" />
  17. <More source="http://localhost:3000/data/more" />
  18. <Like source="http://localhost:3000/data/like" />
  19. </div>,
  20. document.querySelector("#myApp")
  21. );
  1. //index.html
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0, minimal-ui" />
  7. <title>JD_demo</title>
  8. <style>
  9. .bg {
  10. background: #f3f5f7;
  11. }
  12. </style>
  13. </head>
  14. <body class="bg">
  15. <div class="container">
  16. <div id="myApp"></div>
  17. </div>
  18. <script src="./bundle.js" type="text/javascript"></script>
  19. </body>
  20. </html>
  1. //封装的jsonp.js
  2. //app/util/jsonp.js
  3. ;(function () {
  4. /**
  5. * JSONP操作
  6. * @param url : 请求的url
  7. * @param data : 发送数据
  8. * @param jsonpcallback : 服务器给出的JSONP端口的API名称
  9. * @param callback : 执行JSONP获取数据的回调函数
  10. */
  11. var jsonp = function (url, data, jsonpcallback, callback) {
  12. var cbName = 'cb' + jsonp.count++;
  13. var callbackName = 'window.jsonp.' + cbName;
  14. window.jsonp[cbName] = function (jsonpData) {
  15. try {
  16. callback(jsonpData);
  17. } finally {
  18. script.parentNode.removeChild(script);
  19. delete window.jsonp[cbName];
  20. }
  21. };
  22. var script = document.createElement('script');
  23. if (data) {
  24. data = tool.encodeToURIString(data);
  25. }
  26. if (typeof jsonpcallback === 'string') {
  27. var jsonpData = jsonpcallback + '=' + callbackName;
  28. }
  29. url = tool.hasSearch(url, data);
  30. url = tool.hasSearch(url, jsonpData);
  31. script.src = url;
  32. document.body.appendChild(script);
  33. };
  34. jsonp.count = 0;
  35. window.jsonp = jsonp;
  36. var tool = {
  37. encodeToURIString: function (data) {
  38. if (!data) return '';
  39. if (typeof data === 'string') return data;
  40. var arr = [];
  41. for (var n in data) {
  42. if (!data.hasOwnProperty(n)) continue;
  43. arr.push(encodeURIComponent(n) + '=' + encodeURIComponent(data[n]));
  44. }
  45. return arr.join('&');
  46. },
  47. hasSearch: function (url, padString) {
  48. if (!padString) return url;
  49. if (typeof padString !== 'string') return url;
  50. return url + (/\?/.test(url) ? '&' : '?') + padString;
  51. }
  52. }
  53. })();
  54. module.exports = jsonp;

  1. //app/components/header.jsx
  2. require("./header.css");
  3. require('../lib/swiper.min.css');
  4. let Swiper = require('../lib/swiper.min.js');
  5. let jsonp = require('../util/jsonp.js');
  6. import React from 'react';
  7. let Header = React.createClass({
  8. getInitialState: function() {
  9. return {
  10. imgUrls: [],
  11. };
  12. },
  13. componentDidMount: function() {
  14. jsonp(this.props.source, "", "callback", (data) => {
  15. if(data.status) {
  16. //如果组件渲染到了 DOM 中,isMounted() 返回 true。
  17. //可以使用该方法保证 setState() 和 forceUpdate()
  18. //在异步场景下的调用不会出错。
  19. console.log('data.status.....',data.status);
  20. if(this.isMounted()) {
  21. this.setState({
  22. imgUrls: data.data,
  23. })
  24. new Swiper ('#header .swiper-container', {
  25. loop: true,
  26. pagination: '.swiper-pagination',
  27. paginationClickable: true,
  28. autoplay : 3000,
  29. autoplayDisableOnInteraction : false,
  30. })
  31. }
  32. }else {
  33. alert(data.msg);
  34. }
  35. });
  36. },
  37. render: function () {
  38. let countId = 0;
  39. return (
  40. <div id="header">
  41. <div className="swiper-container">
  42. <div className="swiper-wrapper">
  43. {
  44. this.state.imgUrls.map((url) => {
  45. return <div className="swiper-slide" key={"header" + countId++} >
  46. <img className="img" src={url} />
  47. </div>
  48. })
  49. }
  50. </div>
  51. <div className="swiper-pagination"></div>
  52. </div>
  53. </div>
  54. );
  55. }
  56. })
  57. module.exports = Header;

  1. //app/components/search.jsx
  2. require('./search.css');
  3. import React from 'react';
  4. let Search = React.createClass({
  5. getInitialState: function() {
  6. return {
  7. bg: "transparent",
  8. }
  9. },
  10. componentDidMount: function() {
  11. //向下滑动,搜索框固定不变,滚动一定距离,就改变背景色
  12. window.onscroll = (event) => {
  13. let realHeight = document.documentElement.scrollTop || document.body.scrollTop;
  14. let optatic = 0.8 * (realHeight/142);
  15. if(optatic <= 0.8 ) {
  16. this.setState({
  17. bg: `rgba(234, 44, 44, ${optatic})`,
  18. })
  19. }
  20. }
  21. },
  22. render: function() {
  23. let bColor = this.state.bg ? this.state.bg : 'transprent';
  24. return (
  25. <div id="search" className="pf" style={{ background: bColor }}>
  26. <div className="search pr">
  27. <div className="sl pa">
  28. <i></i>
  29. </div>
  30. <div className="frc pr">
  31. <span className="searchicon pa"></span>
  32. <form>
  33. <input placeholder="全场畅饮,部分低至99减50" type="text"/>
  34. </form>
  35. </div>
  36. <div className="sub pa">
  37. <span>登录</span>
  38. </div>
  39. </div>
  40. </div>
  41. );
  42. }
  43. })
  44. module.exports = Search;

  1. //app/components/like.jsx
  2. require('./like.css');
  3. let jsonp = require('../util/jsonp.js');
  4. import React from 'react';
  5. let Like = React.createClass({
  6. getInitialState: function() {
  7. return {
  8. stores: [],
  9. }
  10. },
  11. componentDidMount: function() {
  12. jsonp(this.props.source, "", "callback", (data) => {
  13. console.log('aaaaa',data);
  14. if(data.status) {
  15. if(this.isMounted()) {
  16. this.setState({
  17. stores: data.data,
  18. });
  19. }
  20. }else {
  21. alert(data.msg);
  22. reject("get data error!")
  23. }
  24. })
  25. },
  26. render: function() {
  27. let countId = 0;
  28. return (
  29. <div id="like">
  30. <p>猜你喜欢</p>
  31. {
  32. this.state.stores.map((item) => {
  33. return <div className="like_content" key={"like" + countId++}>
  34. <div className="like_link">
  35. <a href={ item.url }>
  36. <img src={ item.icon } alt=""/>
  37. </a>
  38. </div>
  39. <div className="like_desc">
  40. <span>
  41. { item.desc }
  42. </span>
  43. </div>
  44. <div className="like_price">
  45. <span>¥{ item.price }</span>
  46. <div><a href={ item.more }>看相似</a></div>
  47. </div>
  48. </div>
  49. })
  50. }
  51. </div>
  52. );
  53. }
  54. })
  55. module.exports = Like;

  1. //app/components/more.jsx
  2. require('./more.css');
  3. require('../lib/swiper.min.css');
  4. let Swiper = require('../lib/swiper.min.js');
  5. let jsonp = require('../util/jsonp.js');
  6. import React from 'react';
  7. var More = React.createClass({
  8. getInitialState: function() {
  9. return {
  10. more1: [],
  11. more2: [],
  12. more3: [],
  13. };
  14. },
  15. componentDidMount: function() {
  16. jsonp(this.props.source, "", "callback", (data) => {
  17. console.log('~~~~~~data',data);
  18. if(data.status) {
  19. // 将值分成了三部分,进行处理
  20. if(this.isMounted()) {
  21. this.setState({
  22. more1: data.data.slice(0,3),
  23. more2: data.data.slice(3,5),
  24. more3: data.data.slice(5,7),
  25. })
  26. new Swiper ('.more_bottom .swiper-container', {
  27. loop: true,
  28. pagination: '.swiper-pagination',
  29. paginationClickable: true,
  30. autoplay : 2000,
  31. autoplayDisableOnInteraction : false,
  32. })
  33. }
  34. }else {
  35. alert(data.msg);
  36. }
  37. });
  38. },
  39. render: function() {
  40. let countId = 0;
  41. return (
  42. <div id="more">
  43. <div className="more_top">
  44. {
  45. this.state.more1.map((item) => {
  46. return <div className="more_link" key={"more" + countId++}>
  47. <a href={item.url}>
  48. <img src={item.icon} alt=""/>
  49. </a>
  50. </div>
  51. })
  52. }
  53. </div>
  54. <div className="more_middle">
  55. {
  56. this.state.more2.map((item) => {
  57. return <div className="more_style" key={"more" + countId++}>
  58. <a href={item.url}>
  59. <img src={item.icon} alt=""/>
  60. </a>
  61. </div>
  62. })
  63. }
  64. </div>
  65. <div className="more_bottom">
  66. <div className="swiper-container">
  67. <div className="swiper-wrapper">
  68. {
  69. this.state.more3.map((item) => {
  70. return <div className="swiper-slide" key={"more" + countId++}>
  71. <a href={item.url}>
  72. <img src={item.icon} alt=""/>
  73. </a>
  74. </div>
  75. })
  76. }
  77. </div>
  78. <div className="swiper-pagination"></div>
  79. </div>
  80. </div>
  81. </div>
  82. );
  83. }
  84. })
  85. module.exports = More;

  1. //app/components/otherapp.jsx
  2. require('./otherapp.css');
  3. let jsonp = require('../util/jsonp.js');
  4. import React from 'react';
  5. let Otherapp = React.createClass({
  6. getInitialState: function() {
  7. return {
  8. apps: [],
  9. };
  10. },
  11. componentDidMount: function() {
  12. jsonp(this.props.source, "", "callback", (data) => {
  13. console.log('otherapp',data);
  14. if(data.status) {
  15. if(this.isMounted()) {
  16. this.setState({
  17. apps: data.data,
  18. })
  19. }
  20. }else {
  21. alert(data.msg);
  22. }
  23. });
  24. },
  25. render: function() {
  26. let countId = 0;
  27. return (
  28. <div className="oapp">
  29. <ul>
  30. {
  31. this.state.apps.map((app) => {
  32. return <li key={ "otherapp" + countId++ }>
  33. <a href={ app.url }>
  34. <div className="app_icon">
  35. <img src={ app.icon } alt=""/>
  36. </div>
  37. <span>{ app.title }</span>
  38. </a>
  39. </li>
  40. })
  41. }
  42. </ul>
  43. </div>
  44. );
  45. }
  46. })
  47. module.exports = Otherapp;
  1. //app/components/spike.jsx
  2. require('./spike.css');
  3. let jsonp = require('../util/jsonp.js');
  4. import React from 'react';
  5. let Spike = React.createClass({
  6. getInitialState: function() {
  7. return {
  8. hour: "00",
  9. minutes: "00",
  10. second: "00",
  11. stores: [],
  12. more: ""
  13. }
  14. },
  15. formatTime: function(times=0) {
  16. times = +times;
  17. let hour = 0,
  18. minutes = 0,
  19. second = 0,
  20. regTwo = /^\d{2}$/,
  21. regInteger = /^(\d{1,2})\.?\d*$/;
  22. if(times/3600 >= 1) {
  23. hour = times/3600;
  24. hour = +regInteger.exec(hour.toString())[1]
  25. times -= hour*3600;
  26. hour = regTwo.test(hour.toString()) ? hour.toString() : `0${hour}`;
  27. }
  28. if(times/60 >= 1) {
  29. minutes = times/60;
  30. minutes = +regInteger.exec(minutes.toString())[1]
  31. times -= minutes*60;
  32. minutes = regTwo.test(minutes.toString()) ? minutes.toString() : `0${minutes}`;
  33. }
  34. second = times;
  35. second = regTwo.test(second.toString()) ? second.toString() : `0${second}`;
  36. return {
  37. hour: hour,
  38. minutes: minutes,
  39. second: second,
  40. }
  41. },
  42. componentDidMount: function() {
  43. let getData = () => {
  44. let promise = new Promise((resolve, reject) => {
  45. jsonp(this.props.source, "", "callback", (data) => {
  46. console.log('seeedata....',data);
  47. if(data.status) {
  48. if(this.isMounted()) {
  49. this.setState({
  50. stores: data.data,
  51. more: data.more,
  52. });
  53. resolve(data.times);
  54. }
  55. }else {
  56. alert(data.msg);
  57. reject("get data error!")
  58. }
  59. })
  60. })
  61. return promise;
  62. }
  63. getData().then((times) => {
  64. times = +times;
  65. let timer = window.setInterval(() => {
  66. let {hour, minutes, second} = this.formatTime(times--);
  67. if(times == -1) {
  68. clearInterval(timer);
  69. timer = null;
  70. }
  71. this.setState({
  72. hour: hour,
  73. minutes: minutes,
  74. second: second,
  75. });
  76. }, 1000);
  77. }, (err) => {
  78. alert(err);
  79. });
  80. },
  81. render: function() {
  82. let countId = 0;
  83. return (
  84. <div id="spike">
  85. <div className="spike_header">
  86. <i></i>
  87. <span className="spike_title">掌上时间</span>
  88. <div className="spike_time">
  89. {
  90. (() => {
  91. return <div>
  92. <span>{this.state.hour}</span>:<span>{this.state.minutes}</span>:<span>{this.state.second}</span>
  93. </div>
  94. })()
  95. }
  96. </div>
  97. <div className="spike_more fr">
  98. <i className="fr"></i>
  99. <a href={this.state.more}>
  100. <span>更多秒杀</span>
  101. </a>
  102. </div>
  103. <div style={{clear:"both"}}></div>
  104. </div>
  105. <ul className="spike_content">
  106. {
  107. this.state.stores.map((item) => {
  108. return <li key={"spike" + countId++}>
  109. <a href={item.url}>
  110. <div>
  111. <img src={item.icon} alt=""/>
  112. </div>
  113. <p>¥{item.sprice}</p>
  114. <p className="real-price">¥{item.price}</p>
  115. </a>
  116. </li>
  117. })
  118. }
  119. </ul>
  120. </div>
  121. );
  122. }
  123. })
  124. module.exports = Spike;

感谢无私开源的程序员哟,可以点击项目的哟

react-jd-index的更多相关文章

  1. react拷贝index.html很恶心之解决办法

    https://www.npmjs.com/package/html-webpack-plugin

  2. React入门最好的学习实例-TodoList

    前言 React 的核心思想是:封装组件,各个组件维护自己的状态和 UI,当状态变更,自动重新渲染整个组件. 最近前端界闹的沸沸扬扬的技术当属react了,加上项目需要等等原因,自己也决定花些时间来好 ...

  3. 浅谈 原生javaScript&&react 实现全局触摸按钮(附带对addeventlistener的了解)

    1.采用原生javaACript 实现全局触摸按钮 首先在控制台输出,观察事件有哪些关于触摸的字段可以使用,然后拿这些字段的数据开始来写方法. 因为要做的是全局触摸按钮,我需要拿到的是按钮时时的坐标位 ...

  4. React Native + Nodejs 使用RSA加密登录

    想用rn做个RSA(非对称加密)登录 基本流程就是在服务端生成RSA后,将“公钥”发到客户端,然后客户端用“公钥”加密信息发送到服务端,服务务端用私钥解密. 过程不复杂,问题在于,nodejs和rn都 ...

  5. React 入门最好的实例-TodoList

    React 的核心思想是:封装组件,各个组件维护自己的状态和 UI,当状态变更,自动重新渲染整个组件. 最近前端界闹的沸沸扬扬的技术当属react了,加上项目需要等等原因,自己也决定花些时间来好好认识 ...

  6. React 组件开发初探

    react.js 在线地址:http://slides.com/yueyao/deck/#/ COMPONENT JSX 预编译语言, 一个基于ECMAscript 的xml-link 的语法扩展,最 ...

  7. React-TodoList

    React入门最好的学习实例-TodoList 前言 React 的核心思想是:封装组件,各个组件维护自己的状态和 UI,当状态变更,自动重新渲染整个组件. 最近前端界闹的沸沸扬扬的技术当属react ...

  8. react中文API解读二(教程)

    记下自己的react学习之路 ,官方文档写的很详尽,学起来应该比较简单 官方文档地址:react.http://reactjs.cn/react/docs/getting-started.html 2 ...

  9. React 系列文章(1): npm 手动搭建React 运行实例 (新手必看)

    摘 要 刚接触React 开发, 在摸索中构建react 运行环境,总会遇到各种坑:本文,将用最短时间解决webpack+react 环境搭建问题. 1.如果你还没有React基础 看这里. 2.如果 ...

  10. React 虚拟 DOM 的差异检测机制

    React 使用虚拟 DOM 将计算好之后的更新发送到真实的 DOM 树上,减少了频繁操作真实 DOM 的时间消耗,但将成本转移到了 JavaScript 中,因为要计算新旧 DOM 树的差异嘛.所以 ...

随机推荐

  1. ArcGIS Data Interoperability 的使用(1)

    今天在用OneMap的时候,发现OneMap中注册过后的WFS服务无法在skyline中加载,于是想知道OneMap注册后的WFS服务与server中的原生态WFS服务有啥区别.首先想到是否能在Arc ...

  2. 关于ie11的浏览器检测

    我的电脑昨天更新的时候把ie11给更新出来了,然后发现我的skylineweb项目提示我的浏览器不是ie,这样显然是浏览器检测出现了问题.查找后找到了下面的解决方法.大家的电脑如果也更新成了ie11的 ...

  3. [C#] double指定有效位数格式化

    C#里面指定小数位数格式化大家都知道 ff.ToString("F3") 可以指定精确到三位小数. 但是如何指定有效位数呢?方法是 ff.ToString("G3&quo ...

  4. 取消 ios 上下滑动

  5. jquery同级遍历

    siblings() 返回被选元素的所有同胞元素. next() 返回被选元素的下一个同胞元素. nextAll() 方法返回被选元素的所有跟随的同胞元素. nextUntil() 方法返回介于两个给 ...

  6. SVN 提交时文件锁定 svn: E155004: '' is already locked

    1.先安装TortoiseSVN TortoiseSVN安装成功后,找到工作路径下的项目右键 TortoiseSVN --> Clean up... --> Break locks 勾选上 ...

  7. Leetcode228. Summary Ranges汇总区间

    给定一个无重复元素的有序整数数组,返回数组区间范围的汇总. 示例 1: 输入: [0,1,2,4,5,7] 输出: ["0->2","4->5",& ...

  8. Web前端开发工程师需要掌握哪些核心技能?

    Web前端开发所涉及的内容主要包括W3C标准中的结构.行为和表现,那么这三项中我们需要掌握的核心技能是什么呢? 1.开发语言 HTML发展历史有二十多年,历经多次版本更新,HTML5和CSS3的出现又 ...

  9. Python各种转义符

    文章来源:https://www.cnblogs.com/luckyplj/p/9792658.html 谢谢作者:雨后观山色

  10. Linux下安装docker,更改镜像仓库地址,并部署springboot应用

    今天做不成的事,明天也不会做好. 各位同学大家好,随着docker的快速发展,越来越多的人开始使用,一方面随着容器化这个趋势越来越火,docker成为了其中的佼佼者:二来容器化确实降低了运维的门槛,让 ...