Angular2-6 实现吸顶效果指令

做项目的时候需要用到吸顶效果,可是没有找到单独的插件,所以自己实现一个吸顶的指令.

思路为检测需要吸顶的元素是否滚动到顶部,为其加上fixed属性

auto-fixed.directive.ts

@Directive({
  selector: '[txAutoFixed]'
})
export class AutoFixedDirective {
  // 元素距离顶部的原始距离
  toTop: number = 0;
  // 吸顶元素
  toTopElement: any;
  // 吸顶元素id
  // tslint:disable-next-line:no-input-rename
  @Input('txAutoFixed') selector: string = '';

  @HostListener('scroll', ['$event'])
  onScroll($event: Event) {
    if (this.er.nativeElement.scrollTop >= this.toTop) {
      this.renderer2.setStyle(this.toTopElement, 'position', 'fixed');
    } else {
      this.renderer2.setStyle(this.toTopElement, 'position', 'static');
    }
  }

  constructor(private er: ElementRef, private renderer2: Renderer2) {
    setTimeout(() => {
      this.toTopElement = this.er.nativeElement.querySelector('#' + this.selector);
      this.toTop = this.toTopElement.offsetTop;
    }, 0);
  }

}

指令作用于滚动容器,传入吸顶元素的id,初始化时获取吸顶元素的数据并保存,对滚动进行监听.

用法:

<div class="container"  txAutoFixed="fixedElement">
    <div>
        <div id="fixedElement">
            吸顶元素
        </div>
    </div>
</div>

吸顶元素需要使用一个父容器包裹并设置同样的高度,避免元素脱离,列表布局变动.
不确定高度的吸顶元素可以通过复制来实现,也可以想其他办法.

效果:
这里写图片描述

这只适合用于自己写的页面,在使用ng-weui库使,使用infiniteloader时因为滚动在组件里面,没法监听,只能重写该组件实现.思路差不多.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
< DOCTYPE html> <html ang app ""> <head> < <link rel "stylesheet" href "http: apps bdimg com libs bootstrap 3 2 0 css bootstrap min css"> > < head> <body ng controller "userController"> <div class "container"> <h3>Users< h3> <table class "table table striped"> <thead><tr> <th>Edit< th> <th>First Name< th> <th>Last Name< th> < tr>< thead> <tbody><tr ng repeat "user in users"> <td> <button class "btn" ng click "editUser user id "> < <span class "glyphicon glyphicon pencil">< span>&nbsp;&nbsp;Edit > < button> < td> <td>{{ user fName }}< td> <td>{{ user lName }}< td> < tr>< tbody> < table> <hr> <button class "btn btn success" ng click "editUser "new" "> <span class "glyphicon glyphicon user">< span> Create New User < button> <hr> <h3 ng show "edit">Create New User:< h3> <h3 ng hide "edit">Edit User:< h3> <form class "form horizontal"> <div class "form group size13"> <label style "float:left" class "col sm 2 control label">Email地址:< label> <div class "col sm 10"> <input style "height: 30px;width: 330px;" type "text" ng model "fName" > < div> < div> <div class "form group"> <label style "float:left" class "col sm 2 control label">Last Name:< label> <div class "col sm 10"> <input style "height: 30px;width: 330px;" type "text" ng model "lName" placeholder "Last Name"> < div> < div> <div class "form group"> <label style "float:left" class "col sm 2 control label">Password:< label> <div class "col sm 10"> <input style "height: 30px;width: 330px;" type "password" ng model "passw1" placeholder "Password"> < div> < div> <div class "form group"> <label style "float:left" class "col sm 2 control label">Repeat:< label> <div class "col sm 10"> <input style "height: 30px;width: 330px;" class "size14 "type "password" ng model "passw2" placeholder "Repeat Password"> < div> < div> < form> <hr> <div> <button class "btn btn success" ng disabled "error || incomplete"> 更新 < button> < div> <script src "http: apps bdimg com libs angular js 1 2 15 angular min js">< script> <script src "myUsers js"> function userController $scope { $scope fName ""; $scope lName ""; $scope passw1 ""; $scope passw2 ""; $scope users [ {id:1 fName:"Hege" lName:"Pege" } {id:2 fName:"Kim" lName:"Pim" } {id:3 fName:"Sal" lName:"Smith" } {id:4 fName:"Jack" lName:"Jones" } {id:5 fName:"John" lName:"Doe" } {id:6 fName:"Peter" lName:"Pan" } ]; $scope edit true; $scope error false; $scope incomplete false; $scope editUser function id { if id "new" { $scope edit true; $scope incomplete true; $scope fName ""; $scope lName ""; } else { $scope edit false; $scope fName $scope users[id 1] fName; $scope lName $scope users[id 1] lName; } }; $scope $watch "passw1" function {$scope test ;} ; $scope $watch "passw2" function {$scope test ;} ; $scope $watch "fName" function {$scope test ;} ; $scope $watch "lName" function {$scope test ;} ; $scope test function { if $scope passw1 $scope passw2 { $scope error true; } else { $scope error false; } $scope incomplete false; if $scope edit && $scope fName length || $scope lName length || $scope passw1 length || $scope passw2 length { $scope incomplete true; } }; } < script> < body> < html>>">< DOCTYPE html> <html ang app ""> <head> < <link rel "stylesheet" href "http: apps bdimg com libs bootstrap 3 2 0 css bootstrap min css"> > < head> <body ng controller "userController"> <div class "con [更多]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值