question-wrap.vue 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <template>
  2. <scroll-view v-if="visible" scroll-y class="question-wrap" :scroll-into-view="scrollIntoView"
  3. :scroll-with-animation="true">
  4. <view class="h-20"></view>
  5. <question-item :question="question" :readonly="readonly" :index="index" :subQuestionIndex="subQuestionIndex"
  6. @update:question="emit('update:question', $event)" @scrollTo="handleScrollTo"
  7. @changeSubQuestion="handleChangeSubQuestion" />
  8. <view class="h-20"></view>
  9. </scroll-view>
  10. </template>
  11. <script lang="ts" setup>
  12. import { Study } from '@/types';
  13. import QuestionItem from './question-item.vue';
  14. const props = defineProps<{
  15. question: Study.Question;
  16. readonly?: boolean;
  17. currentIndex: number;
  18. index: number;
  19. subQuestionIndex: number;
  20. }>();
  21. const visible = computed(() => {
  22. return Math.abs(props.currentIndex - props.index) <= 2;
  23. });
  24. const emit = defineEmits<{
  25. (e: 'update:question', question: Study.Question): void;
  26. (e: 'changeSubQuestion', index: number): void;
  27. }>();
  28. const handleChangeSubQuestion = (index: number) => {
  29. emit('changeSubQuestion', index);
  30. }
  31. const scrollIntoView = ref('');
  32. const handleScrollTo = (selector: string) => {
  33. console.log('收到子组件滚动请求', selector)
  34. scrollIntoView.value = '';
  35. setTimeout(() => {
  36. if (selector) {
  37. scrollIntoView.value = selector;
  38. }
  39. }, 200);
  40. }
  41. </script>
  42. <style lang="scss" scoped>
  43. .question-wrap {
  44. @apply h-full box-border;
  45. }
  46. </style>