English 中文(简体)
生产材料
原标题:Making Material tabs scrollable

I m using Material tabs in my application (mat-tab s inside mat-tab-group) When there are more tabs than can be displayed, two navigation buttons are shown to display the other tabs:

“enterography

我的要求是使用户能够浏览表格,以便显示其他表格。

我试图做一些改动,但不能解决。 它高度赞赏能够提出任何解决办法或建议。

问题回答

[案文解决办法] 我的目标是,在没有控制的情况下,使配制成像,但在点击部分可见的垫子时,能够自动输入,将其移至可见的加拿大观察所。

1) This behavior already partially realized in this example "from the box" - https://stackblitz.com/edit/angular-mat-tabs-scrollalble-initial-behavior. The problem is that you can t scroll backward normally - there some buggy behavior to push tabs to the left. So it does not work as I wanted.

2) 下一个变量是滚动活动——在表层组上采用<条码>(wheel)=“event”,但该表还未使用移动电话:。 https://stackoverflow.com/a/51545130/9026103"。

3) 在你点击标语时,我自己在移动和自动滚动上把点击的刺刀加到屏幕浏览中心,但这样做并不简单。

首先,在打上表象和图象时,你需要区分“盒子”的滚动:

mat-tabs-overe.scs:

$black: #121212;
@mixin media-query-for-mobile {
  @media (max-width: 768px) and (min-width: 1px) {
    @content;
  }
}
mat-tab-group {
  .mat-tab-header {
    .mat-tab-header-pagination {
      display: none !important; // <== disable pagination
    }
    .mat-tab-label-container {
      left: 0px; // if you need to use it on mobile - set left position to 0
      width: 100%;
      .mat-tab-list {
        overflow-x: auto !important; // <== set horisontal scroll bar imperatively
        // below rule prevents sliding of buttons  container - because it not sliding properly - to left it not slide as well
        transform: none !important;
        .mat-tab-labels {
          // some tweaks for tabs - up to you
          @include media-query-for-mobile {
            justify-content: unset !important; 
          }
          .mat-tab-label {
            // min-width: 20% !important;
            padding: 1.25% !important;
            margin: 0px !important;
            text-transform: uppercase;
            color: $black;
            font-weight: 600;
            min-width: 140px !important;
          }
        }
      }
    }
  }
}

in this case you will see that all tabs are similar by width and scrollable on mobile.

其次,在你点击时,你需要自动制成表格,并根据目前的观察结果,将其位置改为屏幕中心——请这样做!

我们可以制定指令,听取以下主要集装箱:<代码><mat-tabs-group>,可滚动集装箱的检查宽度。 a. 所需集装箱:

模板中的指令:

<mat-tab-group scrollToCenter>
    <mat-tab label="tab 1"></mat-tab>
    <mat-tab label="tab 2"></mat-tab>
    <mat-tab label="tab 3"></mat-tab>
    <mat-tab label="tab 4"></mat-tab>
    <mat-tab label="tab 5"></mat-tab>
    <mat-tab label="tab 6"></mat-tab>
    <mat-tab label="tab 7"></mat-tab>
    <mat-tab label="tab 8"></mat-tab>
</mat-tab-group>

指令:

import { Directive, ElementRef, OnDestroy } from  @angular/core ;
import { fromEvent, Subscription } from  rxjs ;

interface DOMRectI {
  bottom: number;
  height: number;
  left: number; // position start of element
  right: number; // position end of element
  top: number;
  width: number; // width of element
  x?: number;
  y?: number;
}

@Directive({
  // tslint:disable-next-line:directive-selector
  selector:  [scrollToCenter] ,
})
export class MatTabScrollToCenterDirective implements OnDestroy {
  isMobile: boolean;
  subs = new Subscription();
  constructor(
    private element: ElementRef
  ) {
    this.subs.add(
      fromEvent(this.element.nativeElement,  click ).subscribe((clickedContainer: MouseEvent) => {
        const scrollContainer = this.element.nativeElement.querySelector( .mat-tab-list );
        const currentScrolledContainerPosition: number = scrollContainer.scrollLeft;

        const newPositionScrollTo = this.calcScrollToCenterValue(clickedContainer, currentScrolledContainerPosition);
      })
    );
  }

/** calculate scroll position to center of viewport */
  calcScrollToCenterValue(clickedContainer, currentScrolledContainerPosition): number {
    const scrolledButton: DOMRectI = (clickedContainer.target as HTMLElement).getBoundingClientRect();
    const leftXOffset = (window.innerWidth - scrolledButton.width) / 2;
    const currentVisibleViewportLeft = scrolledButton.left;
    const neededLeftOffset = currentVisibleViewportLeft - leftXOffset;
    console.log(scrolledButton);
    const newValueToSCroll = currentScrolledContainerPosition + neededLeftOffset;
    return newValueToSCroll;
  }

