vue监听页面元素高度实时变化及其案例(实现文本多行溢出省略,点击后显示全部功能)

4 篇文章 1 订阅
4 篇文章 0 订阅

vue监听页面元素高度实时变化及其案例(实现文本多行溢出省略,点击后显示全部功能)

效果预览

在这里插入图片描述

思路

需要完成一个文章列表,其中文章简介超过两行则省略,并可点击展开全部内容。
原思路是给渲染简介的div一个多行文本溢出,但是此时监听到的该块高度只有两行的高度,无法判断是否溢出。

{
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    overflow: hidden;
}

因此在目标div外嵌套一个盒子,给予盒子固定高度并溢出隐藏,盒子内部内容不限制高度。
并且通过ref获取到盒子内容的实际高度,存入数组用于判断是否溢出。
定义一个showAll用于区分是否已展开,展开则取消溢省略样式。
在这里插入图片描述

<style>
    .introbox{ transition: all .5s;}
    .introbox.hidden{ position: relative; height: 40px; overflow: hidden;}
    .introbox .intro{ color: #999; font-size: 13px; line-height: 20px;}
    .introbox.hidden::after{ content: '...'; position: absolute; right: 0; bottom: 0; color: #999; font-size: 13px; line-height: 20px; background-color: #fff; z-index: 2;}
</style>
<div class="introbox" :style="{'height': allHeight[idx] + 'px'}" :class="{'hidden': allHeight[idx] > 40 && !showAll[idx]}">
    <div class="intro" ref="text">{{item.intro}}</div>
</div>
<div class="show" v-show="allHeight[idx] > 40" @click="$set(showAll, idx, !showAll[idx])">{{showAll[idx] ? '收起' : '展开'}}</div>

this.$nextTick(() => {
    // 循环计算出需要溢出省略块的高度,存入数组
    for(let i in this.list){
        this.allHeight.push(this.$refs.text[i].offsetHeight);
        this.showAll.push(false);
    }
})

此时给嵌套在外面的盒子添加一个伪类用来模拟有溢出时的省略号(字号颜色及行高与内容一致),将其定位在盒子尾部,设置背景色及层级模拟多行文本溢出省略的效果。

完整代码

<style>
    *{margin: 0; padding: 0;}
    .index_page{ width: 100%; min-height: 100%; padding: 1px 0; background-color: #f8f8f8;}
    .index_page .list{ display: flex; margin: 10px; padding: 10px; border-radius: 5px; background-color: #fff;}
    .index_page .list .img{ width: 60px; height: 60px; border-radius: 5px; margin-right: 10px; overflow: hidden;}
    .index_page .list .cont{ flex: 1 1 0; overflow: hidden;}
    .index_page .list .cont .title{ color: #000; font-size: 16px; text-overflow: ellipsis; white-space: nowrap; overflow: hidden; line-height: 1.5; margin: 0;}
    .index_page .list .cont .introbox{ transition: all .5s;}
    .index_page .list .cont .introbox.hidden{ position: relative; height: 40px; overflow: hidden;}
    .index_page .list .cont .introbox .intro{ color: #999; font-size: 13px; line-height: 20px;}
    .index_page .list .cont .introbox.hidden::after{ content: '...'; position: absolute; right: 0; bottom: 0; color: #999; font-size: 13px; line-height: 20px; background-color: #fff; z-index: 2;}
    .index_page .list .cont .show{ display: inline-block; color: #0C81E0; float: right; font-size: 13px;}
</style>
<body>
<div id="index" class="index_page">
    <div class="list" v-for="(item, idx) in list">
        <div class="img"><img src="https://img1.baidu.com/it/u=2728776560,2236636365&fm=26&fmt=auto&gp=0.jpg" alt=""></div>
        <div class="cont">
            <h3 class="title">vue监听页面元素高度实时变化及其案例</h3>
            <div class="introbox" :style="{'height': allHeight[idx] + 'px'}" :class="{'hidden': allHeight[idx] > 40 && !showAll[idx]}">
                <div class="intro" ref="text">{{item.intro}}</div>
            </div>
            <div class="show" v-show="allHeight[idx] > 40" @click="$set(showAll, idx, !showAll[idx])">{{showAll[idx] ? '收起' : '展开'}}</div>
        </div>
    </div>
</div>
<script>
    var indexVm = new Vue({
        el: '#index',
        data() {
            return {
                list: [],
                subHeight: '',
                allHeight: [],
                showAll: []
            }
        },
        mounted() {
            this.list = [
                {intro: 'vue监听页面元素高度实时变化及其案例vue监听页面元素高度实时变化及其案例vue监听页面元素高度实时变化及其案例vue监听页面元素高度实时变化及其案例vue监听页面元素高度实时变化及其案例vue监听页面元素高度实时变化及其案例vue监听页面元素高度实时变化及其案例vue监听页面元素高度实时变化及其案例vue监听页面元素高度实时变化及其案例vue监听页面元素高度实时变化及其案例'},
                {intro: 'vue监听页面元素高度实时变化及其案例vue监听页面元素高度'},
                {intro: 'vue监听页面元素高度实时变化及其案例vue监听页面元素高度实时变化及其案例vue监听页面元素高度实时变化及其案例vue监听页面元素高度实时变化及其案例vue监听页面元素高度实时变化及其案例vue监听页面元素高度实时变化及其案例vue监听页面元素高度实时变化及其案例vue监听页面元素高度实时变化及其案例vue监听页面元素高'},
                {intro: 'vue监听页面元素高度实时变化及其案例vue'},
                {intro: 'vue监听页面元素高度实时变化及其案例vue监听'},
            ];
        },
        watch: {
            list: function(){
                this.$nextTick(() => {
                    // 循环计算出需要溢出省略块的高度,存入数组
                    for(let i in this.list){
                        this.allHeight.push(this.$refs.text[i].offsetHeight);
                        this.showAll.push(false);
                    }
                })
            }
        }
    })
</script>
</body>
  • 28
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
要在 Vue监听 DOM 元素高度变化,您可以使用 Vue 的 `$nextTick` 方法和 `Intersection Observer API`。 首先,在模板中,您需要使用一个具有固定高度的外层元素包裹您要监听高度变化元素,例如: ```html <template> <div class="container"> <div class="content" ref="myContent"></div> </div> </template> <style> .container { height: 200px; overflow: auto; } .content { height: 400px; } </style> ``` 上面的代码中,我们在外层使用了一个具有固定高度和滚动条的容器,并在内部添加了一个高度为 400px 的 `<div>` 元素。 接下来,在 Vue 实例中,您需要通过 `$refs` 来获取要监听高度变化元素,并使用 `Intersection Observer API` 来监听元素变化,例如: ```js export default { mounted() { const observer = new IntersectionObserver((entries) => { entries.forEach((entry) => { if (entry.isIntersecting) { this.$nextTick(() => { console.log(this.$refs.myContent.clientHeight) }); } }); }); observer.observe(this.$refs.myContent); } } ``` 上面的代码中,我们在 Vue 实例的 `mounted` 生命周期钩子中创建了一个 `IntersectionObserver`,并通过 `$refs` 获取了要监听高度变化元素。然后,我们使用 `IntersectionObserver` 监听元素变化,并在元素进入视口时,使用 `$nextTick` 方法获取元素的新高度,并打印到控制台中。 需要注意的是,`$nextTick` 方法会在 DOM 更新后执行回调函数,以确保获取到的元素高度是最新的。这是因为 Vue 的更新是异步的,在修改 DOM 后,您无法立即获取到最新的 DOM 元素高度

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天外来鹿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值