React开发的导航插件,轮播插件

本文介绍了一款使用React实现的导航插件和轮播插件,详细展示了插件的JavaScript代码及CSS样式,同时提供了配置文件示例。通过这些代码,读者可以了解如何在React应用中创建平滑过渡的导航菜单和自动轮播图片效果。

导航插件:

js

import React, { Component } from 'react';
import './List.css';
import listData from "./data/list.config.json";
class List extends Component{
constructor(props) {
super(props);
this.state = {
listitem: [],
left:0,
width:0
};

}
removeClass(domArr){
for(var i=0;i<domArr.length;i++){
domArr[i].classList.remove("selected");
}
}

addClass(dom,classname){
dom.classList.add(classname);
}
hoverdown(e){

var _target = e.target;
var index = _target.getAttribute("index");
var left = _target.offsetLeft;
var width = _target.offsetWidth;
var Nodes = _target.parentNode.childNodes;
this.removeClass(Nodes);
e.target.classList.add("selected");
this.setState({
listitem:listData[index],
left:left,
width:width,
checkclass:"slidedown"
});
}
hoverout(e){
this.setState({
checkclass:""
});
e.stopPropagation();
}
listOut(e){
this.setState({
checkclass:"hide"
});
e.stopPropagation();

}
// listOutbreak(e){
// this.setState({
// checkclass:"hide"
// });
// var _target = e.target;
// var href = window.location.href;
// href = href.substring(0,href.indexOf("/"));
// window.location.href =href +"app"
// e.stopPropagation();
// }
render(){
const ListData = listData.navdata;
var listItem = this.state.listitem;
var _this = this;
return (
<div className="header-wrap">
<ul className="navul">
{
ListData.map(function(value,key){
return (
<li className="lipadding" key={value.key} onMouseOver={(e) => _this.hoverdown(e)} onMouseOut={(e) => _this.hoverout(e)} index = {value.key}>{value.value}</li>
)
})
}
</ul>
<div className={"listitem-wrap" + " "+ _this.state.checkclass} style={{left:_this.state.left,width:_this.state.width}} onMouseLeave={(e)=>_this.listOut(e)}>
<ul>
{
listItem.map(function(value,key){
return (<li key={key} onClick={(e)=>_this.listOut(e)}>{value}</li>)
})
}
</ul>
</div>
</div>
)
}
}
export default List;

 

css

 

