Я пытаюсь использовать CSS-сетку, чтобы создать макет с альтернативными столбцами в каждой строке.Первая строка будет иметь изображение слева и текст справа, вторая строка с текстом слева и изображение справа и т. Д.
Я пытался использовать nth-of-type, нопоскольку родительский узел (Box) повторяется для каждого поста, я думаю, что это будет очень сложно.
Дело в том, что я не могу охватить всех детей по отдельности, потому что контент поступает из GraphQL, и у меня есть только один узел дляиметь дело.
Кто-нибудь есть предложение для этого?
Спасибо!
import React, { Component } from "react";
import { Link, graphql, StaticQuery } from 'gatsby'
import styled from 'styled-components'
import Img from 'gatsby-image'
const Wrapper = styled.div`
margin: 0 auto;
margin-bottom: 6rem;
overflow: visible;
`
const Title = styled.h5`
margin-bottom: 2rem;
text-align: center;
`
const Inner = styled.div`
display: grid;
grid-template-columns: 1fr;
grid-row-gap: 3rem;
@media (max-width: 1024px) {
grid-template-columns: 1fr;
grid-row-gap: 80px;
padding: 1rem;
}
`
const Box = styled.div`
display: grid;
grid-template-columns: 1fr 1fr;
grid-column-gap: 6rem;
align-items: center;
grid-template-rows: auto;
grid-template-areas: "left right";
@media (max-width: 1024px) {
grid-template-columns: 1fr;
grid-row-gap: 80px;
padding: 1rem;
}
`
const Content = styled.div`
text-decoration: none;
&:nth-of-type(even){
grid-area: right;
}
&:nth-of-type(odd){
grid-area: left;
}
`
const StyledLink = styled(Link)`
text-decoration: none;
color: inherit;
:nth-of-type(even){
grid-area: left;
}
:nth-of-type(odd){
grid-area: right;
}
`
const StyledImg = styled(Img)`
border-radius: 7px;
margin-bottom: 1rem;
opacity: 1;
-webkit-transition: .5s ease-in-out;
transition: .5s ease-in-out;
:hover {
opacity: .7;
}
`
const PostTitle = styled.h6`
margin-bottom: 0.5rem;
`
const Date = styled.p`
font-size: 0.8rem;
display: block;
color: #777;
`
export class Gallery extends Component {
render(){
return (
<Wrapper>
<Title>
Works
</Title>
<Inner>
{this.props.data.allMarkdownRemark.edges.map(({ node }) => (
<Box key={node.id} className='box'>
<StyledLink to={node.fields.slug}>
<StyledImg fluid={node.frontmatter.image.childImageSharp.fluid} />
</StyledLink>
<Content>
<StyledLink to={node.fields.slug}>
<PostTitle>
{node.frontmatter.title}{" "}
</PostTitle>
</StyledLink>
<Date>
{node.frontmatter.date}
</Date>
<p>{node.excerpt}</p>
</Content>
</Box>
))}
</Inner>
</Wrapper>
)
}
}
export default props => (
<StaticQuery
query={graphql`
query {
allMarkdownRemark(sort: { fields: [frontmatter___date], order: DESC }) {
totalCount
edges {
node {
id
frontmatter {
title
date(formatString: "DD MMMM, YYYY")
image {
childImageSharp {
fluid(maxWidth: 800) {
...GatsbyImageSharpFluid_noBase64
}
}
}
}
fields {
slug
}
excerpt
}
}
}
}
`}
render={data => <Gallery data={data} {...props} />}
/>
)
````