Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

137
Visualizações
How to scroll a div automatically with button onClick

Hi i am trying to create an image slider that can slide with a next/prev button. I also have an array of data that will be map through. So this is my code.


import React,{useState, useEffect} from 'react'

import { ThirdContainer, SecondContainer, ButtonLeft, ButtonRight, ButtonTitle, Container, FirstRow, FirstTitle, SecondRow, SecondTitle, TitleContainer } from './FirstSectionStyled'

const FirstSection = ({bgData}) => {

 //functions
 const nextSLide = () =>{}
 const prevSlide = () =>{}

    return (
        <Container>
            <ButtonLeft onClick={prevSLide}/>
                <SecondContainer id="scroller">
                    {bgData.map((data,index) => (
                        <ThirdContainer key={data.id} backgroundImg = {data.backgroundImg}>
                            <FirstRow>
                                <TitleContainer>
                                    <FirstTitle>{data.firstTitle}</FirstTitle>
                                    <SecondTitle>{data.secondTitle}</SecondTitle>
                                    <ButtonTitle>Buy Now</ButtonTitle>
                                </TitleContainer>
                            </FirstRow>
                            <SecondRow>
                            </SecondRow>
                        </ThirdContainer>
                    ))
                    }
                </SecondContainer> 
            <ButtonRight onClick={nextSLide} />
        </Container>
    )
}

export default FirstSection

And this is the styled component

export const SecondContainer = styled.div`
    height:100%;
    width: 100vw;
    overflow-x: auto;
    background: black;
    display : flex;

`
export const ThirdContainer = styled.div`
    min-width: 100vw;
    display: grid;
    grid-template-rows: 70% 30%;
    background-image: linear-gradient(to bottom, rgb(21, 21, 21, 0.5), rgb(21, 21, 21, 0.2), rgb(21, 21, 21, 0.2), rgb(21, 21, 21, 0.4)), 
                      url(${(props) => (props.backgroundImg)});
    background-position: center center;
    background-repeat: no-repeat;
    background-size: cover;
`

The problem is How can I make the SecondContainer to scroll its x-axis by 100vw everytime the button is clicked so that it will show the next ThirdContainer and create the image slider effect. I also want to make it animate smoothly. So any idea to solve this? Thank u!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Add a class name to your container

<ThirdContainer className='third-container' ....

import React,{useState, useEffect} from 'react'

import { ThirdContainer, SecondContainer, ButtonLeft, ButtonRight, ButtonTitle, Container, FirstRow, FirstTitle, SecondRow, SecondTitle, TitleContainer } from './FirstSectionStyled'

const FirstSection = ({bgData}) => {

 //functions
 const nextSLide = (index) =>{
   let ele = document. getElementsByClassName('third-container')[0] //replace 0 with current active slide index
    document.getElementByClassName('second-container').scrollTo(ele.offsetWidth)
 }
 const prevSlide = () =>{}

    return (
        <Container>
            <ButtonLeft onClick={prevSLide}/>
                <SecondContainer className="second-container" id="scroller">
                    {bgData.map((data,index) => (
                        <ThirdContainer className='third-container' key={data.id} backgroundImg = {data.backgroundImg}>
                            <FirstRow>
                                <TitleContainer>
                                    <FirstTitle>{data.firstTitle}</FirstTitle>
                                    <SecondTitle>{data.secondTitle}</SecondTitle>
                                    <ButtonTitle>Buy Now</ButtonTitle>
                                </TitleContainer>
                            </FirstRow>
                            <SecondRow>
                            </SecondRow>
                        </ThirdContainer>
                    ))
                    }
                </SecondContainer> 
            <ButtonRight onClick={nextSLide} />
        </Container>
    )
}

export default FirstSection

please note this is not tested in an actual environment. the method could help you to implement it. if it still not working please post your code to slackBits and share the link in comments

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda