VUE Angular通用动态列表组件-亦可为自动轮播组件-01-纵向滚动,鼠标划入停止滚动

17 篇文章 2 订阅

本文为纵向轮播,横向轮播/动态列表组件请戳----

代码是angular的,稍微改改就可以放入Vue项目里,差别不大哟

以下代码可以根据实际情况自行调整

父组件html

<app-scroll-up [dataObj]="dataObjUp">
  <div class="slot-one">
    <div [style]="{  height: dataObjUp.height + dataObjUp.unit }" class="inner_item"
      *ngFor="let item of dataObjUp.scrollList,index as i">
      内容:{{ item }}介绍:我是嵌入的slot{{ i+1 }},相当于vue的slot
    </div>
  </div>
  <div class="slot-two">
    <div [style]="{ height: dataObjUp.height + dataObjUp.unit }" class="inner_item">
      {{ dataObjUp.scrollList[0] }}嵌入的slot2,这里要放和 slot1里面第一个画面一模一样的东西
    </div>
  </div>
</app-scroll-up>


父组件ts

dataObjUp = {
    time: 50,
    minLength: 1,
    width:200,
    height: 200,
    unit: 'px',
    scrollList: ['1111','2222','3333','444','555','666']
  }


子组件html

<div class="scroll_outermost_up" [style]="{ width: dataObj.width + dataObj.unit, height: dataObj.height + dataObj.unit }"
  #outerMost (mouseover)="rollStop('over')" (mouseout)="rollStart(60)">
  <div class="outer_box" #outerBox1>
    <ng-content select=".slot-one"></ng-content>
  </div>
  <div class="outer_box" #outerBox2>
    <ng-content select=".slot-two"></ng-content>
  </div>
</div>


子组件ts


import { Component, ElementRef, OnInit, Input, ViewChild, ViewEncapsulation } from '@angular/core';

@Component({
  selector: 'app-scroll-up',
  templateUrl: './scroll-up.component.html',
  styleUrls: ['./scroll-up.component.less'],
  encapsulation: ViewEncapsulation.Emulated,
})
export class ScrollUpComponent implements OnInit {

  constructor() { }

  ngOnInit(): void {
  }

  @Input()
  public dataObj: any = {
    time: 50,
    minLength: 1,
    width:200,
    height: 200,
    unit: 'px',
    scrollList: ['1111','2222','3333','444','555','666']
  }
  
  @ViewChild('outerMost', { static: false }) outerMost: ElementRef;
  @ViewChild('outerBox1', { static: false }) outerBox1: ElementRef;
  @ViewChild('outerBox2', { static: false }) outerBox2: ElementRef;

  ngAfterViewInit() {
    setTimeout(()=>{this.roll(this.dataObj.time)},1000)
  }
  timer = null
  ngOnDestroy() {
    if (this.timer) clearInterval(this.timer);
  }
  roll(t) {
    var outerMost = this.outerMost.nativeElement
    if (this.dataObj.scrollList.length < this.dataObj.minLength) { return console.log('不需要滚动') }
    outerMost.scrollTop = 0;
    this.rollStart(t);
  }
  rollStart(t) {
    var outerMost = this.outerMost.nativeElement
    var outerBox1 = this.outerBox1.nativeElement
    this.rollStop('开始');
    this.timer = setInterval(() => {
      // 当滚动高度大于列表内容高度时恢复为0  
      outerMost.scrollTop++;
      if (outerBox1.scrollHeight - outerMost.scrollTop === 1) {
        outerMost.scrollTop = 0;
        outerMost.scrollTop++;
      }
      if (outerMost.scrollTop >= outerBox1.scrollHeight) {
        outerMost.scrollTop = 0;
        outerMost.scrollTop++;
      }
    }, t);
  }
  rollStop(e) {
    console.log(e)
    clearInterval(this.timer);
  }

}

子组件less

.scroll_outermost_up {
  overflow: hidden; /* 必须 */
  cursor: pointer;
  .outer_box {
    /deep/.inner_item {
      background-color: rgb(235, 210, 243);
    }
    /deep/.inner_item:nth-child(odd) {
      background-color: rgb(179, 223, 248);
    }
  }
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值