所有分类
  • 所有分类
  • 织梦模板

实战用干货分享用react实现一个tab组件

使用react实现一个tab组件的方法:1、通过“export default props => {...}”方式创建TAB button组件;2、通过“tab-group-layout.js”组件来传“tabIndex”,并设置默认选中的tab效果;3、用react继承“react.component”组件里的onMouseOver和OnMouseOut方法即可。

怎么使用react实现一个tab组件

怎么使用react实现一个tab组件

react写Tab组件

使用react写TAB栏组件和对应hover事件(背景:在用gatsby开发页面时,遇到这样的组件效果,顺便记录一下)

1、效果

默认选中的tab选中效果 和 鼠标放上去的hover效果

当鼠标滑过右侧的tab时,也会有和第一个一样的选中效果!

2、tab-button.js 组件

import React from "react"
import { css } from "@emotion/core"
import { Link } from "gatsby"
import jdyStyles from "./container.module.css"
  
// TAB button 组件
export default props => {
  
return (
  
<li css={css`font-size: 18px;margin-left:18px;margin-right: 18px;display:flex;flex-direction: column;align-items:center;justify-content:center`} >
  
<Link css={css`font-size: 18px;padding: 20px 12px;`} 
className={ (props.selected?jdyStyles.header_hover_default:jdyStyles.header_hover)  }  to={props.to}>
{props.children}
</Link>
  
</li>
  
)
}

3、tab-group-layout.js 组件

import React from "react"
import { css } from "@emotion/core"
import { Link } from "gatsby"
import ListLink from "../components/tab-button"
import RegisterButton from "../components/round-button"
export default ({ tabIndex }) => {
  
return (
  
<div>
   
{/* tab */}
<ul style={{ listStyle: `none`, float: `right` }} css={css`display: flex;justify-content: space-between;align-items: center;`}>
<ListLink to="/official-site/" selected={(tabIndex==='official-site')}>产品介绍</ListLink>
<ListLink to="/about/" selected={(tabIndex==='about')}>成功案列</ListLink>
<ListLink to="/contact/" selected={(tabIndex==='contact')}>服务支持</ListLink>
<ListLink to="/sweet-pandas-eating-sweets/" selected={(tabIndex==='sweet-pandas-eating-sweets')}>资源中心</ListLink>
</ul>
  
</div>
  
)
}

使用这个组件传过来 tabIndex 设置默认选中的tab效果;也可以自己处理展示的逻辑

4、对应的css样式 container.module.css

.header_hover{
  color: #333;
}
  
.header_hover_default{
  color: #0084ff!important;
  border-top: 3px solid #0084ff;
}
  
.header_hover:hover{
  color: #0084ff!important;
  border-top: 3px solid #0084ff;
}

5、当前组件的hover使用的是css样式控制,也可以用 react继承 react.component组件里的onMouseOver和OnMouseOut方法来实现

常见问题
所有VIP可以商用吗,会不会有版权问题?
本站提供的源码大部分可以作为源码开发参考和学习,部分开源可商用。
账号可以分享给其他人使用吗?
您好,账号禁止共享,我们有验证机制的,会自动封号处理,谢谢配合。
如果遇到问题怎么联系解决呢?
最快的途径是在导航菜单选择提交【售后】会在24小时处理
下载的源码可以使用吗?
我们会在显著的地方表明【已测试】,显示已测试的都是小编测试过 。
1. 本站所有资源来源于用户上传和网络,因此不包含技术服务请大家谅解!
2. 本站不保证所提供下载的资源的准确性、安全性和完整性,资源仅供下载学习之用!如有链接无法下载、失效或广告,请联系客服处理!
3. 您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容资源!如用于商业或者非法用途,与本站无关,一切后果请用户自负!
4. 如果您也有好的资源或教程,您可以投稿发布,成功分享后有站币奖励和额外收入!
5.如有侵权请联系客服邮件770973008@qq.com
原文链接:https://www.56admin.com/27971.html,转载请注明出处。
0
分享海报

评论0

请先
九年老站,质量有保证,90%亲自测试!优惠原价199模板币终生SVIP,优惠价68模板币! 数量有限! 购买SVIP 所有资源免积分下载,畅享无忧!
显示验证码
没有账号?注册  忘记密码?