vue—實(shí)現(xiàn)點(diǎn)擊按鈕,滑出面板(滑出時(shí)下面的元素不會(huì)一起下滑)

element-uicollapse折疊面板時(shí)吻商,如果面板滑出的時(shí)候下面的元素會(huì)跟著一起下滑糟红,然后我現(xiàn)在的需求是:希望面板滑出的時(shí)候,下面的元素不會(huì)跟著下滑柒爸,就是這個(gè)面板在下面的元素之上捎稚。

效果圖

先看一下效果圖:


折疊面板.gif

實(shí)現(xiàn)思路

主要是運(yùn)用定位來(lái)實(shí)現(xiàn)面板元素脫離文檔流今野;代碼如下:

1、html

因?yàn)槲乙獙⑦@個(gè)封裝成一個(gè)組件条霜,所以使用了<slot></slot>宰睡,這樣別人使用這個(gè)組件的時(shí)候面板里的內(nèi)容就可以自定義气筋。

<div class="collapse-wrap" :class="{'show-border-top': !iconShow}">
    <div class="collapse-down"  @click="funTransitionHeightIn(600)" v-show="iconShow">
        <div class="collapse-down-icon el-icon-caret-bottom"></div>
    </div>
    <div ref="collapse" class="collapse-content-wrap">
        <div class="collapse-content">
           <slot></slot>
        </div>
    </div>
    <div class="collapse-down" @click="funTransitionHeightOut(600)" v-show="!iconShow">
        <div class="collapse-down-icon el-icon-caret-top"></div>
    </div>
</div>
2宠默、css
.collapse-wrap{
    position: absolute;
    width: 100%;
    border-bottom: 1px solid #ebeef5;
    z-index: 1001;
    box-sizing: border-box;
}
.collapse-wrap .collapse-down {
    position: absolute;
    bottom: -17.5px;
    left: 50%;
    margin-left: -17.5px;
    background-color: #fff;
    width: 35px;
    height: 35px;
    border-radius: 20px;
    cursor: pointer;
    transition: .3s;
    box-shadow: 0 0 6px rgba(0,0,0,.12);
    z-index: 1000;
}
.collapse-wrap .collapse-down-icon {
    color: #409eff;
    display: block;
    line-height: 35px;
    text-align: center;
    font-size: 18px;
}
.collapse-wrap .collapse-content-wrap {
    position: relative;
    top: 0;
    left: 0;
    width: 100%;
    z-index: 999;
    background-color: #fff;
    height: 0;
    overflow: hidden;
}
.collapse-wrap .collapse-content-wrap .collapse-content{
    padding: 10px;
}
.show-border-top {
    border-top: 1px solid #ebeef5;
}
.show-border-side {
    border-right: 1px solid #ebeef5;
    border-left: 1px solid #ebeef5;
}
3搀矫、封裝成組件

當(dāng)你們要使用的時(shí)候,直接引用collapse-component.js采够,然后調(diào)用就可以了冰垄。里面的根據(jù)高度自適應(yīng)寫動(dòng)畫借鑒的是張?chǎng)涡?/a>的代碼。
collapse-component.js 內(nèi)容:

/**
 * ---組件--不擠掉下面元素的折疊面板
 1逝薪、模板:
    <collapse-component :border-show = 'true'>
        <template>
            ...
        </template>
    </collapse-component>
2董济、參數(shù)說(shuō)明
    :border-show = 'true'  // 控制左右兩邊的邊框是否顯示要门;true:顯示;false:不顯示
    <template>
        ...     // 里面的內(nèi)容自定義
    </template>
 */
