当前位置:   article > 正文

React Taro每次点击实现自动下滑滚动_react 点击向下移动

react 点击向下移动
  1. import React, { useState, useEffect, useRef } from 'react'
  2. import { View, Text, Image, ScrollView } from '@tarojs/components'
  3. import { questionList, questionOptions } from './data'
  4. import QuestionCard from './questionCard'
  5. import ToolbarButton from '@/component/ToolbarButton'
  6. import { FixedView, Toast } from '@taroify/core'
  7. import Taro from '@tarojs/taro'
  8. export default function QuestionNaire() {
  9. const messagesEndRef = useRef(null)
  10. let [qusetionIndex, setQuestionIndex] = useState('1')
  11. const [newQueList, setNewQueList] = useState([])
  12. let [height, setHeight] = useState('300')
  13. const getTobToTop = () => {
  14. Taro.createSelectorQuery()
  15. .select('#date_list')
  16. .boundingClientRect((rec) => {
  17. console.log('rec33', rec)
  18. setHeight(rec?.height || 0)
  19. if (!rec?.top) {
  20. setTimeout(() => {
  21. getTobToTop()
  22. }, 200)
  23. }
  24. })
  25. .exec()
  26. }
  27. useEffect(() => {
  28. getTobToTop()
  29. if (questionList[0].questions.length > newQueList.length) {
  30. setNewQueList(questionList[0].questions.slice(0, qusetionIndex))
  31. } else {
  32. Toast.open('这是最后一题啦')
  33. }
  34. }, [qusetionIndex])
  35. useEffect(() => {
  36. console.log(height, 'height')
  37. Taro.pageScrollTo({
  38. scrollTop: height,
  39. duration: 300,
  40. })
  41. }, [height])
  42. return (
  43. <>
  44. <View className="p-sm m-5 bg-white text-sm" id="date_list">
  45. {newQueList.map((item, index) => {
  46. console.log(item.jumpTo, 'item')
  47. return (
  48. <View key={index}>
  49. <QuestionCard {...item} i={++index} />
  50. <View ref={messagesEndRef}></View>
  51. </View>
  52. )
  53. })}
  54. </View>
  55. <View className="h-35"></View>
  56. <FixedView position="bottom">
  57. <View className="bg-white p-sm">
  58. <ToolbarButton
  59. leftButton={{
  60. text: '上一题',
  61. color: 'default',
  62. onClick: () => setQuestionIndex(--qusetionIndex),
  63. }}
  64. rightButton={{
  65. text: ' 下一题',
  66. onClick: () => {
  67. setQuestionIndex(++qusetionIndex)
  68. // if (item.jumpTo == '-2') {
  69. // setQuestionIndex(questionList[0].questions.length)
  70. // } else if (item.jumpTo == '-2') {
  71. // setQuestionIndex(++qusetionIndex)
  72. // } else {
  73. // setQuestionIndex('1')
  74. // }
  75. },
  76. }}
  77. ></ToolbarButton>
  78. </View>
  79. </FixedView>
  80. <Toast id="toast" />
  81. </>
  82. )
  83. }

1、监听元素高度,给要监听的元素设置id,注意在Dom渲染完成前获取为null的问题

 Taro.createSelectorQuery()

      .select('#date_list')

      .boundingClientRect((rec) => {

        console.log('rec33', rec)

        setHeight(rec?.height || 0)

        if (!rec?.top) {

          setTimeout(() => {

            getTobToTop()

          }, 200)

        }

      })

      .exec()

2、使用Taro实现向下滚动

Taro.pageScrollTo({

      scrollTop: height,

      duration: 300,

    })

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/275065
推荐阅读
相关标签
  

闽ICP备14008679号