typescript 使用angular2上的按钮水平滚动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48955095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Horizontal Scroll using buttons on angular2
提问by Sergio Mendez
So I have this app in angular2 where I need to scroll a component horizontally but with buttons right and left. So I need a function for each button that scroll to right or left the content. I need something like this:
所以我在 angular2 中有这个应用程序,我需要水平滚动一个组件,但有左右按钮。所以我需要为每个向右或向左滚动内容的按钮提供一个功能。我需要这样的东西:
I tried using document.getElementById('myscrolldiv').animate({ scrollLeft: "-=" + 250 + "px"; }
but Angular does not recognize the animate
line.
我尝试使用document.getElementById('myscrolldiv').animate({ scrollLeft: "-=" + 250 + "px"; }
但 Angular 无法识别该animate
行。
So I am looking for a diferent way of scroll horizontally using buttons but NOT using jquery. Is there any way to do this in angular?
所以我正在寻找一种使用按钮而不是使用 jquery 水平滚动的不同方式。有没有办法在角度上做到这一点?
Here is my html
这是我的 html
<div class="container">
<div class="side">
<mat-icon (click)="scrollLeft()">keyboard_arrow_left</mat-icon>
</div>
<div id="widgetsContent" class="middle">
<div class="scrolls">
<div class="info-widget">
WIDGET
</div>
<div class="info-widget">
WIDGET
</div>
<div class="info-widget">
WIDGET
</div>
<div class="info-widget">
WIDGET
</div>
<div class="info-widget">
WIDGET
</div>
<div class="info-widget">
WIDGET
</div>
</div>
</div>
<div class="side">
<mat-icon (click)="scrollRight()">keyboard_arrow_right</mat-icon>
</div>
</div>
And here is my css
这是我的 css
.container {
display: flex;
height: 22.5vh !important;
}
.side {
width: 50px;
height: 22.5vh !important;
}
.middle {
flex-grow: 1;
height: 22.5vh !important;
overflow-x: scroll;
overflow-y: hidden;
white-space: nowrap;
}
So, how do I scroll right and left pushing the buttons? please help.
那么,我如何左右滚动按钮?请帮忙。
回答by H.Azizkhani
Import ViewChild and ElementRef to get elemenet refrence.
导入 ViewChild 和 ElementRef 以获取元素引用。
use #localvariable as shown here, <div #widgetsContent class="middle">
使用 #localvariable 如下所示, <div #widgetsContent class="middle">
get element in component, @ViewChild('widgetsContent', { read: ElementRef }) public widgetsContent: ElementRef<any>;
获取组件中的元素, @ViewChild('widgetsContent', { read: ElementRef }) public widgetsContent: ElementRef<any>;
change scrollvalue, this.widgetsContent.nativeElement.scrollTo({ left: (this.widgetsContent.nativeElement.scrollLeft + 150), behavior: 'smooth' });
更改滚动值, this.widgetsContent.nativeElement.scrollTo({ left: (this.widgetsContent.nativeElement.scrollLeft + 150), behavior: 'smooth' });
An example is shown below
一个例子如下所示
import { Component, OnInit, ViewChild, ElementRef } from "@angular/core";
@Component({
selector: 'my-app',
template: `
<div class="container">
<div style="float: left">
<button (click)="scrollLeft()">left</button>
</div>
<div #widgetsContent class="middle">
<div class="info-widget">
WIDGET
</div>
<div class="info-widget">
WIDGET
</div>
<div class="info-widget">
WIDGET
</div>
<div class="info-widget">
WIDGET
</div>
<div class="info-widget">
WIDGET
</div>
<div class="info-widget">
WIDGET
</div>
</div>
<div style="float: right">
<button (click)="scrollRight()">right</button>
</div>
</div>
`,
styles: [`
.info-widget {
width: 31.75%;
border: 1px solid black;
display: inline-block;
}
.middle {
float: left;
width: 90%;
overflow: auto;
/*will change this to hidden later to deny scolling to user*/
white-space: nowrap;
}
`]
})
export class AppComponent {
@ViewChild('widgetsContent', { read: ElementRef }) public widgetsContent: ElementRef<any>;
public scrollRight(): void {
this.widgetsContent.nativeElement.scrollTo({ left: (this.widgetsContent.nativeElement.scrollLeft + 150), behavior: 'smooth' });
}
public scrollLeft(): void {
this.widgetsContent.nativeElement.scrollTo({ left: (this.widgetsContent.nativeElement.scrollLeft - 150), behavior: 'smooth' });
}
}
回答by Sahil Ralkar
Consider the following solution
考虑以下解决方案
In .html
在 .html 中
<div #widgetsContent class="custom-slider-main">
<div class="info-widget">
WIDGET
</div>
<div class="info-widget">
WIDGET
</div>
<div class="info-widget">
WIDGET
</div>
<div class="info-widget">
WIDGET
</div>
<div class="info-widget">
WIDGET
</div>
<div class="info-widget">
WIDGET
</div>
</div>
In .scss
在.scss中
.custom-slider-main{
display: flex;
overflow: hidden;
scroll-behavior: smooth;
}
In .ts
在.ts
@ViewChild('widgetsContent') widgetsContent: ElementRef;
And On click of left/right button use the following functions
并单击左/右按钮使用以下功能
scrollLeft(){
this.widgetsContent.nativeElement.scrollLeft -= 150;
}
scrollRight(){
this.widgetsContent.nativeElement.scrollLeft += 150;
}