Vue.component('collapse-component',{
    data: function(){
        return {
            iconShow: true
        }
    },
    props: ['borderShow'],
    template: 
        '<div class="collapse-wrap" :class="{\'show-border-top\': !iconShow,\'show-border-side\': borderShow}">\
            <div class="collapse-down"  @click="funTransitionHeightIn(600)" v-show="iconShow">\
                <div class="collapse-down-icon el-icon-caret-bottom"></div>\
            </div>\
            <div ref="collapse" class="collapse-content-wrap">\
                <div class="collapse-content">\
                    <slot></slot>\
                </div>\
            </div>\
            <div class="collapse-down" @click="funTransitionHeightOut(600)" v-show="!iconShow">\
                <div class="collapse-down-icon el-icon-caret-top"></div>\
            </div>\
        </div>',
    methods:{
        // 顯示
        funTransitionHeightIn : function(time) { // time, 數(shù)值封豪,可缺省
            if (typeof window.getComputedStyle == "undefined") return;
            this.iconShow = false;
            element = this.$refs.collapse;
            var height = window.getComputedStyle(element).height;
            element.style.transition = "none";    // mac Safari下,貌似auto也會(huì)觸發(fā)transition, 故要none下~
           
            element.style.height = "auto";
            var targetHeight = window.getComputedStyle(element).height;
            element.style.height = height;
            element.offsetWidth = element.offsetWidth;
            if (time) element.style.transition = "height "+ time +"ms";
            element.style.height = targetHeight;
        },
        // 折疊
        funTransitionHeightOut : function(time) { // time, 數(shù)值炒瘟,可缺省
            if (typeof window.getComputedStyle == "undefined") return;
            height = window.getComputedStyle(element).height;

            element.style.transition = "none";    // mac Safari下吹埠,貌似auto也會(huì)觸發(fā)transition, 故要none下~
            targetHeight = "0px";
            element.style.height = height;
            element.offsetWidth = element.offsetWidth;
            if (time) element.style.transition = "height "+ time +"ms";
            element.style.height = targetHeight;
            var _this = this;
            window.setTimeout(function(){
                _this.iconShow = true;
            },time);
        }
    }
});
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市疮装,隨后出現(xiàn)的幾起案子缘琅,更是在濱河造成了極大的恐慌,老刑警劉巖廓推,帶你破解...
    沈念sama閱讀 217,826評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件刷袍,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡樊展,警方通過(guò)查閱死者的電腦和手機(jī)做个,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)滚局,“玉大人太闺,你說(shuō)我怎么就攤上這事省骂。” “怎么了怠惶?”我有些...
    開封第一講書人閱讀 164,234評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵策治,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我履腋,道長(zhǎng)遵湖,這世上最難降的妖魔是什么延旧? 我笑而不...
    開封第一講書人閱讀 58,562評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮闷盔,結(jié)果婚禮上牡整,老公的妹妹穿的比我還像新娘逃贝。我一直安慰自己沐扳,他們只是感情好沪摄,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,611評(píng)論 6 392
  • 文/花漫 我一把揭開白布祈餐。 她就那樣靜靜地躺著帆阳,像睡著了一般。 火紅的嫁衣襯著肌膚如雪离赫。 梳的紋絲不亂的頭發(fā)上塌碌,一...
    開封第一講書人閱讀 51,482評(píng)論 1 302
  • 那天翎猛,我揣著相機(jī)與錄音切厘,去河邊找鬼疫稿。 笑死遗座,一個(gè)胖子當(dāng)著我的面吹牛途蒋,可吹牛的內(nèi)容都是我干的号坡。 我是一名探鬼主播宽堆,決...
    沈念sama閱讀 40,271評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼墩划!你這毒婦竟也來(lái)了嗡综?” 一聲冷哼從身側(cè)響起察净,我...
    開封第一講書人閱讀 39,166評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎译秦,沒(méi)想到半個(gè)月后筑悴,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體阁吝,經(jīng)...
    沈念sama閱讀 45,608評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,814評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了摔刁。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片共屈。...
    茶點(diǎn)故事閱讀 39,926評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡幌衣,死狀恐怖壤玫,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情猎贴,我是刑警寧澤她渴,帶...
    沈念sama閱讀 35,644評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站苛败,受9級(jí)特大地震影響纱扭,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜暗赶,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,249評(píng)論 3 329
  • 文/蒙蒙 一因惭、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧激率,春花似錦乒躺、人聲如沸嘉冒。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,866評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至镰吆,卻和暖如春万皿,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背芝雪。 一陣腳步聲響...
    開封第一講書人閱讀 32,991評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留抒抬,地道東北人擦剑。 一個(gè)月前我還...
    沈念sama閱讀 48,063評(píng)論 3 370
  • 正文 我出身青樓惠勒,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親巾遭。 傳聞我的和親對(duì)象是個(gè)殘疾皇子吼和,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,871評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容