vue中一个标题过多,选中时自动滚动到中间

废话少说,先看效果

效果图

下面放代码

<template>
  <div>
    <div class="info">
      <div class="info-list">
        <div class="info-list-box" id="infomationTitleBox">
          <ul class="info-list-ul" id="infomationTitleUl">
            <li :class="item.isHighlight ? 'info-list-li highlight' : 'info-list-li'" v-for="item,index in titleList" :key="item.title + index" :id="'infomation' + index" @click="clickTitle(item, index)">
              {{ item.title }}
            </li>
          </ul>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
  export default {
    data() {
      return {
        titleList:[
          { title: '最新', isHighlight: true, },
          { title: '电影推荐', isHighlight: false, },
          { title: '濒危动物', isHighlight: false, },
          { title: '爱豆说环保', isHighlight: false, },
          { title: '环保书籍', isHighlight: false, },
          { title: '在线咨询', isHighlight: false, },
          { title: '政策文件', isHighlight: false, },
          { title: '低碳视频', isHighlight: false, },
          { title: '低碳艺术', isHighlight: false, },
          { title: '低碳标讯', isHighlight: false, },
          { title: '低碳福建', isHighlight: false, },
          { title: '低碳上海', isHighlight: false, },
          { title: '低碳北京', isHighlight: false, },
          { title: '低碳深圳', isHighlight: false, },
          { title: '低碳广州', isHighlight: false, },
          { title: '低碳天津', isHighlight: false, },
          { title: '低碳杭州', isHighlight: false, },
          { title: '低碳南京', isHighlight: false, },
          { title: '低碳湖北', isHighlight: false, },
          { title: '低碳四川', isHighlight: false, },
        ],
      }
    },
    computed: {
      width () {
        let domWidth = (document.getElementById('infomationTitleBox').offsetWidth) / 2
        return domWidth
      }
    },
    mounted() {},
    methods: {
      clickTitle(item, index) {
        // console.log(item)
        this.titleList.forEach((i) => {
          i.isHighlight = false
        });
        item.isHighlight = true
        let domId = 'infomation' + index
        let domLeft = document.getElementById(domId).offsetLeft
        let ulDom = document.getElementById('infomationTitleUl')
        // console.log(ulDom)
        if(domLeft > this.width) {
          ulDom.style.left = (this.width - domLeft) + 'px'
        } else {
          ulDom.style.left = 0
        }
      }
    }
  }
</script>
<style lang='scss' scoped>
.info {
  width: 60%;
  margin: 50px auto;
  .info-list {
    width: 100%;
    .info-list-box {
      width: 100%;
      position: relative;
      overflow: hidden;
      height: 40px;
      .info-list-ul {
        display: flex;
        position: absolute;
        transition: all .3s;
        .info-list-li {
          padding: 4px 12px;
          white-space: nowrap;
          cursor: pointer;
        }
        .info-list-li:hover {
          color: #409EFF;
        }
        .highlight {
          position: relative;
          background-color: #409EFF;
          color: #fff;
        }
        .highlight:hover {
          color: #fff;
        }
        .highlight::after {
          content: '';
          position: absolute;
          bottom: -5px;
          left: 50%;
          transform: translateX(-50%);
          width:0;
          height:0;
          border-left: 5px solid transparent;
          border-right: 5px solid transparent;
          border-top: 5px solid #409EFF;
        }
      }
    }

  }
}
</style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值