This commit is contained in:
2021-05-16 12:31:41 +08:00
parent 52138c2f83
commit f6076e0e32
846 changed files with 89 additions and 16811 deletions
+44
View File
@@ -0,0 +1,44 @@
import React from 'react';
import Swiper from './components/swiper';
import "./assets/css/app.css";
class App extends React.Component{
constructor(){
super();
this.state={
images:[],
images2:[]
};
}
//模拟请求数据
componentDidMount(){
setTimeout(()=>{
let images=[
{src:require("./assets/images/banner1.jpg"),url:"https://pages.tmall.com/wow/a/act/tmall/tmc/23759/wupr?spm=875.7931836/B.2016006.d6.66144265aPX4zF&trackInfo=20160815100101;64866506034;287638;587431337429;3;287638_587431337429;1007.14152.68669.100200300000000;92ba02ea-a2a8-4bbe-99ac-d5c3abf2eba5;3;0;10000002&item_id=587431337429&pvid=92ba02ea-a2a8-4bbe-99ac-d5c3abf2eba5&pos=3&activity_id=287638&wh_pid=industry-170400&acm=07055.1003.1.2519102&scm=1003.1.20160815.OTHER_0_6801489"},
{src:require("./assets/images/banner2.jpg"),url:"https://pages.tmall.com/wow/a/act/tmall/tmc/23759/wupr?spm=875.7931836/B.2016006.d1.66144265aPX4zF&trackInfo=20160815100101;64950842484;288994;530549532317;3;288994_530549532317;1007.14152.68669.100200300000000;92ba02ea-a2a8-4bbe-99ac-d5c3abf2eba5;1;0;10000002&item_id=530549532317&pvid=92ba02ea-a2a8-4bbe-99ac-d5c3abf2eba5&pos=1&activity_id=288994&wh_pid=industry-170636&acm=07055.1003.1.2519102&scm=1003.1.20160815.OTHER_0_6810849"},
{src:require("./assets/images/banner3.jpg"),url:"https://www.tmall.com/wow/brand/act/fashion?acm=lb-zebra-2386-265936.1003.4.410386&scm=1003.4.lb-zebra-2386-265936.OTHER_1_410386&ali_trackid=19_4803ba43894904cab9c8c08820f2e4a5&spm=875.7931836/B.2016006.d2"}
];
this.setState({images:images});
let images2=[
{src:require("./assets/images/banner2.jpg"),url:"https://pages.tmall.com/wow/a/act/tmall/tmc/23759/wupr?spm=875.7931836/B.2016006.d6.66144265aPX4zF&trackInfo=20160815100101;64866506034;287638;587431337429;3;287638_587431337429;1007.14152.68669.100200300000000;92ba02ea-a2a8-4bbe-99ac-d5c3abf2eba5;3;0;10000002&item_id=587431337429&pvid=92ba02ea-a2a8-4bbe-99ac-d5c3abf2eba5&pos=3&activity_id=287638&wh_pid=industry-170400&acm=07055.1003.1.2519102&scm=1003.1.20160815.OTHER_0_6801489"},
{src:require("./assets/images/banner3.jpg"),url:"https://pages.tmall.com/wow/a/act/tmall/tmc/23759/wupr?spm=875.7931836/B.2016006.d1.66144265aPX4zF&trackInfo=20160815100101;64950842484;288994;530549532317;3;288994_530549532317;1007.14152.68669.100200300000000;92ba02ea-a2a8-4bbe-99ac-d5c3abf2eba5;1;0;10000002&item_id=530549532317&pvid=92ba02ea-a2a8-4bbe-99ac-d5c3abf2eba5&pos=1&activity_id=288994&wh_pid=industry-170636&acm=07055.1003.1.2519102&scm=1003.1.20160815.OTHER_0_6810849"},
{src:require("./assets/images/banner1.jpg"),url:"https://www.tmall.com/wow/brand/act/fashion?acm=lb-zebra-2386-265936.1003.4.410386&scm=1003.4.lb-zebra-2386-265936.OTHER_1_410386&ali_trackid=19_4803ba43894904cab9c8c08820f2e4a5&spm=875.7931836/B.2016006.d2"}
];
this.setState({images2:images2})
},400)
}
render(){
return (
<div className="App">
<div className="banner">
{/* props传值,父级向子级传值 */}
<Swiper data={this.state.images}></Swiper>
{/* <Swiper data={this.state.images2}></Swiper> */}
</div>
</div>
)
}
}
export default App;
+1
View File
@@ -0,0 +1 @@
.banner{width:800px;height:350px;}
@@ -0,0 +1,10 @@
html,body {
margin: 0;
padding:0;
font-size:16px;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen",
"Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue",
sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 204 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 53 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 140 KiB

+103
View File
@@ -0,0 +1,103 @@
import React from 'react';
import PropTypes from 'prop-types';
import "./style.css";
export default class Swiper extends React.Component{
constructor(){
super();
this.state={};
this.aData=[];
this.isInit=true;
this.timer=null;
this.index=0;
}
componentDidMount(){
this.autoPlay();
}
//自动播放
autoPlay(){
this.timer=setInterval(()=>{
if(this.aData && this.aData.length>0){
this.isInit=false;
for(let i=0;i<this.aData.length;i++){
if(this.aData[i].active){
this.aData[i].active=false;
break;
}
}
if(this.index>=this.aData.length-1){
this.index=0;
}else {
this.index++;
}
this.aData[this.index].active=true;
this.setState({});
}
},8800)
}
//停止自动播放
stop(){
clearInterval(this.timer);
}
//点击切换图片
changeImg(index){
this.isInit=false;
this.index=index;
if(this.aData && this.aData.length>0){
for(let i=0;i<this.aData.length;i++){
// if(this.aData[i].active){
// this.aData[i].active=false;
// break;
// }
this.aData[i].active=false;
}
this.aData[index].active=true;
this.setState({});
}
}
componentWillUnmount(){
//页面离开的时候清除定时器,解决性能问题
clearInterval(this.timer);
}
render(){
//接收父组件传过来的值
this.aData=this.props.data;
if(this.aData && this.aData.length>0 && this.isInit){
for(let i=0;i<this.aData.length;i++){
// 默认显示第一张
if(i===0){
this.aData[i].active=true;
}
//隐藏其他的
else {
this.aData[i].active = false;
}
}
}
return(
<div className="my-swiper-main" onMouseOver={this.stop.bind(this)} onMouseOut={this.autoPlay.bind(this)}>
{
(this.aData && this.aData.length>0) && this.aData.map((item,index)=>{
return (
<div className={item.active?"slide show":"slide"} key={index}>
<a href={item.url} target="_blank" rel="noopener noreferrer"><img src={item.src} alt=""/></a>
</div>
)
})
}
<div className="pagination">
{
(this.aData && this.aData.length>0) && this.aData.map((item,index)=>{
return (
<div className={item.active?"dot active":"dot"} key={index} onClick={this.changeImg.bind(this,index)}></div>
)
})
}
</div>
</div>
)
}
}
Swiper.propTypes={
data:PropTypes.array.isRequired
};
@@ -0,0 +1,41 @@
.my-swiper-main{
width:100%;
height:100%;
position: relative;
z-index:1;}
.my-swiper-main .slide{
width:100%;
height:100%;
position: absolute;
z-index:1;
left:0px;
top:0px;
opacity:0;
transition:opacity 0.5s;
}
.my-swiper-main .slide.show{
opacity: 1;
z-index: 2;
}
.my-swiper-main .slide img{
width:100%;
height:100%;
border:
0 none;
}
.my-swiper-main .pagination{
width:auto;
height:auto;
position: absolute;z-index: 3;
left:50%;bottom:8%;transform: translateX(-50%)}
.my-swiper-main .pagination .dot{
width:
30px;height:8px;
background-color:rgba(0,0,0,0.8);
cursor:pointer;
display: inline-block;
margin-left:2.5px;
margin-right:2.5px;}
.my-swiper-main .pagination .dot.active{
background-color:rgba(255,255,255,0.8)
}
+12
View File
@@ -0,0 +1,12 @@
import React from 'react';
import ReactDOM from 'react-dom';
import './assets/css/common/public.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
ReactDOM.render(<App />, document.getElementById('root'));
// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: https://bit.ly/CRA-PWA
serviceWorker.unregister();
+135
View File
@@ -0,0 +1,135 @@
// This optional code is used to register a service worker.
// register() is not called by default.
// This lets the app load faster on subsequent visits in production, and gives
// it offline capabilities. However, it also means that developers (and users)
// will only see deployed updates on subsequent visits to a page, after all the
// existing tabs open on the page have been closed, since previously cached
// resources are updated in the background.
// To learn more about the benefits of this model and instructions on how to
// opt-in, read https://bit.ly/CRA-PWA
const isLocalhost = Boolean(
window.location.hostname === 'localhost' ||
// [::1] is the IPv6 localhost address.
window.location.hostname === '[::1]' ||
// 127.0.0.1/8 is considered localhost for IPv4.
window.location.hostname.match(
/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
)
);
export function register(config) {
if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
// The URL constructor is available in all browsers that support SW.
const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href);
if (publicUrl.origin !== window.location.origin) {
// Our service worker won't work if PUBLIC_URL is on a different origin
// from what our page is served on. This might happen if a CDN is used to
// serve assets; see https://github.com/facebook/create-react-app/issues/2374
return;
}
window.addEventListener('load', () => {
const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
if (isLocalhost) {
// This is running on localhost. Let's check if a service worker still exists or not.
checkValidServiceWorker(swUrl, config);
// Add some additional logging to localhost, pointing developers to the
// service worker/PWA documentation.
navigator.serviceWorker.ready.then(() => {
console.log(
'This web app is being served cache-first by a service ' +
'worker. To learn more, visit https://bit.ly/CRA-PWA'
);
});
} else {
// Is not localhost. Just register service worker
registerValidSW(swUrl, config);
}
});
}
}
function registerValidSW(swUrl, config) {
navigator.serviceWorker
.register(swUrl)
.then(registration => {
registration.onupdatefound = () => {
const installingWorker = registration.installing;
if (installingWorker == null) {
return;
}
installingWorker.onstatechange = () => {
if (installingWorker.state === 'installed') {
if (navigator.serviceWorker.controller) {
// At this point, the updated precached content has been fetched,
// but the previous service worker will still serve the older
// content until all client tabs are closed.
console.log(
'New content is available and will be used when all ' +
'tabs for this page are closed. See https://bit.ly/CRA-PWA.'
);
// Execute callback
if (config && config.onUpdate) {
config.onUpdate(registration);
}
} else {
// At this point, everything has been precached.
// It's the perfect time to display a
// "Content is cached for offline use." message.
console.log('Content is cached for offline use.');
// Execute callback
if (config && config.onSuccess) {
config.onSuccess(registration);
}
}
}
};
};
})
.catch(error => {
console.error('Error during service worker registration:', error);
});
}
function checkValidServiceWorker(swUrl, config) {
// Check if the service worker can be found. If it can't reload the page.
fetch(swUrl)
.then(response => {
// Ensure service worker exists, and that we really are getting a JS file.
const contentType = response.headers.get('content-type');
if (
response.status === 404 ||
(contentType != null && contentType.indexOf('javascript') === -1)
) {
// No service worker found. Probably a different app. Reload the page.
navigator.serviceWorker.ready.then(registration => {
registration.unregister().then(() => {
window.location.reload();
});
});
} else {
// Service worker found. Proceed as normal.
registerValidSW(swUrl, config);
}
})
.catch(() => {
console.log(
'No internet connection found. App is running in offline mode.'
);
});
}
export function unregister() {
if ('serviceWorker' in navigator) {
navigator.serviceWorker.ready.then(registration => {
registration.unregister();
});
}
}