vue+原生JavaScript实现slideDown与slideUp[简单思路]

整个代码如下:

<template>
  <div style="width:400px;margin:100px auto;">
            <div class="con">
                    <div class="same_module">
                            <div class="title up" @click="slide($event)"><span>标题一</span><i class="arrow"></i></div>
                            <div class="detail" style="height:0">
                                    <div class="inner">
                                            <p>这是一段文本</p>
                                            <p>这是一段文本</p>
                                            <p>这是一段文本</p>
                                            <p>这是一段文本</p>
                                    </div>
                            </div>
                    </div>
            </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
        
    }
  },
    methods:{
        slide:function(event){
            let curTarget = event.currentTarget,
                    containsCurClass = curTarget.classList.contains("up"),
                    nextSibling = curTarget.nextSibling;
            while(nextSibling.nodeType == 3 && /\s/.test(nextSibling.nodeValue)){
                nextSibling = nextSibling.nextSibling;
            };
            let detailScrollHeight = nextSibling.scrollHeight;
            if(containsCurClass){
                    curTarget.classList.remove("up");
                    this.toggleSlide(nextSibling,detailScrollHeight,'500');
            }else{
                curTarget.classList.add("up");
                this.toggleSlide(nextSibling,0,'500');
            }
        },
        toggleSlide:function(dom,height,time){
            dom.style.transition = 'height ' + time + 'ms';
            dom.style.height = height + 'px';
        }
    }
}
</script>
<style scoped>
    .same_module{border:1px solid grey;}
    .title{color:#fff;height:30px;line-height:30px;background:blue;padding:0 10px;cursor: pointer;overflow: hidden;}
    .title span{vertical-align:middle;}
    .title .arrow{float: right;}
    .detail{overflow: hidden;}
    .detail .inner{padding:5px 10px;background: #808080;color:#fff;}
    .detail p{line-height: 26px;}
    .arrow{
        display: inline-block;
    border-top: 2px solid;
    border-right: 2px solid;
    width: 8px;
    height: 8px;
    border-color: #EA6000;
    transform: rotate(315deg);
        position: relative;
        transition: all 0.5s ease-in;
        transform-origin: center center;
        top:50%;
        margin-top:-10px;
    }
    .up .arrow{
            transform: rotate(135deg);
    }
    
</style>

参考地址:vue也可以 slidedown

转载于:https://www.cnblogs.com/moqiutao/p/10552736.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值