当前位置:首页 > 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>

响应式截断

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

<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 实现更多展开

vue 实现更多展开

Vue 实现更多展开功能 在 Vue 中实现“更多展开”功能可以通过多种方式完成,以下是几种常见的实现方法: 使用 v-show 或 v-if 控制显示 通过 Vue 的指令 v-show 或 v-…

vue实现点击查看大图

vue实现点击查看大图

实现点击查看大图功能 在Vue中实现点击查看大图功能,可以通过自定义组件或使用第三方库(如vue-photo-preview)完成。以下是两种常见方法: 使用自定义组件 创建一个弹窗组件,点击小图…

js实现加载更多内容

js实现加载更多内容

实现滚动加载更多内容 使用Intersection Observer API监听元素是否进入视口,触发加载更多操作。这种方法性能较好,避免频繁计算滚动位置。 const observer = new…

js实现下拉加载更多

js实现下拉加载更多

实现下拉加载更多的步骤 监听滚动事件 通过监听窗口的滚动事件,判断用户是否滚动到页面底部。当滚动到接近底部时触发加载更多数据的函数。 window.addEventListener('scroll…

js实现更多

js实现更多

实现“更多”功能的 JavaScript 方法 监听滚动事件动态加载内容 通过监听页面滚动事件,当用户滚动到接近底部时触发加载更多内容。使用 window.innerHeight 和 document…