vue 点击加载更多

然后按照需求一步步来,先写基本页面

如果不想看过程,直接去后面完整代码那就可以

<template>
  <div>
    <div>
      <div class="Journalism" ref="div">
        <div v-for="item in todos" :key="item.id" ref="show">
          <span>{{item.title}}</span>
          <span>{{item.time}}</span>
        </div>
      </div>
      <div class="JournalismShow">显示更多<i class="el-icon-arrow-down"></i></div>
    </div>
  </div>
</template>
<script>
export default {
  data(){
    return{
      todos:[
        {id:"1",title:"31省区市新增本土确诊80例、无症状21例",time:'2021-08-06'},
        {id:"2",title:"江苏新增本土确诊病例61例 其中扬州58例",time:'2021-08-06'},
        {id:"3",title:"因为这件事 美国又打脸谭德塞!",time:'2021-08-06'},
        {id:"4",title:"恶劣!拜登签署一份干涉港备忘录",time:'2021-08-06'},
        {id:"5",title:"巴萨官宣梅西正式离队",time:'2021-08-06'},
        {id:"6",title:"深圳甩出“王炸” 学区房炒作能否彻底“熄火”?",time:'2021-08-06'},
        {id:"7",title:"伊朗新总统刚就职 美国就喊话了",time:'2021-08-06'},
        {id:"8",title:"“全省最差场地” 全红婵儿时就在这里训练",time:'2021-08-06'},
        {id:"9",title:"湖北新增6例本土确诊、12例本土无症状",time:'2021-08-06'},
        {id:"10",title:"河南新增1例本土确诊、8例本土无症状",time:'2021-08-06'}
      ],
    }
  },
}
</script>

<style scoped lang="scss">
.Journalism{
  width: 500px;
  div{
    display: flex;
    justify-content: space-between;
    span{
      height: 50px;
      font-size: 18px;
      color: #666;
      line-height: 50px;
    }
  }
}

.JournalismShow{
  position: absolute;
  width: 500px;
  text-align: center;
  cursor:pointer;
}
</style>

效果:

后面只贴变化的代码

 需求:默认显示5条其余点击显示更多后显示

<template>
  <div>
    <div>
      <div class="Journalism" :class="{active:flag}" ref="div">
        <!-- 动态判断是否显示隐藏部分 -->
        <div v-for="item in todos" :key="item.id" ref="show">
          <span>{{item.title}}</span>
          <span>{{item.time}}</span>
        </div>
      </div>
      <div class="JournalismShow" @click="showTag">显示更多<i :class="flag ? 'el-icon-arrow-down' :'el-icon-arrow-up'"></i></div>
      <!-- 动态判断显示的图标 -->
    </div>
  </div>
</template>
<script>
export default {
  data(){
    return{
      flag:true
    }
  },
  methods:{
    //点击后切换状态
    showTag(){
      this.flag=!this.flag
    }
  }
}
</script>

<style scoped lang="scss">
//默认隐藏
.active{
  overflow: hidden;
  height: 250px;
}
</style>

 页面效果:

写到现在我们可以发现css中高度是写死的,上边代码中的div高度都是一样的。

还有一种就是超出宽度换行显示,这种情况写死高度就会出现有可能某一行高度显示不完整

解决:

<template>
  <div>
    <div>
      <!-- scss使用js变量 -->
      <div :style="{'--divHeight':divHeight,'--showHeight':showHeight}" class="Journalism" :class="{active:flag}" ref="div">
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data(){
    return{
      divHeight:"",
      showHeight:"",
    }
  },
  methods:{
    // 获取高度
    getHeight(){
      this.divHeight=this.$refs['div'].clientHeight+"px"
      let numb=0
      let arr=this.$refs['show']
      arr.forEach((item,index)=>{
        if(index<5){
          numb+=item.clientHeight
        }
      })
      this.showHeight=numb+'px'
    },
  },
  mounted(){
    this.getHeight()
  }
}
</script>

<style scoped lang="scss">
//默认隐藏
.active{
  overflow: hidden;
  height: var(--showHeight);
}
</style>

到现在页面基本完成,再设置下css样式就可了。本来是想用过渡的,但是过渡效果需要伪类激活,所以我们选择css动画

<style scoped lang="scss">
.Journalism{
  width: 500px;
  overflow: hidden;
  animation-name: examp;
  animation-duration: 1s;
  div{
    display: flex;
    justify-content: space-between;
    span{
      height: 50px;
      font-size: 18px;
      color: #666;
      line-height: 50px;
    }
  }
}
//默认隐藏
.active{
  overflow: hidden;
  height: var(--showHeight);
  animation-name: example;
  animation-duration: 1s;
}

@keyframes example {
  from {height: var(--divHeight);}
  to {height: var(--showHeight);}
}
@keyframes examp {
  from {height: var(--showHeight);}
  to {height: var(--divHeight);}
}
</style>

页面效果

 

页面写到这里本来以为结束了,但是实验的时候就又发现了问题,刷新页面或者从其他页面跳到此页面后就会先执行了一次的动画,并且页面会闪烁一下。

我想要的是点击后才出现动画,解决的方式也是简单粗暴到让人窒息

