Yansen 2 years ago
parent
commit
d2bf306c52
3 changed files with 55 additions and 32 deletions
  1. 12
    5
      src/pages/bk1/index.vue
  2. 20
    20
      src/pages/bk1/pg2.vue
  3. 23
    7
      src/pages/bk1/useSerial.js

+ 12
- 5
src/pages/bk1/index.vue View File

@@ -2,15 +2,15 @@
2 2
   <div class="bk-pg">
3 3
     <GoBack class="goback abs" @click="router.go(-1)" />
4 4
     <div id="bk1">
5
-      <Pg1 />
6
-      <Pg2 />
7
-      <Pg3 />
5
+      <Pg1 :active="pgRef == 1" />
6
+      <Pg2 :active="pgRef == 2" />
7
+      <Pg3 :active="pgRef == 3" />
8 8
     </div>
9 9
   </div>
10 10
 </template>
11 11
 
12 12
 <script setup>
13
-  import { onMounted } from 'vue';
13
+  import { onMounted, ref } from 'vue';
14 14
   import { useRoute, useRouter } from 'vue-router';
15 15
   import GoBack from '@/components/GoBack.vue';
16 16
   import Pg1 from './pg1.vue';
@@ -21,13 +21,20 @@
21 21
   const { query } = useRoute();
22 22
   const { page = 1 } = query || {};
23 23
 
24
+  const pgRef = ref(page);
25
+
24 26
   onMounted(() => {
25 27
     $("#bk1").turn({
26 28
       width: '100vw',
27 29
       height: '100vh',
28 30
       display: 'single',
29 31
       page,
30
-      pages: 3
32
+      pages: 3,
33
+      when: {
34
+        turned: function(e, pg) {
35
+          pgRef.value = pg;
36
+        }
37
+      }
31 38
     });
32 39
   });
33 40
 </script>

+ 20
- 20
src/pages/bk1/pg2.vue View File

@@ -3,12 +3,12 @@
3 3
     <div class="content">
4 4
       <div class="header-img">
5 5
         <img src="/images/bk1/pg2/header.png" alt="">
6
-        <div id="flower" class="flower" />
6
+        <div ref="flowerRef" class="flower" />
7 7
       </div>
8 8
       <br>
9 9
       <img class="badge-img" src="/images/bk1/pg2/badge.png" alt="">
10 10
 
11
-      <p class="txt animate__animated animate__rubberBand">
11
+      <p class="txt animate__animated animate__fadeIn">
12 12
         2014年,<br>
13 13
         南京广电集团以“紫金草”为原型,<br>
14 14
         制作紫金草徽章发放给市民。<br>
@@ -24,35 +24,35 @@
24 24
 </template>
25 25
 
26 26
 <script setup>
27
-  import { onMounted } from 'vue';
27
+  import { computed, onMounted, ref } from 'vue';
28 28
   import Dot from './Dot.vue';
29 29
   import FlipIcon from './FlipIcon.vue';
30 30
   import useSerial from './useSerial';
31 31
 
32
-  const flowerImg = {
33
-    maxHeight: 11200,
34
-    height: 700,
35
-    renderHeight: 150,
36
-    percent: 150 / 700,
37
-  }
32
+  const props = defineProps({
33
+    active: Boolean,
34
+  });
35
+
36
+  const flowerRef = ref();
37
+  const ready = computed(() => props.active);
38 38
 
39
-  onMounted(() => {
39
+  console.log('-----<>', ready);
40
+  
41
+  useSerial((t) => {
40 42
     const maxHeight = 11200;
41 43
     const rawHeight = 700;
42
-    const flower = document.querySelector('#flower');
44
+    const flower = flowerRef.value;
43 45
     const height = flower.offsetHeight;
46
+    const pos = t * rawHeight;
44 47
 
45
-    useSerial((t) => {
46
-      const pos = t * flowerImg.height;
48
+    const backgroundPosition = `0 -${pos * height / rawHeight}px`;
49
+    flower.style.backgroundPosition = backgroundPosition;
47 50
 
48
-      const backgroundPosition = `0 -${pos * height / rawHeight}px`;
49
-      flower.style.backgroundPosition = backgroundPosition;
51
+    if (pos >= (maxHeight - rawHeight)) {
52
+      return true;
53
+    }
54
+  }, ready);
50 55
 
51
-      if (pos >= (maxHeight - rawHeight)) {
52
-        return true;
53
-      }
54
-    })
55
-  });
56 56
 </script>
57 57
 
58 58
 <style lang="less" scoped>

+ 23
- 7
src/pages/bk1/useSerial.js View File

@@ -1,13 +1,29 @@
1
+import { onMounted, ref, watch } from 'vue';
1 2
 
2
-export default function useSerial(callback) {
3
+export default function useSerial(callback, dep) {
3 4
   let times = 0;
5
+  let execed = false;
6
+  const loaded = ref(false);
4 7
 
5
-  const i = setInterval(() => {
6
-    const isOver = callback(times);
7
-    times += 1;
8
+  onMounted(() => {
9
+    loaded.value = true;
10
+  });
8 11
 
9
-    if (isOver) {
10
-      clearInterval(i);
12
+  watch([loaded, dep], (values) => {
13
+    console.log(values)
14
+    if (execed) return;
15
+
16
+    if (values[0] && values[1]) {
17
+      execed = true;
18
+      
19
+      const i = setInterval(() => {
20
+        const isOver = callback(times);
21
+        times += 1;
22
+
23
+        if (isOver) {
24
+          clearInterval(i);
25
+        }
26
+      }, 2000 / 16);
11 27
     }
12
-  }, 2000 / 16);
28
+  });
13 29
 }