最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue實現折疊展開收縮動畫效果

 更新時間:2023年11月14日 11:31:07   作者:ps酷教程  
這篇文章主要介紹了vue實現折疊展開收縮動畫,通過scrollHeight實現,本文通過實例代碼給大家介紹的非常詳細,感興趣的朋友一起看看吧

學習鏈接

vue項目列表折疊面板動畫效果實現
element-ui之el-collapse-transition(折疊展開動畫)源碼解析學習

通過scrollHeight實現

在這里插入圖片描述

以下代碼注意兩點

  • trainsition是需要有兩個值,才能產生過渡動畫的,所以一開始就需要獲取到box1的高度(通過scrollHeight去獲取它的高度)
  • box1收縮,其實就是把它的height改為0,超出部分隱藏,這樣子元素就隱藏了(但是注意,這個時候,仍然可以通過scrollHeight獲取到box1的實際高度,盡管它的style的height已經是0了)
<template>
    <div>
        <el-button plain type="danger" @click="toggleDiv" size="mini" style="margin-bottom: 10px;" >toggleDiv</el-button>
        <div class="box1" ref="box1" id="box1">
            <div class="box1-item"></div>
            <div class="box1-item"></div>
            <div class="box1-item"></div>
            <div class="box1-item"></div>
            <div class="box1-item"></div>
        </div>
    </div>
</template>
<script>
export default {
    name: 'Collapse',
    components: {
    },
    data() {
        return {
            isCollapse: false,
        }
    },
    mounted() {
        // 剛開始的時候, 就必須先獲取到這個元素的高度(transition需要有兩個數值才能產生過渡), 它必須剛開始就是可見的(不能display:none)
        console.log('mounted', this.$refs['box1'].scrollHeight);
        this.$refs['box1'].style.height = this.$refs['box1'].scrollHeight + 'px'
    },
    methods: {
        toggleDiv() {
            this.isCollapse = !this.isCollapse
            if(this.isCollapse) {
                this.$refs['box1'].style.height = 0
            } else {
                // 這個時候,box1已經收縮了,但是需要展開,那就必須獲取到它的高度(此時它的style的height為0)
                console.log( this.$refs['box1'].scrollHeight);
                this.$refs['box1'].style.height = this.$refs['box1'].scrollHeight + 'px'
            }
        }
    }
}
</script>
<style>
.box1 {
    width: 200px;
    /* height: 200px; */
    background-color: #bfa;
    transition: height 0.28s;
    overflow: hidden;
}
.box1 .box1-item {
    height: 20px;
    border: 1px solid red;
}
</style>

通過js獲取auto時的高度去實現(效果不好)

雖然,實現效果并不怎么好,但是比較巧妙,它通過js設置height為auto,然后就可以獲取元素的自然高度。這種獲取高度的方式可以借鑒下

<template>
    <div>
        <el-button plain type="danger" @click="toggleDiv" size="mini" style="margin-bottom: 10px;" >toggleDiv</el-button>
        <div class="box1" ref="box1" id="box1">
            <div class="box1-item"></div>
            <div class="box1-item"></div>
            <div class="box1-item"></div>
            <div class="box1-item"></div>
            <div class="box1-item"></div>
        </div>
    </div>
</template>
<script>
export default {
    name: 'Collapse',
    components: {
    },
    data() {
        return {
            isCollapse: false,
        }
    },
    mounted() {
        console.log(this.$refs['box1'].scrollHeight); // 110
        this.$refs['box1'].style.height = 'auto'
        this.$refs['box1'].style.height = window.getComputedStyle(this.$refs['box1']).height
    },
    methods: {
        toggleDiv() {
            this.isCollapse = !this.isCollapse
            if(this.isCollapse) {
                this.$refs['box1'].style.height = 0
            } else {
                this.$refs['box1'].style.height = 'auto'
                let height = window.getComputedStyle(this.$refs['box1']).height
                // 這里修改的太快,transition都還沒開始做動畫
                this.$refs['box1'].style.height = 0
                this.$refs['box1'].style.height = height
            }
        }
    }
}
</script>
<style>
.box1 {
    width: 200px;
    background-color: #bfa;
    overflow: hidden;
}
.box1 .box1-item {
    height: 20px;
    border: 1px solid red;
}
</style>

優(yōu)化

要使用setTimeout,才能在展開的時候,有過渡效果,不然兩個修改高度的js在一起,它是不會有過渡的,可能跟瀏覽器的渲染有關系

<template>
    <div>
        <el-button plain type="danger" @click="toggleDiv" size="mini" style="margin-bottom: 10px;" >toggleDiv</el-button>
        <div class="box1" ref="box1" id="box1">
            <div class="box1-item"></div>
            <div class="box1-item"></div>
            <div class="box1-item"></div>
            <div class="box1-item"></div>
            <div class="box1-item"></div>
        </div>
    </div>
</template>
<script>
export default {
    name: 'Collapse',
    components: {
    },
    data() {
        return {
            isCollapse: false,
            styleObj: {}
        }
    },
    mounted() {
        console.log(this.$refs['box1'].scrollHeight); // 110
        this.$refs['box1'].style.height = 'auto'
        this.$refs['box1'].style.height = window.getComputedStyle(this.$refs['box1']).height
    },
    methods: {
        toggleDiv() {
            this.isCollapse = !this.isCollapse
            if(this.isCollapse) {
                this.$refs['box1'].style.height = 0
            } else {
                this.$refs['box1'].style.height = 'auto'
                let height = window.getComputedStyle(this.$refs['box1']).height
                console.log(height,1);
                this.$refs['box1'].style.height = 0
                setTimeout(()=> {
                    this.$refs['box1'].style.height = height
                })
            }
        }
    }
}
</script>
<style>
.box1 {
    width: 200px;
    background-color: #bfa;
    overflow: hidden;
    transition: all 0.5s;
}
.box1 .box1-item {
    height: 20px;
    border: 1px solid red;
}
</style>