<template>
  <div>
    <div class="top">
      <div :style="{'--divHeight':divHeight,'--showHeight':showHeight}" class="Journalism" :class="flag==0 ?  '' : (flag==1 ?  'active2' : 'active')" ref="div">
        <!-- 使用三目运算判断是否执行或执行某一个 -->
        <div v-for="item in todos" :key="item.id" ref="show">
          <span>{{item.title}}</span>
          <span>{{item.time}}</span>
        </div>
      </div>
      <div class="JournalismShow" @click="showTag">显示更多<i :class="flag==1 ?  'el-icon-arrow-up' : 'el-icon-arrow-down'"></i></div>
      <!-- 使用三目运算判断图标的方向 -->
    </div>
  </div>
</template>
<script>
export default {
  data(){
    return{
      // 开始的默认值
      flag:0,
    }
  },
  methods:{
    showTag(){
      // 使用if判断flag的值
      if(this.flag==0){
        this.flag=1
      }else if(this.flag==1){
        this.flag=2
      }else{
        this.flag=1
      }
    }
  }
}
</script>

<style scoped lang="scss">
.Journalism{
  width: 500px;
  overflow: hidden;
  height: var(--showHeight);
  div{
    display: flex;
    justify-content: space-between;
    span{
      height: 50px;
      font-size: 18px;
      color: #666;
      line-height: 50px;
    }
  }
}
.JournalismShow{
  position: absolute;
  width: 500px;
  text-align: center;
  cursor:pointer;
}
.active{
  overflow: hidden;
  height: var(--showHeight);
  animation-name: example;
  animation-duration: 1s;
}
.active2{
  height: var(--divHeight);
  animation-name: examp;
  animation-duration: 1s;
}

@keyframes example {
  from {height: var(--divHeight);}
  to {height: var(--showHeight);}
}
@keyframes examp {
  from {height: var(--showHeight);}
  to {height: var(--divHeight);}
}
</style>

 完整代码:

<template>
  <div>
    <div class="top">
      <div :style="{'--divHeight':divHeight,'--showHeight':showHeight}" class="Journalism" :class="flag==0 ?  '' : (flag==1 ?  'active2' : 'active')" ref="div">
        <div v-for="item in todos" :key="item.id" ref="show">
          <span>{{item.title}}</span>
          <span>{{item.time}}</span>
        </div>
      </div>
      <div class="JournalismShow" @click="showTag">显示更多<i :class="flag==1 ?  'el-icon-arrow-up' : 'el-icon-arrow-down'"></i></div>
    </div>
  </div>
</template>
<script>
export default {
  data(){
    return{
      todos:[
        {id:"1",title:"31省区市新增本土确诊80例、无症状21例",time:'2021-08-06'},
        {id:"2",title:"江苏新增本土确诊病例61例 其中扬州58例",time:'2021-08-06'},
        {id:"3",title:"因为这件事 美国又打脸谭德塞!",time:'2021-08-06'},
        {id:"4",title:"恶劣!拜登签署一份干涉港备忘录",time:'2021-08-06'},
        {id:"5",title:"巴萨官宣梅西正式离队",time:'2021-08-06'},
        {id:"6",title:"深圳甩出“王炸” 学区房炒作能否彻底“熄火”?",time:'2021-08-06'},
        {id:"7",title:"伊朗新总统刚就职 美国就喊话了",time:'2021-08-06'},
        {id:"8",title:"“全省最差场地” 全红婵儿时就在这里训练",time:'2021-08-06'},
        {id:"9",title:"湖北新增6例本土确诊、12例本土无症状",time:'2021-08-06'},
        {id:"10",title:"河南新增1例本土确诊、8例本土无症状",time:'2021-08-06'}
      ],
      divHeight:'',
      showHeight:"250",
      flag:0,
    }
  },
  methods:{
    showTag(){
      if(this.flag==0){
        this.flag=1
      }else if(this.flag==1){
        this.flag=2
      }else{
        this.flag=1
      }
    },
    getHeight(){
      this.divHeight=this.$refs['div'].clientHeight+"px"
      let numb=0
      let arr=this.$refs['show']
      arr.forEach((item,index)=>{
        if(index<5){
          numb+=item.clientHeight
        }
      })
      this.showHeight=numb+'px'
    },
  },
  mounted(){
    this.getHeight()
  },
}
</script>

<style scoped lang="scss">
.Journalism{
  width: 500px;
  overflow: hidden;
  height: var(--showHeight);
  div{
    display: flex;
    justify-content: space-between;
    span{
      height: 50px;
      font-size: 18px;
      color: #666;
      line-height: 50px;
    }
  }
}
.JournalismShow{
  position: absolute;
  width: 500px;
  text-align: center;
  cursor:pointer;
}
.active{
  overflow: hidden;
  height: var(--showHeight);
  animation-name: example;
  animation-duration: 1s;
}
.active2{
  height: var(--divHeight);
  animation-name: examp;
  animation-duration: 1s;
}

@keyframes example {
  from {height: var(--divHeight);}
  to {height: var(--showHeight);}
}
@keyframes examp {
  from {height: var(--showHeight);}
  to {height: var(--divHeight);}
}
</style>

如果大家有更好的方法,欢迎告知!!!

  • 5
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值