<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                [TOC] >[success] # 組件和元素切換動畫的實現 1. **多元素的切換動畫** 之前上一章講過**單元素動畫** ,這次講一下 **多元素的動畫** ,實際上單元素與 **多元素** 寫法都一樣,代碼如下: **index.html** ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>組件和元素切換動畫的實現</title> <style> /* -------------------入場動畫----------------------- */ .v-enter-from{ opacity: 0; } .v-enter-active{ transition: opacity 3s ease-in; } .v-enter-to { opacity: 1; } /* -------------------出場動畫----------------------- */ .v-leave-from{ opacity: 1; } .v-leave-active{ transition: opacity 3s ease-in; } .v-leave-to { opacity: 0; } </style> <!-- 通過cdn方式引入vue --> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id="root"></div> </body> <script> const app = Vue.createApp({ data(){ return { show: false } }, methods: { handleClick(){ this.show = !this.show } }, template: ` <div> <transition> <div v-if="show">hello world</div> <div v-else="show">bye world</div> </transition> <button @click="handleClick">切換</button> </div>` }) const vm = app.mount('#root') </script> </html> ~~~ 上面雖然實現了 **多元素動畫** 的效果,但是有一個問題:**hello world 展示時候,bye world 慢慢悠悠的消失,導致每次切換時候能同時看到2個元素,如果想每次一個元素隱藏結束了,再顯示另外一個元素** ,如何解決呢,只需要在 **transition標簽** 上添加 **mode="out-in"** 就行,意思就是 **先隱藏后展示,先出去再進來(in-out 先進再出 out-in 先出再進)** , 代碼如下: ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>組件和元素切換動畫的實現</title> <style> /* -------------------入場動畫----------------------- */ .v-enter-from{ opacity: 0; } .v-enter-active{ transition: opacity 3s ease-in; } .v-enter-to { opacity: 1; } /* -------------------出場動畫----------------------- */ .v-leave-from{ opacity: 1; } .v-leave-active{ transition: opacity 3s ease-in; } .v-leave-to { opacity: 0; } </style> <!-- 通過cdn方式引入vue --> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id="root"></div> </body> <script> const app = Vue.createApp({ data(){ return { show: false } }, methods: { handleClick(){ this.show = !this.show } }, template: ` <div> <transition mode="out-in"> <div v-if="show">hello world</div> <div v-else="show">bye world</div> </transition> <button @click="handleClick">切換</button> </div>` }) const vm = app.mount('#root') </script> </html> ~~~ 剛進入頁面時候 **bye world** 是沒有動畫的,如果希望剛進入頁面 **bye world** 就有一個動畫效果,可以給 **transition標簽** 添加一個 **appear** 屬性,它的意思是 **初次對某一個默認元素顯示的時候會給它帶上我們寫的這個動畫效果** ,代碼如下: ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>組件和元素切換動畫的實現</title> <style> /* -------------------入場動畫----------------------- */ .v-enter-from{ opacity: 0; } .v-enter-active{ transition: opacity 3s ease-in; } .v-enter-to { opacity: 1; } /* -------------------出場動畫----------------------- */ .v-leave-from{ opacity: 1; } .v-leave-active{ transition: opacity 3s ease-in; } .v-leave-to { opacity: 0; } </style> <!-- 通過cdn方式引入vue --> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id="root"></div> </body> <script> const app = Vue.createApp({ data(){ return { show: false } }, methods: { handleClick(){ this.show = !this.show } }, template: ` <div> <transition mode="out-in" appear> <div v-if="show">hello world</div> <div v-else="show">bye world</div> </transition> <button @click="handleClick">切換</button> </div>` }) const vm = app.mount('#root') </script> </html> ~~~ 2. **多組件的切換動畫** **多組件** 的切換沒什么不同只是改成了 **組件** 的形式,代碼如下: **index.html** ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>組件和元素切換動畫的實現</title> <style> /* -------------------入場動畫----------------------- */ .v-enter-from{ opacity: 0; } .v-enter-active{ transition: opacity 1s ease-in; } .v-enter-to { opacity: 1; } /* -------------------出場動畫----------------------- */ .v-leave-from{ opacity: 1; } .v-leave-active{ transition: opacity 1s ease-in; } .v-leave-to { opacity: 0; } </style> <!-- 通過cdn方式引入vue --> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id="root"></div> </body> <script> // 多個單元素標簽之間的切換 // 多個單組件之間的切換 const ComponentA = { template: '<div>hello world</div>' } const ComponentB = { template: '<div>bye world</div>' } const app = Vue.createApp({ data(){ return { show: false } }, components: { 'component-a': ComponentA, 'component-b': ComponentB }, methods: { handleClick(){ this.show = !this.show } }, template: ` <div> <transition mode="out-in" appear> <component-a v-if="show" /> <component-b v-else="show" /> </transition> <button @click="handleClick">切換</button> </div>` }) const vm = app.mount('#root') </script> </html> ~~~ 或者也可以寫成 **動態組件** ,代碼如下: ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>組件和元素切換動畫的實現</title> <style> /* -------------------入場動畫----------------------- */ .v-enter-from{ opacity: 0; } .v-enter-active{ transition: opacity 1s ease-in; } .v-enter-to { opacity: 1; } /* -------------------出場動畫----------------------- */ .v-leave-from{ opacity: 1; } .v-leave-active{ transition: opacity 1s ease-in; } .v-leave-to { opacity: 0; } </style> <!-- 通過cdn方式引入vue --> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id="root"></div> </body> <script> // 多個單元素標簽之間的切換 // 多個單組件之間的切換 const ComponentA = { template: '<div>hello world</div>' } const ComponentB = { template: '<div>bye world</div>' } const app = Vue.createApp({ data(){ return { component: 'component-a' } }, components: { 'component-a': ComponentA, 'component-b': ComponentB }, methods: { handleClick(){ if(this.component === 'component-a'){ this.component = 'component-b' } else { this.component = 'component-a' } } }, template: ` <div> <transition mode="out-in" appear> <component :is="component" /> </transition> <button @click="handleClick">切換</button> </div>` }) const vm = app.mount('#root') </script> </html> ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看