  ngOnDestroy() {
    this.subs.unsubscribe();
  }
}


And it works! :0 But not in ios and IE... Why? Because ios and IE don t support Element.scroll()

Solution - npm i element-scroll-polyfill and set to polyfills.ts

/** enable polufill for element.scroll() on IE and ios */
import  element-scroll-polyfill ;

Great! IE 和 ios不支持顺利发展。

Solution - npm i smoothscroll-polyfill and add to polyfills.ts

import smoothscroll from  smoothscroll-polyfill ;
// enable polyfill
smoothscroll.polyfill();

Finally it works everywhere. Hope it helps somebody to fix mat-tabs autoscrolling emptiness :)

< 收到:

Try this pure css solution StackBlitz

Put this code in the same component where mat-tab is used

::ng-deep .mat-tab-header {
  overflow-x: scroll !important;
}

::ng-deep .mat-tab-label-container { 
  overflow: visible !important;
}

::ng-deep .mat-tab-header::-webkit-scrollbar { // TO Remove horizontal scrollbar in tabs
  display: none;
}

在 mo轮活动(前引)上添加rag和拖拉机起步活动,以便在一个表形小组下铺设一个 st垫垫。

onDrag(event) {
  if (event.clientX > 0) {
    let deltaX = this.previousX - event.clientX;
    const children = this.tabGroup._tabHeader._elementRef.nativeElement.children;

    // get the tabGroup pagination buttons
    const back = children[0];
    const forward = children[2];
    console.log( dragging  + deltaX);
    // depending on scroll direction click forward or back
    if (deltaX > 3) {
      forward.click();
    } else if (deltaX < -3) {
      back.click();
    }
  }
  this.previousX = event.clientX;
  event.target.style.opacity = 1;
}
onDragStart(event) {
  this.previousX = event.clientX;
  event.target.style.opacity = 0;
}
.noselect {
  user-select: none;
  /* Non-prefixed version, currently
                                supported by Chrome, Edge, Opera and Firefox */
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<mat-card fxflex class="card component-right" *ngIf="editMode">
  <mat-card-content class="card-content">
    <mat-tab-group class="noselect" draggable="true" (drag)="onDrag($event)" (dragstart)="onDragStart($event)" (wheel)="scrollTabs($event)" #tabGroup>
      <mat-tab label="1st tab"> </mat-tab>
      <mat-tab label="2nd tab"> </mat-tab>
      <mat-tab label="3rd tab"> </mat-tab>
      <mat-tab label="4th tab"> </mat-tab>
      <mat-tab label="5th tab"> </mat-tab>
      <mat-tab label="6th tab"> </mat-tab>
    </mat-tab-group>
  </mat-card-content>
</mat-card>

Mat tabs are scroll-able by default. Just check if you have not overridden that property any where like this CSS below ( This CSS blocks the scroll behavior )

.mat-tab-header-pagination-controls-enabled .mat-tab-header-pagination {
display: none !important;
}
.mat-ripple-element {
display: none !important;
}

注:

The tabs will be scroll-able when screen width is less than tabs-group width

就我而言,我只想躲藏 but子,只用可感触的装置 s:

@media (hover: none) {
    ::ng-deep nav[mat-tab-nav-bar] > div {
        overflow-x: scroll !important;
        -ms-overflow-style: none; /* IE and Edge */
        scrollbar-width: none; /* Firefox */
    }
  
    ::ng-deep nav[mat-tab-nav-bar] > div::-webkit-scrollbar {
        display: none; /* Hide scrollbar for Chrome, Safari and Opera */
    }
  
    ::ng-deep nav[mat-tab-nav-bar] .mat-mdc-tab-list {
        transform: translateX(0) !important;
    }
  
    ::ng-deep nav[mat-tab-nav-bar] > button {
        display: none !important;
    }
}

我预计,在我的卷宗中添加以下法典。

    ::ng-deep .mat-mdc-tab-label-container {
    overflow-x: auto !important;
}




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签