angular 实现无缝滚动

  constructor(
    private api: ApiService,
    private renderer: Renderer2,//订阅angular这个服务
  ) { }  

 
  //页面向上滚动
  
//页面向上滚动
  offset() {
    let arr = this.noticeList //数据数组
    let dom: any = document.getElementsByClassName('tbody1')[0] //获取dom元素
    let h = dom.offsetHeight//获取dom元素的高度
    if (h) {
      this.noticeList = [...this.noticeList, ...arr]  //复制1份数据
      let offsetnum = this.noticeList.length//设置滚动最低数量
      this.domspeed = 14 / h //设置速度的参数 通过一个值除高度获得一个参数值 
      let hPrecent = 50  // 设置50百分比
      let n = 0//目前移动的点

      //防止面板数据只有1条 
      for (let i = 1; i <= offsetnum; i++) {
        if (this.noticeList.length < offsetnum) {
          this.noticeList = [...this.noticeList, ...arr]
        } else {
          i = offsetnum + 1
        }
      }
      this.Interval = setInterval(() => {
        if (n > hPrecent) {
          n = 0
          if (this.noticeList.length < offsetnum) {//防止数据少于
            this.noticeList = [...this.noticeList, ...arr]
          }
        }
        console.log(n);
        console.log(hPrecent);
        console.log(this.noticeList);

        n += this.domspeed
        this.renderer.setStyle(dom, 'transform', `translateY(-${n}%)`); //通过transform的translateY往上移动
      }, 10);

    }

  }

html

<div class="box" >
  <div style="overflow: hidden">
    <table border="0" cellspacing="0" cellpadding="0">
      <thead id="thead1" class="thead1" style="max-height: 50%; overflow: hidden;">
        <tr style="background-color: #012593;">
<td>{{item}}</td>
        </tr>
      </thead>
    </table>
  </div>
  <div style=" overflow: hidden">
    <table border="0" cellspacing="0" cellpadding="0">
      <tbody class="tbody1" style="max-height: 50%; overflow: hidden;">
        <tr *ngFor="let item of emparr; let i=index" [style]="item.q==1? bgc:''">
<td>{{item}}</td>
        </tr>
      </tbody>
    </table>
  </div>
</div>

css


.thead1 {
  tr>th {
    width: 260px;
    padding: 10px 0px;
  }
}

.tbody1 {
  height: 80px;

  tr>td {
    width: 260px;
    text-align: center;
    padding: 12px 6px;
  }
}

.box {
  height: 380px;//设置一个盒子把两个table放里面
  overflow: hidden;
  text-overflow: ellipsis;
  color: white;
}
//两个table的th 和td 的宽设置一样

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值