.navul{
list-style: none;
display: flex;
display: -webkit-flex;
flex-direction: row;
flex-wrap: nowrap;
justify-content: center;
font-size: 20px;
color: #010101;
background-color: #f2f2f2;
margin: 0;
padding: 0;
}
.navul li{
flex: 1;
}
.lipadding{
padding: 24px 10px;

}
.lipadding.selected{
color: #fff;
background-color: #666;
}
.header-wrap{
position: relative;
}
.listitem-wrap{
position: absolute;

z-index: 100;

}
.listitem-wrap ul{
list-style: none;
margin: 0;
padding: 0;
}
.listitem-wrap li{
box-sizing: border-box;
padding: 10px 5px;
padding-left: 20px;
text-align: left;
background-image: linear-gradient(rgba(0,0,0,0),rgba(0,0,0,.25));
}
.listitem-wrap li:hover{
color: #fff;
background-color: #888;
}
.slidedown,.hide{
-moz-transform-origin: 50% 0%;
-ms-transform-origin: 50% 0%;
-o-transform-origin: 50% 0%;
-webkit-transform-origin: 50% 0%;
transform-origin: 50% 0%;
}
.slidedown{
-moz-transform: scaleY(1);
-ms-transform: scaleY(1);
-o-transform: scaleY(1);
-webkit-transform: scaleY(1);
transform: scaleY(1);
animation: showAnimation 0.5s ease-in-out;
-moz-animation: showAnimation 0.5s ease-in-out;
-webkit-animation: showAnimation 0.5s ease-in-out;
-moz-transition: max-height 1s ease-in-out;
-o-transition: max-height 1s ease-in-out;
-webkit-transition: max-height 1s ease-in-out;
transition: max-height 1s ease-in-out;
}
.hide{
max-height: 0;
-moz-transform: scaleY(0);
-ms-transform: scaleY(0);
-o-transform: scaleY(0);
-webkit-transform: scaleY(0);
transform: scaleY(0);
animation: hideAnimation 0.4s ease-out;
-moz-animation: hideAnimation 0.4s ease-out;
-webkit-animation: hideAnimation 0.4s ease-out;
-moz-transition: max-height 0.6s ease-out;
-o-transition: max-height 0.6s ease-out;
-webkit-transition: max-height 0.6s ease-out;
transition: max-height 0.6s ease-out;
}
@keyframes showAnimation {
0% {
-moz-transform: scaleY(0.1);
-ms-transform: scaleY(0.1);
-o-transform: scaleY(0.1);
-webkit-transform: scaleY(0.1);
transform: scaleY(0.1);
}

40% {
-moz-transform: scaleY(1.04);
-ms-transform: scaleY(1.04);
-o-transform: scaleY(1.04);
-webkit-transform: scaleY(1.04);
transform: scaleY(1.04);
}

60% {
-moz-transform: scaleY(0.98);
-ms-transform: scaleY(0.98);
-o-transform: scaleY(0.98);
-webkit-transform: scaleY(0.98);
transform: scaleY(0.98);
}

80% {
-moz-transform: scaleY(1.04);
-ms-transform: scaleY(1.04);
-o-transform: scaleY(1.04);
-webkit-transform: scaleY(1.04);
transform: scaleY(1.04);
}

100% {
-moz-transform: scaleY(0.98);
-ms-transform: scaleY(0.98);
-o-transform: scaleY(0.98);
-webkit-transform: scaleY(0.98);
transform: scaleY(0.98);
}

80% {
-moz-transform: scaleY(1.02);
-ms-transform: scaleY(1.02);
-o-transform: scaleY(1.02);
-webkit-transform: scaleY(1.02);
transform: scaleY(1.02);
}

100% {
-moz-transform: scaleY(1);
-ms-transform: scaleY(1);
-o-transform: scaleY(1);
-webkit-transform: scaleY(1);
transform: scaleY(1);
}
}
@-moz-keyframes showAnimation {
0% {
-moz-transform: scaleY(0.1);
-ms-transform: scaleY(0.1);
-o-transform: scaleY(0.1);
-webkit-transform: scaleY(0.1);
transform: scaleY(0.1);
}

40% {
-moz-transform: scaleY(1.04);
-ms-transform: scaleY(1.04);
-o-transform: scaleY(1.04);
-webkit-transform: scaleY(1.04);
transform: scaleY(1.04);
}

60% {
-moz-transform: scaleY(0.98);
-ms-transform: scaleY(0.98);
-o-transform: scaleY(0.98);
-webkit-transform: scaleY(0.98);
transform: scaleY(0.98);
}

80% {
-moz-transform: scaleY(1.04);
-ms-transform: scaleY(1.04);
-o-transform: scaleY(1.04);
-webkit-transform: scaleY(1.04);
transform: scaleY(1.04);
}

100% {
-moz-transform: scaleY(0.98);
-ms-transform: scaleY(0.98);
-o-transform: scaleY(0.98);
-webkit-transform: scaleY(0.98);
transform: scaleY(0.98);
}

80% {
-moz-transform: scaleY(1.02);
-ms-transform: scaleY(1.02);
-o-transform: scaleY(1.02);
-webkit-transform: scaleY(1.02);
transform: scaleY(1.02);
}

100% {
-moz-transform: scaleY(1);
-ms-transform: scaleY(1);
-o-transform: scaleY(1);
-webkit-transform: scaleY(1);
transform: scaleY(1);
}
}
@-webkit-keyframes showAnimation {
0% {
-moz-transform: scaleY(0.1);
-ms-transform: scaleY(0.1);
-o-transform: scaleY(0.1);
-webkit-transform: scaleY(0.1);
transform: scaleY(0.1);
}

40% {
-moz-transform: scaleY(1.04);
-ms-transform: scaleY(1.04);
-o-transform: scaleY(1.04);
-webkit-transform: scaleY(1.04);
transform: scaleY(1.04);
}

60% {
-moz-transform: scaleY(0.98);
-ms-transform: scaleY(0.98);
-o-transform: scaleY(0.98);
-webkit-transform: scaleY(0.98);
transform: scaleY(0.98);
}

80% {
-moz-transform: scaleY(1.04);
-ms-transform: scaleY(1.04);
-o-transform: scaleY(1.04);
-webkit-transform: scaleY(1.04);
transform: scaleY(1.04);
}

100% {
-moz-transform: scaleY(0.98);
-ms-transform: scaleY(0.98);
-o-transform: scaleY(0.98);
-webkit-transform: scaleY(0.98);
transform: scaleY(0.98);
}

80% {
-moz-transform: scaleY(1.02);
-ms-transform: scaleY(1.02);
-o-transform: scaleY(1.02);
-webkit-transform: scaleY(1.02);
transform: scaleY(1.02);
}

100% {
-moz-transform: scaleY(1);
-ms-transform: scaleY(1);
-o-transform: scaleY(1);
-webkit-transform: scaleY(1);
transform: scaleY(1);
}
}
@keyframes hideAnimation {
0% {
-moz-transform: scaleY(1);
-ms-transform: scaleY(1);
-o-transform: scaleY(1);
-webkit-transform: scaleY(1);
transform: scaleY(1);
}

60% {
-moz-transform: scaleY(0.98);
-ms-transform: scaleY(0.98);
-o-transform: scaleY(0.98);
-webkit-transform: scaleY(0.98);
transform: scaleY(0.98);
}

80% {
-moz-transform: scaleY(1.02);
-ms-transform: scaleY(1.02);
-o-transform: scaleY(1.02);
-webkit-transform: scaleY(1.02);
transform: scaleY(1.02);
}

100% {
-moz-transform: scaleY(0);
-ms-transform: scaleY(0);
-o-transform: scaleY(0);
-webkit-transform: scaleY(0);
transform: scaleY(0);
}
}
@-moz-keyframes hideAnimation {
0% {
-moz-transform: scaleY(1);
-ms-transform: scaleY(1);
-o-transform: scaleY(1);
-webkit-transform: scaleY(1);
transform: scaleY(1);
}

60% {
-moz-transform: scaleY(0.98);
-ms-transform: scaleY(0.98);
-o-transform: scaleY(0.98);
-webkit-transform: scaleY(0.98);
transform: scaleY(0.98);
}

80% {
-moz-transform: scaleY(1.02);
-ms-transform: scaleY(1.02);
-o-transform: scaleY(1.02);
-webkit-transform: scaleY(1.02);
transform: scaleY(1.02);
}

100% {
-moz-transform: scaleY(0);
-ms-transform: scaleY(0);
-o-transform: scaleY(0);
-webkit-transform: scaleY(0);
transform: scaleY(0);
}
}
@-webkit-keyframes hideAnimation {
0% {
-moz-transform: scaleY(1);
-ms-transform: scaleY(1);
-o-transform: scaleY(1);
-webkit-transform: scaleY(1);
transform: scaleY(1);
}

60% {
-moz-transform: scaleY(0.98);
-ms-transform: scaleY(0.98);
-o-transform: scaleY(0.98);
-webkit-transform: scaleY(0.98);
transform: scaleY(0.98);
}

80% {
-moz-transform: scaleY(1.02);
-ms-transform: scaleY(1.02);
-o-transform: scaleY(1.02);
-webkit-transform: scaleY(1.02);
transform: scaleY(1.02);
}

100% {
-moz-transform: scaleY(0);
-ms-transform: scaleY(0);
-o-transform: scaleY(0);
-webkit-transform: scaleY(0);
transform: scaleY(0);
}
}

