Html同页面内滚动跳转方法与监听滚动事件

1、同html页面滚动条跳转 TAB页设计及实现

document.getElementById("***anchor").scrollIntoView(true); 

注:***anchor为html页面的锚点,scrollIntoView为true表示从上往下滚动值锚点位置,为false表示从下往上。

Angular实现:

html:

<div class = "plx-col-1" style="position:fixed;right:1px;padding-left:8px">
<ul class="right-menu">

  <li
    *ngFor="let step of steps; let i = index"
    [ngClass]="{ 'active-step': currentStep == i }"
    (click)="changeStep(i)"
  >
    <a>
      {{ step.label }}
    </a>
  </li>

</ul>
</div>

ts:

//右侧导航条
  steps = [
    {label: "***"},    
    {label: "***"},   
    {label: "***"},  
    {label: "***"},   // ***自行填充字符串
  ];
  currentStep = 0;
  changeStep(num) {
     if (num == 2) {
      document.getElementById("***anchor").scrollIntoView(true); //***anchor表示html上的锚点
      this.currentStep = num;

     } else if (num == 0) {
      document.getElementById("***anchor").scrollIntoView(true);
      this.currentStep = num;

     } else if (num == 1) {
      document.getElementById("***anchor").scrollIntoView(true);
      this.currentStep = num;

     } else if (num == 3) {
      document.getElementById("***anchor").scrollIntoView(true);
      this.currentStep = num;

     } else if (num == 4) {
      document.getElementById("***anchor").scrollIntoView(true);
      this.currentStep = num;

     } else if (num == 5) {
      document.getElementById("***anchor").scrollIntoView(true);
      this.currentStep = num;

     }

  }

less

.right-menu {
  padding: 0px 0px 8px 8px;
  width: 94px;
  color: #4d4d4d;
  li {
    list-style: none;
    cursor: pointer;
    padding: 5px 10px;
    width: 130px;
    font-family: MicrosoftYaHei;
    font-size: 16px;
    border-left: 1px solid  #d9d9d9;

    &.active-step {
      color: #1993ff;
      border-left: 1px solid #1993ff;

    }
  }
}

2、监听滚动事件,实现Tab页随滚动条变化而变化

import {Subject} from "rxjs";

subscribeScoll: any;
contentElement: any;
isMonitorScroll = true;  //是否监听滚动事件

ngAfterViewInit() {
    setTimeout(() => {
      this.setScroll();  //监听滚动事件切换导航条
    });
 }


setScroll() {  //监听滚动事件切换导航条
    let elements = document.getElementsByClassName('***');  //获取滚动条所在的元素***
    this.contentElement = elements[elements.length - 1];
    this.subscribeScoll = fromEvent(this.contentElement, 'scroll').subscribe((event) => {
      if ( !this.isMonitorScroll ) {  //自行切换导航条,则本次监听到滚动后不做操作
        this.isMonitorScroll = true; 
        return
      }
      this.onWindowScroll();
    }); 

  }

onWindowScroll(){
     // 获取不滚动情况下浏览器可见区域高度
      var documentClientHeight = document.documentElement.clientHeight || window.innerHeight;

    // 获取元素顶端到可见区域(网页)顶端的距离 
    var ElementClientTop = document.getElementById('***anchor').getBoundingClientRect().top; 
    
    //其余元素获取过程省略,对应下文***ElementClientTop
     
    // 可视区高度大于元素距可视区顶部的高度时的操作

    if ( documentClientHeight >= ElementClientTop && ElementClientTop > 0 ) { 
        this.currentStep = 0;
        return 
      } else if ( documentClientHeight >= ***ElementClientTop && ***ElementClientTop > 0 ) {
        this.currentStep = 1;
        return
      } 
    // currentStep 为2,3,4,5时的操作同上,省略
 }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值