App下載

怎么用css的cubic-bezier函數(shù)做出動(dòng)態(tài)緩沖效果?

猿友 2021-03-25 17:23:45 瀏覽數(shù) (2776)
反饋

今天W3Cschool小編為大家?guī)?lái)怎么用css的cubic-bezier函數(shù)做出動(dòng)態(tài)的緩沖效果?cubic-bezier即三次貝塞爾,可以生成貝塞爾曲線(xiàn),在css中主要是給transition以及animation提供過(guò)渡效果的速度曲線(xiàn)。

cubic-bezier函數(shù)默認(rèn)接受四個(gè)參數(shù),cubic-bezier(x1,y1,x2,y2),其中y1、y2是可以大于或小于0的,基于此可以做出一些緩沖的過(guò)渡效果。

<template>
    <div class="menu">
        <div v-for="item in menuList">
            <div class="menu-item" @mouseover="mouseover($event, item)" @mouseleave="mouseleave">{{ item.name }}</div>
        </div>
    </div>
    <transition name="move">
        <div id="sub-menu" ref="subMenu" v-show="show" @mousemove="overSub">
            <div class="sub-menu-item" v-for="item in subMenuList">
                {{ item.name }}
            </div>
        </div>
    </transition>
</template>

<script>
import { ref } from ‘vue‘;
export default {
    setup() {
        let menuList = ref([
            {
                name: ‘cloud‘,
                children: [
                    {
                        name: ‘cloud1‘
                    },
                    {
                        name: ‘cloud2‘
                    }
                ]
            },
            {
                name: ‘software‘,
                children: [
                    {
                        name: ‘software1‘
                    },
                    {
                        name: ‘software2‘
                    },
                    {
                        name: ‘software3‘
                    }
                ]
            },
            {
                name: ‘docs‘,
                children: [
                    {
                        name: ‘docs1‘
                    },
                    {
                        name: ‘docs2‘
                    }
                ]
            }
        ]);
        let subMenuList = ref([]);
        let show = ref(false);
        return { menuList, subMenuList, show };
    },
    methods: {
        mouseover(e, item) {
            this.subMenuList = item.children;
            this.$refs.subMenu.style.left = e.target.offsetLeft + ‘px‘;
            this.$refs.subMenu.style.height = `${43 * this.subMenuList.length}px`;
            this.show = true;
        },
        mouseleave() {
            this.show = false;
        }
    }
};
</script>

<style scoped>
.menu {
    display: flex;
}
.menu-item {
    height: 40px;
    width: fit-content;
    margin-left: 20px;
    font-size: 26px;
    font-weight: lighter;
    color: #aaa;
    cursor: pointer;
}
#sub-menu {
    position: absolute;
    font-size: 32px;
    font-weight: lighter;
    color: #000;
    border: solid 1px #ccc;
    padding: 15px;
    border-radius: 10px;
}
.move-enter-active,
.move-leave-active {
    transition: all 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}
</style>

沒(méi)有用原生的transition屬性是因?yàn)関-if不支持,所以必須結(jié)合vue的transition組件。

其中 cubic-bezier(0.175, 0.885, 0.32, 1.275);  是提供過(guò)渡效果的關(guān)鍵,可以看出整個(gè)菜單會(huì)有一個(gè)緩沖的過(guò)渡效果,視覺(jué)上也更加具有沖擊力。


CSS

0 人點(diǎn)贊