配置文件

{
"navdata":[{"key":"k0","value":"首页"},

{"key":"k1","value":"军事"},
{"key":"k3","value":"科技"},
{"key":"k4","value":"美食"},
{"key":"k5","value":"美女"},
{"key":"k6","value":"国际风云"}
],
"k0":["首页"],
"k1":["国内","国外"],
"k3":["武器","战略"],
"k4":["科技前沿","VR"],
"k5":["国内秘境","国外美食","小哥精选"],
"k6":["时事热点","国际纠纷"]
}

轮播插件

 

js

 

import React, { Component } from 'react';
import "../css/reactslider.css";
const imgUrl = [
{"url":require("../images/dakar.jpg"),"alt":"1","title":"我是第一张"},
{"url":require("../images/london.jpg"),"alt":"3","title":"我是第二张"},
{"url":require("../images/monaco.jpg"),"alt":"4","title":"我是第三张"}
];
function callBack(){

}
//轮播设置
const slideConfig = {
width:800,
height:400,
speed:3000,
curritem: 0,
isShowTitle: true,
callback:callBack
}


//返回轮播项的长度
function retAllLen(imgArr){
return imgArr.length;
}
//计算总长度
function retAllWidth(imgArr){
var len = imgArr.length;
var width = slideConfig.width;
return len*width
}

