0
点赞
收藏
分享

微信扫一扫

React 简书项目实战【1】实现Header组件布局

React 简书项目实战【1】实现Header组件布局

App.js

import React, { Component } from "react";
import Header from "./common/header";
class App extends Component{
render() {
return (
<Header />
);
}
}
export default App;

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import './style.js';
import App from './App';


ReactDOM.render(<App />,document.getElementById('root'));

style.js 下载styled-components

import { createGlobalStyle } from 'styled-components'


export const Globalstyle = createGlobalStyle`
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
display: block;
}
body {
line-height: 1;
}
ol, ul {
list-style: none;
}
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
content: '';
content: none;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
`;

导入logo

创建statics文件夹导入图片

React 简书项目实战【1】实现Header组件布局_javascript

创建组件

在src下创建common文件夹

在common下创建header文件夹

在header文件夹下创建index.js、style.js

index.js

import React, { Component } from "react";
import {HeaderWrapper,Logo,Nav,NavItem,NavSearch,Addition,Button} from "./style";

class Header extends Component {
render() {
return (
<HeaderWrapper>
<Logo />
<Nav>
<NavItem className='left active'>首页</NavItem>
<NavItem className='left'>下载App</NavItem>
<NavItem className='right'>登录</NavItem>
<NavItem className='right'>Aa</NavItem>
<NavSearch></NavSearch>
</Nav>
<Addition>
<Button className='writting'>写文章</Button>
<Button className='reg'>注册</Button>
</Addition>
</HeaderWrapper>
)
}
}

export default Header;

style.js

import styled from 'styled-components';
import logoPic from '../../statics/logo.png'
export const HeaderWrapper = styled.div`
position: relative;
height: 58px;
border-bottom: 1px solid #f0f0f0;
`;
export const Logo = styled.a.attrs({href:'/'})`
position: absolute;
top: 0;
left: 0;
display: block;
width: 100px;
height: 56px;
background: url(${logoPic});
background-size: contain;
`;
export const Nav = styled.div`
width: 960px;
height: 100%;
padding-right: 70px;
box-sizing: border-box;
margin: 0 auto;
`;
export const NavItem = styled.div`
line-height: 56px;
padding: 0 15px;
font-size: 17px;
color: #333;
&.left {
float: left;
}
&.right {
float: right;
color: #969696;
}
&.active {
color: #ea6f5a;
}
`;
export const NavSearch = styled.input.attrs({
placeholder: '搜索'
})`
width: 160px;
height: 38px;
padding: 0 20px;
margin-top: 9px;
margin-left: 20px;
box-sizing: border-box;
border: none;
outline: none;
border-radius: 19px;
background: #eee;
font-size: 14px;,
&::placeholder {
color: #999;
}
`;

export const Addition = styled.div`
position: absolute;
right: 0;
top: 0;
height: 56px;
`;

export const Button = styled.div`
float: right;
margin-top: 9px;
margin-right: 20px;
padding: 0 20px;
line-height: 38px;
border-radius: 19px;
border: 1px solid #ec6149;
font-size: 14px;
&.reg {
color: #ec6149;
}
&.writting {
color: #fff;
background: #ec6149;
}
`;

效果图

React 简书项目实战【1】实现Header组件布局_react.js_02


举报

相关推荐

0 条评论