通過grid實現

注意下面的grid網格布局是加給外面的這個容器,外面這個容器從0fr到1fr會產生動畫。overflow:hidden是加給里面的這個div。這樣就能實現從0->auto的高度變化過渡效果。

<!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>Document</title>
    <style>
        body {
            margin: 0;
        }
        .quick-example {
            margin: 1rem;
            padding: 1rem;
            background: hsl(200, 50%, 50% );
            border-radius: 0.5rem;
            display: grid;
            grid-template-rows: 0fr;
            transition: grid-template-rows 0.5s;
        }
        .quick-example>div {
            overflow: hidden;
        }
        .quick-example:hover {
            grid-template-rows: 1fr;
        }
    </style>
</head>
<body>
    <div class="quick-example">
        <div>
            this is amazing!
            this is amazing!
            this is amazing!
            this is amazing!
            this is amazing!
            this is amazing!
            this is amazing!
            this is amazing!
        </div>
    </div>
    <div>
    	[一段很長的文字Lorem1000]
    </div>
</body>
</html>

到此這篇關于vue實現折疊展開收縮動畫的文章就介紹到這了,更多相關vue折疊展開收縮動畫內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 淺談Vue-cli 命令行工具分析

    淺談Vue-cli 命令行工具分析

    本篇文章主要介紹了淺談Vue-cli 命令行工具分析,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • 基于vue開發(fā)的在線付費課程應用過程

    基于vue開發(fā)的在線付費課程應用過程

    這篇文章主要介紹了基于vue開發(fā)的在線付費課程應用過程,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2018-01-01
  • vue-cli的eslint相關用法

    vue-cli的eslint相關用法

    本篇文章主要介紹了vue-cli的eslint相關用法,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • vue3中如何使用Pinia實現數據持久化操作

    vue3中如何使用Pinia實現數據持久化操作

    使用vue3中的pinia,我們可以在多個頁面間共享數據,但是一旦我們關閉或刷新頁面,這些數據就會丟失,因此,我們需要有一種數據持久化的解決方案,下面我們就來看看具體如何解決的吧
    2023-10-10
  • vue-create創(chuàng)建VUE3項目詳細圖文教程

    vue-create創(chuàng)建VUE3項目詳細圖文教程

    create-vue是Vue官方新的腳手架工具,底層切換到了vite(下一代前端工具鏈),為開發(fā)提供極速響應,下面這篇文章主要給大家介紹了關于vue-create創(chuàng)建VUE3項目的相關資料,需要的朋友可以參考下
    2024-03-03
  • Vue的虛擬DOM和diff算法你了解嗎

    Vue的虛擬DOM和diff算法你了解嗎

    這篇文章主要為大家詳細介紹了Vue的虛擬DOM和diff算法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02
  • VUE2—defineProperty和VUE3—proxy使用方式

    VUE2—defineProperty和VUE3—proxy使用方式

    Vue2和Vue3的響應式原理不同,Vue2使用Object.defineProperty,Vue3使用Proxy,Object.defineProperty可以監(jiān)聽某個屬性,但不能監(jiān)聽整個對象,且無法監(jiān)聽對象屬性的新增和刪除,Proxy可以監(jiān)聽整個對象,且不會修改原數據,可以監(jiān)聽數組的長度變化
    2025-01-01
  • tomcat部署前端vue項目步驟(項目上線具體操作)

    tomcat部署前端vue項目步驟(項目上線具體操作)

    在實際開發(fā)中,我們經常會遇到將Vue項目部署到Tomcat服務器上的需求,下面這篇文章主要給大家介紹了關于tomcat部署前端vue項目(項目上線具體操作)的相關資料,需要的朋友可以參考下
    2024-07-07
  • Vue生命周期詳解

    Vue生命周期詳解

    這篇文章詳細介紹了Vue的生命周期,文中通過代碼示例介紹的非常詳細。對大家的學習有一定的參考借鑒價值,需要的朋友可以參考下
    2023-04-04
  • Vue3使用defineAsyncComponent實現異步組件加載的代碼示例

    Vue3使用defineAsyncComponent實現異步組件加載的代碼示例

    在 Vue 3 中,異步組件加載是一種優(yōu)化應用性能的重要手段,通過異步加載組件,可以減少初始加載時的資源體積,從而提升應用的加載速度和用戶體驗,本文將詳細介紹如何使用 defineAsyncComponent 實現異步組件加載,并提供相關的代碼示例,需要的朋友可以參考下
    2025-03-03

最新評論

工布江达县| 阳新县| 松滋市| 手游| 隆化县| 通河县| 库伦旗| 宁武县| 望都县| 自贡市| 巨野县| 滕州市| 临朐县| 沁水县| 邓州市| 虹口区| 定日县| 抚州市| 贡嘎县| 长岭县| 墨脱县| 黑河市| 莱芜市| 民和| 岳阳市| 南雄市| 久治县| 门源| 双辽市| 平南县| 清水县| 营口市| 巴彦淖尔市| 临沂市| 永州市| 陆川县| 涡阳县| 鸡泽县| 阿克苏市| 盱眙县| 赤峰市|