angular 粘合性指令
粘贴性属性
.sticky {
position: sticky;
left: 0;
}
至少应指定top
、right
、bottom
最近滚动祖先
如果要两个属性都靠左
.company {
position: sticky;
left: 0px;
}
.manager {
position: sticky;
left: 120px;
}
需要计算位置
计算示例
- 表格位置 = (100, 200) // <-- x = 100
Company
位置 = (100, 200) // <-- x 相对于表 = 100 - 100 = 0Manager
的位置 = (300, 200) // <-- x 相对于表 = 300 - 100 = 200
要减去table
距离左侧的距离
Optional
可以理解我是儿子,通过这个属性拿到父母给我的管道或者指令
举个小栗子
app.component
<div one>
<div two></div>
</div>
在two
指令中, 我们可以通过Optional
拿到one
的指令和app.component
的组件
twoDirective
constructor( @Optional() private one: oneDirective,@Optional() private app:appComponent) {
}
上完整的案例
<table appStickyTable style="width: 900px;overflow-x: auto;margin-left:200px;">
<tr>
<th appSticky>Company</th>
<th appSticky>Manager</th>
<th>Employees</th>
<th>Contractors</th>
<th>Jobs</th>
<th>Contracts</th>
</tr>
<ng-container *ngFor="let item of data">
<tr>
<td appSticky style="min-width:200px">{{ item.a }}</td>
<td appSticky>{{ item?.a }}</td>
<td> {{ item?.b }} </td>
<td> {{ item?.c }} </td>
<td> {{ item?.d }} </td>
<td> {{ item?.f }} </td>
</tr>
</ng-container>
</table>
data:any = [
{a: 111, b: 222, c: 333, d: 444, f: 555},
{a: 111, b: 222, c: 333, d: 444, f: 555},
{a: 111, b: 222, c: 333, d: 444, f: 555},
{a: 111, b: 222, c: 333, d: 444, f: 555},
]
td,th{
width: 200px;
text-align: center;
background-color: #fff;
}
table的指令,主要是拿到距离左侧的距离
@Directive({
selector: '[appStickyTable]'
})
export class StickyTableDirective {
constructor(private el: ElementRef) {
}
get x() {
return (this.el.nativeElement as HTMLElement)?.getBoundingClientRect().x;
}
}
appSticky
指令
@Directive({
selector: '[appSticky]'
})
export class StickyDirective implements AfterViewInit {
constructor(private el: ElementRef,
@Optional() private table: StickyTableDirective,@Optional() private One:Text1Component) {
}
ngAfterViewInit() {
console.log(this.One);
const el = this.el.nativeElement as HTMLElement;
const { x } = el.getBoundingClientRect();
el.style.position = 'sticky';
el.style.left = this.table ? `${x - this.table.x}px` : '0px';
}
}
决定自己的高度的是你的态度,而不是你的才能
记得我们是终身初学者和学习者
总有一天我也能成为大佬

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
2020-01-16 本周学习总结