業界トップクラスの求人数を誇る転職エージェントPR

リクルートグループのコネクションを活かした非公開求人も充実、他にはない好条件の求人と出会える

7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Vue.js readmore/もっと見る/続きを読むの実装方法

Last updated at Posted at 2019-11-01

vue.jsを使い、readmoreに関わっている実装方法をまとめてみました。

1、readmore表示のみ#

参考先:Quick Vue: How to implement ‘read more’ feature

効果図:##

表示したい文字数を設定すると、残りの部分はRead more...で表示されます。↓
スクリーンショット 2019-12-02 13.09.47.png
Read more...を押すと、全文が表示されます。ただし、省略の状態に戻れないです。↓
スクリーンショット 2019-12-02 13.09.54.png

コード##

readmore1.vue

<template>    
<div>   
<span v-if="!readMoreActivated">{{longText.slice(0, 20)}}   </span>    
<a class="" v-if="!readMoreActivated" @click="activateReadMore" href="#"> Read more...</a>        
<span v-if="readMoreActivated" v-html="longText"></span>    
</div>
</template>

<script>
export default {    
  name: "Readmore",    
  data(){        
  return {
  longText: `これはテスト。これはテスト。これはテスト。これはテスト。これはテスト。これはテスト。これはテスト。これはテスト。これはテスト。これはテスト。これはテスト。これはテスト。これはテスト。これはテスト。これはテスト。これはテスト。`,
    readMoreActivated: false    
 }
}
  ,methods: {    
   activateReadMore(){        
    this.readMoreActivated = true;    
   },
 }
}
</script>

2、表示したい文字数を設定すると、残りの部分は「...」で表示する#

参考先:表示したくない部分は省略

効果図:##

表示したい文字数を設定すると、残りの部分は「...」で表示します。ただし、...を押すことができず、ただの...です。
制限文字数は10の時(value.slice(0,10))、↓
スクリーンショット 2019-12-02 13.16.00.png
制限文字数は100の時(value.slice(0,100))、↓
スクリーンショット 2019-12-02 13.16.53.png

コード##

readmore2.vue
<template>
    <div class="title">{{ name | ellipsis}}</div>
</template>

<script>
    export default {
        name: "demo",
        data(){
            return {
                name:'これはテストです。これはテストです。これはテストです。これはテストです。これはテストです。これはテストです。これはテストです。これはテストです。これはテストです。これはテストです。これはテストです。これはテストです。これはテストです。これはテストです。これはテストです。これはテストです。これはテストです。これはテストです。これはテストです。これはテストです。',
            }
        },
        filters:{
            ellipsis(value){
                if (!value) return '';
                if (value.length > 10) {
                    return value.slice(0,10) + '...'
                }
                return value
            }
        }
    }
</script>

<style scoped>

</style>

3、readmore表示+readless表示#

参考先:github-read-more

効果図:##

表示したい文字数を設定すると、残りの部分はread moreで表示されます。↓
スクリーンショット 2019-12-02 13.22.10.png
read moreを押すと、全文が表示されます。↓また、read lessを押すと、省略文に戻ります。↑
スクリーンショット 2019-12-02 13.22.19.png

demo page

コード##

 $npm install vue-read-more --save
main.js
 import ReadMore from 'vue-read-more';
 
 Vue.use(ReadMore)
readmore3.vue
<template>
  <div class="hello">
    <read-more more-str="read more" :text="msg" link="#" less-str="read less" :max-chars="100"></read-more>
   
</template>

<script>
export default {
  data () {
    return {
      msg: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. ',
    }
  }
}
</script>
7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Qiita Conference 2025 will be held!: 4/23(wed) - 4/25(Fri)

Qiita Conference is the largest tech conference in Qiita!

Keynote Speaker

ymrl、Masanobu Naruse, Takeshi Kano, Junichi Ito, uhyo, Hiroshi Tokumaru, MinoDriven, Minorun, Hiroyuki Sakuraba, tenntenn, drken, konifar

View event details
7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?