1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
| <script lang="ts"> import { Swiper, SwiperSlide } from "vue-awesome-swiper"; import { Vue, Component } from "vue-property-decorator"; import axios from "axios";
interface PageItem { imageUrl: string; translation: string; text: string; readIndex: number; [propName: string]: any; }
declare global { interface Window { yt: any; } }
@Component({ name: "StoryDemo", components: { Swiper, SwiperSlide }, computed: { swiper() { return (this.$refs.mySwiper as any).$swiper; }, }, }) export default class Home extends Vue { title = "Story Demo"; pageName = ""; showTranslation = false; orientation = ""; picbook = { name: "", coverImageUrl: "", coverAudioUrl: "", wordCount: 1 }; hideGo = false; isPlaying = false; playingIndex = 0; pageContents: PageItem[] = []; swiper: any; timeupdate: any;
mounted() { this.orient(); window.addEventListener( "onorientationchange" in window ? "orientationchange" : "resize", () => this.orient(), false ); }
orient() { if (window.orientation === 0 || window.orientation === 180) { this.orientation = "portrait"; } else if (window.orientation === 90 || window.orientation === -90) { this.orientation = "landscape"; } }
asyncData() { return axios({ url: "story.json", method: "get" }) .then((res: any) => { this.picbook = res.data.picbook; this.pageContents = res.data.picbook.pageContents; }) .catch((err) => console.error("故事数据加载失败:", err)); }
async start() { this.hideGo = true; await this.asyncData(); this.pageName = "start"; await this.delay(1); this.audioPlay(-1); }
async restart() { this.pageName = "start"; await this.delay(0.5); this.audioPlay(-1); }
delay(speed: number) { return new Promise((resolve) => setTimeout(() => resolve(1), speed * 1000)); }
audioPlay(index: number) { this.isPlaying = true; this.playingIndex = index; const audioDom = document.getElementById(`audioPlayer${index}`) as HTMLAudioElement; audioDom.load(); audioDom.play(); if (index > -1) this.changeTextColor(index); }
resetTextColor(index: number) { this.pageContents[index].readIndex = -1; }
changeTextColor(index: number) { const pageItem = this.pageContents[index]; const audioDom = document.getElementById(`audioPlayer${index}`) as HTMLAudioElement; this.timeupdate = setInterval(() => { pageItem.wordStartTimes.forEach((time: number, textIndex: number) => { if (audioDom.currentTime * 1000 >= time) { pageItem.readIndex = textIndex; } }); }, 25); }
audioPause(index: number) { const audioDom = document.getElementById(`audioPlayer${index}`) as HTMLAudioElement; audioDom.pause(); if (this.timeupdate) { clearInterval(this.timeupdate); this.timeupdate = null; } }
async startAudioEndedCallback() { this.isPlaying = false; await this.delay(1); this.pageName = "swiper"; await this.delay(0.5); this.audioPlay(0); }
async audioReadEndedCallback() { this.isPlaying = false; const newIndex = this.swiper.activeIndex + 1; if (this.pageContents.length === newIndex) { this.pageName = "end"; await this.delay(0.5); this.swiper.slideTo(0, 0, false); } else { this.swiper.slideTo(newIndex, 500, true); } }
changeTransitionEnd(item: any) { if (this.isPlaying) { this.audioPause(this.playingIndex); this.resetTextColor(this.playingIndex); } this.resetTextColor(item.activeIndex); this.audioPlay(item.activeIndex); }
backToApp() { window.yt?.backToApp(); }
audioReadCallback(item: any) { console.log("音频时间更新:", item); } changeTransitionStart(item: any) { console.log("滑动开始:", item); } transitionStart(item: any) { console.log("过渡动画开始:", item); } } </script>
|