当前位置:首页 > VUE

vue实现点击查看更多

2026-01-23 02:34:35VUE

Vue 实现点击查看更多功能

基础实现方案

使用 v-showv-if 控制内容显示状态,通过点击事件切换状态:

<template>
  <div>
    <p v-show="!isExpanded">{{ truncatedText }}</p>
    <p v-show="isExpanded">{{ fullText }}</p>
    <button @click="toggleExpand">
      {{ isExpanded ? '收起' : '查看更多' }}
    </button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      fullText: '这里是完整的长文本内容...',
      isExpanded: false
    }
  },
  computed: {
    truncatedText() {
      return this.fullText.slice(0, 100) + '...'
    }
  },
  methods: {
    toggleExpand() {
      this.isExpanded = !this.isExpanded
    }
  }
}
</script>

动态内容截断方案

当需要处理动态内容时,可以使用 ref 获取DOM元素计算高度:

<template>
  <div>
    <div ref="content" :class="{ 'line-clamp': !expanded }">
      {{ content }}
    </div>
    <button @click="expanded = !expanded">
      {{ expanded ? '收起' : '查看更多' }}
    </button>
  </div>
</template>

<script>
export default {
  props: ['content'],
  data() {
    return {
      expanded: false
    }
  }
}
</script>

<style>
.line-clamp {
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
</style>

列表数据加载更多

对于分页加载的场景,可以结合事件触发加载:

<template>
  <div>
    <ul>
      <li v-for="item in visibleItems" :key="item.id">
        {{ item.name }}
      </li>
    </ul>
    <button 
      v-if="hasMore" 
      @click="loadMore"
      :disabled="loading">
      {{ loading ? '加载中...' : '加载更多' }}
    </button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      allItems: [], // 所有数据
      visibleCount: 5,
      loading: false
    }
  },
  computed: {
    visibleItems() {
      return this.allItems.slice(0, this.visibleCount)
    },
    hasMore() {
      return this.visibleCount < this.allItems.length
    }
  },
  methods: {
    loadMore() {
      this.loading = true
      // 模拟异步加载
      setTimeout(() => {
        this.visibleCount += 5
        this.loading = false
      }, 500)
    }
  }
}
</script>

动画过渡效果

为展开/收起添加平滑过渡:

<template>
  <div>
    <div class="content-wrapper">
      <transition name="expand">
        <p v-if="expanded" key="full">{{ text }}</p>
        <p v-else key="truncated">{{ truncatedText }}</p>
      </transition>
    </div>
    <button @click="expanded = !expanded">
      {{ expanded ? '收起' : '查看更多' }}
    </button>
  </div>
</template>

<style>
.expand-enter-active, .expand-leave-active {
  transition: all 0.3s ease;
  overflow: hidden;
}
.expand-enter, .expand-leave-to {
  max-height: 0;
  opacity: 0;
}
.content-wrapper {
  position: relative;
}
</style>

响应式截断

根据容器宽度自动计算截断位置:

vue实现点击查看更多

<template>
  <div ref="container" class="truncate-container">
    <p ref="content">{{ text }}</p>
    <span 
      v-if="needsTruncation && !expanded" 
      class="ellipsis">...</span>
    <button 
      v-if="needsTruncation" 
      @click="expanded = !expanded">
      {{ expanded ? '收起' : '查看更多' }}
    </button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      expanded: false,
      needsTruncation: false
    }
  },
  mounted() {
    this.checkTruncation()
    window.addEventListener('resize', this.checkTruncation)
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.checkTruncation)
  },
  methods: {
    checkTruncation() {
      this.$nextTick(() => {
        const container = this.$refs.container
        const content = this.$refs.content
        this.needsTruncation = content.scrollWidth > container.offsetWidth
      })
    }
  }
}
</script>

<style>
.truncate-container {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  position: relative;
  padding-right: 80px;
}
.truncate-container p {
  display: inline;
}
.ellipsis {
  position: absolute;
  right: 60px;
  background: white;
}
</style>

分享给朋友:

相关文章

vue加载更多实现

vue加载更多实现

滚动监听实现加载更多 通过监听滚动事件判断是否到达底部触发加载。在组件的mounted钩子中添加滚动事件监听器,计算滚动位置与文档高度的关系。 mounted() { window.addEve…

css制作更多

css制作更多

使用CSS制作更多效果 CSS(层叠样式表)是现代网页设计的核心工具之一,通过灵活运用CSS可以实现丰富的视觉效果和交互功能。以下是一些实用的CSS技巧和方法,帮助开发者制作更多样化的网页效果。 动…

vue实现显示更多

vue实现显示更多

Vue 实现显示更多功能 在Vue中实现显示更多功能可以通过动态控制内容的显示与隐藏来实现。以下是几种常见的方法: 使用v-show或v-if控制显示 通过一个布尔值变量来控制内容的显示与隐藏,点击…

vue 实现加载更多

vue 实现加载更多

Vue 实现加载更多功能 数据准备与状态管理 在Vue组件中定义必要的数据状态,包括当前页码、每页数据量、总数据量以及数据列表。使用data或ref(Composition API)进行初始化: d…

vue实现图片显示更多

vue实现图片显示更多

实现图片显示更多功能 在Vue中实现图片显示更多功能,可以通过动态加载或懒加载的方式处理大量图片,避免一次性渲染所有图片导致性能问题。 基础实现方法 创建一个组件,使用v-for循环渲染图片列表,…

vue实现点击查看大图

vue实现点击查看大图

实现点击查看大图功能 在Vue中实现点击查看大图功能,可以通过以下几种方式完成: 使用Element UI的Dialog组件 如果项目中使用Element UI,可以通过Dialog组件快速实现大图…