CSSTransition not continuing after 1st question array

Viewed 32

I have an array of questions and I am trying to transition how each question moves to the next.

The transition works for the 1st question, but does not for the 2nd, 3rd etc.

I am asking the CSSTransition to start (in) at the first question. Any thoughts as to why this may not be working?

    const [currentQuestion, setCurrentQuestion] = useState(0);

...

                    <div className={css.questionsContainer}>
                        <CSSTransition
                            in={currentQuestion}
                            classNames="alert"
                            timeout={300}
                        >
                            <Paragraph large className={css.questions}>
                                {internationalOnboardingQuestions[currentQuestion].questionText}
                            </Paragraph>
                        </CSSTransition>
                    </div>
.alert-enter {
    opacity: 0;
    transform: scale(0.9);
}

.alert-enter-active {
    opacity: 1;
    transform: translateX(0);
    transition: opacity 300ms, transform 300ms;
}

.alert-exit {
    opacity: 1;
}

.alert-exit-active {
    opacity: 0;
    transform: scale(0.9);
    transition: opacity 300ms, transform 300ms;
}
1 Answers

If you're trying to make a slideshow of text, consider this:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {box-sizing: border-box}
body {font-family: Verdana, sans-serif; margin:0}

  /* Question container */
.question-container {
  position: relative;
  background: #f1f1f1f1;
}

/* Questions */
.myQuestion {
  display: none;
  padding: 80px;
  text-align: center;
}

/* Next & previous buttons */
.prev, .next {
  cursor: pointer;
  position: absolute;
  top: 50%;
  width: auto;
  margin-top: -30px;
  padding: 16px;
  color: #888;
  font-weight: bold;
  font-size: 20px;
  border-radius: 0 3px 3px 0;
  user-select: none;
}

/* Position the "next button" to the right */
.next {
  position: absolute;
  right: 0;
  border-radius: 3px 0 0 3px;
}

/* On hover, add a black background color with a little bit see-through */
.prev:hover, .next:hover {
  background-color: rgba(0,0,0,0.8);
  color: white;
}

/* The dot/bullet/indicator container */
.dot-container {
    text-align: center;
    padding: 20px;
    background: #ddd;
}

/* The dots/bullets/indicators */
.dot {
  cursor: pointer;
  height: 15px;
  width: 15px;
  margin: 0 2px;
  background-color: #bbb;
  border-radius: 50%;
  display: inline-block;
  transition: background-color 0.6s ease;
}

/* Add a background color to the active dot/circle */
.active, .dot:hover {
  background-color: #717171;
}

/* Add an italic font style to all quotes */
q {font-style: italic;}

/* Add a blue color to the author */
.author {color: cornflowerblue;}
</style>
</head>
<body>

<div class="question-container">

<div class="myQuestion">
  <p > Question 1</p>
</div>

<div class="myQuestion">
  <p > Question 2</p>
</div>

<div class="myQuestion">
  <p > Question 3</p>
</div>

<a class="prev" onclick="plusQuestions(-1)">❮</a>
<a class="next" onclick="plusQuestions(1)">❯</a>

</div>

<div class="dot-container">
  <span class="dot" onclick="currentSlide(1)"></span> 
  <span class="dot" onclick="currentSlide(2)"></span> 
  <span class="dot" onclick="currentSlide(3)"></span> 
</div>

<script>
var questionIndex = 1;
showQuestions(questionIndex);

function plusQuestions(n) {
  showQuestions(questionIndex += n);
}

function currentQuestion(n) {
  showQuestions(questionIndex = n);
}

function showQuestions(n) {
  var i;
  var q = document.getElementsByClassName("myQuestion");
  var dots = document.getElementsByClassName("dot");
  if (n > q.length) {questionIndex = 1}    
  if (n < 1) {questionIndex = q.length}
  for (i = 0; i < q.length; i++) {
      q[i].style.display = "none";  
  }
  for (i = 0; i < dots.length; i++) {
      dots[i].className = dots[i].className.replace(" active", "");
  }
  q[questionIndex-1].style.display = "block";  
  dots[questionIndex-1].className += " active";
}
</script>

</body>
</html> 

Here we have a loop of classes called "myQuestions" within a container, js controls how they are looped through that way the style is added to each one within js.

Related