上下篇文章切换

上下篇文章切换(Vue.js)


<div>
  <p :style="{ visibility: isPrev ? 'unset' : 'hidden' }">
    <span>
      <div
        @click="previousPage"
      >&lt;&emsp;上一篇:{{ active != 0 ? totals[+active - 1].title : "" }}</div>
    </span>
  </p>
  <p :style="{ visibility: isNext ? 'unset' : 'hidden' }">
    <span>
      <div
        @click="nextPage"
      >下一篇:{{ active != totals.length - 1 ? totals[+active + 1].title : "" }}&emsp;&gt;</div>
    </span>
  </p>
</div>

totals(文章的数量)

data() {
    return {
      isPrev: true,
      isNext: false,
      active: 0
    };
  },
activated() {
    this.sanNew()
    this.currentId()
  },
methods: {
    sanNew() {
      //获取文章数据的接口
    },      
    previousPage() {
      //上一篇
      var index = this.totals.map(item => item.id).indexOf(+this.id)
      if (index != -1 && index != 0) {
        this.id = this.totals[index - 1].id
        this.sanNew()
      }
      this.currentId()
    },
    nextPage() {
      //下一篇
      var index = this.totals.map(item => item.id).indexOf(+this.id)
      if (index != -1 && index != this.totals.length - 1) {
        this.id = this.totals[index + 1].id
        this.sanNew()
      }
      this.currentId()
    },
    currentId() {
      for (let i in this.totals) {
        if (this.totals[i].id == this.id) {
          this.active = i
          this.isPrev = i == 0 ? false : true
          this.isNext = i == this.totals.length - 1 ? false : true
        }
      }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

傻狍子学前端

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

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

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

打赏作者

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

抵扣说明:

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

余额充值