老师你好,这里的删除,为啥没有过渡效果啊

老师你好,这里的删除,为啥没有过渡效果啊

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>2-5</title>

    <script src="https://unpkg.com/vue@next"></script>

    <style>

        .v-enter-from.v-leave-to {

            opacity0;

            transformtranslateY(30px);

        }

        .v-enter-active.v-leave-active {

            transitionall 1s ease-in;

        }

        .v-enter-to {

            opacity1;

            transformtranslateY(0px);

        }

        .v-leave-from {

            opacity1;

            transformtranslateY(0px);

        }

        .v-move {

            transitionall 1s ease-in;

        }

    </style>

</head>

<body>

    <div id="root"></div>

</body>

<script>

    const app = Vue.createApp({

        data() {

            return { 

                list: [321]

             }

        },

        methods: {

            handleClick() {

                this.list.unshift(this.list.length + 1)

            },

            handleClick2(index) {

                this.list.splice(index1)

            }

        },

        template: `

        <div>

            <transition-group>

               <span class="span-item" v-for="(item, index) in list" :key="item" @click="handleClick2(index)">{{item}}</span>

            </transition-group>

            <button @click="handleClick">增加</button>

        </div>

        `

    })


    const vm = app.mount('#root')

</script>

</html>


正在回答

登陆购买课程后可参与讨论,去登陆

2回答

同学你好,目前可以添加如代码实现该效果:

http://img1.sycdn.imooc.com//climg/607fed99091af34808290142.jpg

说明:这种方法只适合从前往后删除元素或者是从中间删除元素,如果从最后一个元素开始删除是没有效果的。

示例如下:

http://img1.sycdn.imooc.com//climg/607fee080a66808105130079.jpg

祝学习愉快!

好帮手慕慕子 2021-04-21 11:41:07

同学你好,老师测试同学的代码,删除也是有过渡效果的,同学可以再测试下,如下图所示:

http://img1.sycdn.imooc.com//climg/607f9ec00a47555303820079.jpg

祝学习愉快~​

  • 提问者 xiao白做大事 #1

    老师 ,刚才的代码不全面,代码是这样的

    <!DOCTYPE html>

    <html lang="en">

    <head>

        <meta charset="UTF-8">

        <meta http-equiv="X-UA-Compatible" content="IE=edge">

        <meta name="viewport" content="width=device-width, initial-scale=1.0">

        <title>2-6列表动画</title>

        <script src="https://unpkg.com/vue@next"></script>

        <style>

            .v-enter-from.v-leave-to {

                opacity0;

                transformtranslateY(30px);

            }

            .v-enter-active.v-leave-active {

                transitionall 1s ease-in;

            }

            .v-enter-to {

                opacity1;

                transformtranslateY(0px);

            }

            .v-leave-from {

                opacity1;

                transformtranslateY(0px);

            }

            .v-move {

                transitionall 2s ease-in;

            }

            .span-item {

                displayinline-block;

                margin-right10px;

            }

        </style>

    </head>

    <body>

        <div id="root"></div>

    </body>

    <script>

        const app = Vue.createApp({

            data() {

                return { 

                    list: [321]

                 }

            },

            methods: {

                handleClick() {

                    this.list.unshift(this.list.length + 1)

                },

                handleClick2(index) {

                    this.list.splice(index1)

                }

            },

            template: `

            <div>

                <transition-group>

                   <span class="span-item" v-for="(item, index) in list" :key="item" @click="handleClick2(index)">{{item}}</span>

                </transition-group>

                <button @click="handleClick">增加</button>

            </div>

            `

        })


        const vm = app.mount('#root')

    </script>

    </html>



    当增加元素的时候,整体的移动是有效果的,但是删除元素的时候,没有过渡效果,为什么?

    2021-04-21 16:13:06
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师