class reactSlider extends Component{
Timer="";
constructor(props) {
super(props);
var allWidth = retAllWidth(imgUrl);
var liwidth = slideConfig.width;
var allLen = retAllLen(imgUrl);
var curritem = slideConfig.curritem;
var height = slideConfig.height;
this.state = {width:allWidth,liwidth:liwidth,liheight:height,allLen:allLen,curritem:curritem,isShowTitle:false};

}
doCallback() {
var call = slideConfig.callback;
if(typeof call == "function"){
call();
}
}
removeClass(domArr){
for(var i=0;i<domArr.length;i++){
domArr[i].classList.remove("curritem");
}
}

addClass(dom,classname){
dom.classList.add(classname);
}
slidefun(){
var currItem = document.querySelector(".curritem");
var index = currItem.getAttribute("index");
const itemWidth = this.state.liwidth;
const slideWid = index*itemWidth;
var slider = document.querySelector(".sliderul");
currItem.classList.remove("curritem");
var nextslide = currItem.nextSibling;
if(nextslide==null){
slider.style.transition ="all .3s";
slider.style.left = 0;
currItem.parentNode.firstElementChild.classList.add("curritem");
this.isShowTitle();
return false;
}
nextslide.classList.add("curritem");
this.isShowTitle();
slider.style.transition ="all .8s";
slider.style.left = -slideWid+"px";
this.doCallback();

}
componentDidMount(){//组件加载完成后执行的函数
this.isShowTitle();
this.Timer = setInterval(()=>this.slidefun(),slideConfig.speed);
}
isShowTitle(){
const isShow = slideConfig.isShowTitle;
if(isShow){
this.setState({"isShowTitle":true});
this.chanageTitle();
}else{
this.setState({"isShowTitle":false});
}

}

chanageTitle(){
var currItem = document.querySelector(".curritem");
var title = currItem.getAttribute("title");
var slideTitle = document.querySelector(".slidetitle");
slideTitle.innerHTML = title;
}
navMouseOver(e){
const _target = e.target;
var Nodes = _target.parentNode.childNodes;
this.removeClass(Nodes);
e.target.classList.add("curritem");
var index = _target.getAttribute("index")-1;
const itemWidth = this.state.liwidth;
const slideWid = index*itemWidth;
var slider = document.querySelector(".sliderul");
slider.style.transition ="all .5s";
slider.style.left = -slideWid+"px";
this.isShowTitle();
this.doCallback();
}
startTimer(e){
this.Timer = setInterval(()=>this.slidefun(),slideConfig.speed);
}
stopTimer(e){
clearInterval(this.Timer);
}
render() {
const _this = this;
return (
<div className="sliderwrap" style={{width:_this.state.liwidth,height:_this.state.liheight}} onMouseOver={(e)=>_this.stopTimer(e)} onMouseLeave={(e)=>_this.startTimer(e)}>
<ul className="sliderul" style={{width:_this.state.width}}>
{
imgUrl.map(function(val,index){
var url = val.url;
return (
<li key={index} index={index} style={{width:_this.state.liwidth}}>
<img src={val.url} alt={val.alt}/>
</li>
)
})
}
</ul>
<div className="navbar">
{
imgUrl.map(function(val,index){
if(index == _this.state.curritem ){
return (
<span className="navbaritem curritem" index={index+1} onMouseOver={(e)=>_this.navMouseOver(e)} key={index} title={val.title}></span>
)
}
return (
<span className="navbaritem" index={index+1} onMouseOver={(e)=>_this.navMouseOver(e)} key={index} title={val.title}></span>
)
})
}
</div>
<div className="slidetitle" style={{display:_this.state.isShowTitle?"block":"none"}}></div>
</div>
)
}
}
export default reactSlider;

 

css

.sliderwrap{
position: relative;
overflow: hidden;
}
.sliderul{
list-style: none;
margin: 0;
padding: 0;
display: block;
overflow: hidden;
white-space:nowrap;
position: relative;
left: 0

}
.sliderul li{
display: table-row;
display: inline-block;


}
.sliderwrap img{
width: 100%;
display: block;
}
.navbar{
width: 100%;
position: absolute;
bottom: 0;
left: 0;
padding: 20px 0;
z-index: 20;
}
.navbar .navbaritem{
display: inline-block;
vertical-align: top;
margin-right: 14px;
width: 14px;
height: 14px;
border-radius: 7px;
background-color: #fff;
opacity: .5;
}
.navbar .navbaritem.curritem{
background-color: #010101;
}
.navbar .navbaritem:last-child{
margin-right: 0;
}
.slidetitle{
width: 100%;
position: absolute;
top: 0;
left: 0;
padding: 20px 0;
z-index: 20;
font-size: 20px;
color: #fff;
background-color: #222;
}

 

完整代码地址:

https://github.com/wangKXX/my-app/tree/master/my-app

欢迎大家指正

转载于:https://www.cnblogs.com/wangkdeblogs/p/7880379.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值