Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Material select acts weirdly on fixed blocks

I have created a side block which is static by default, but becomes fixed when you scroll to some point. And in this block I use Angular-Material select.

CSS:

.pos-fixed {
    position:fixed;
    top: 60px;
    width:16.5%!important;
}

#sidebar-right {
    float:right;
    width:23%;
}
#sidebar-right #widget {
    width:100%;
}

HTML:

  <div id="sidebar-right">
    <div id="widget" ng-class="{'pos-fixed': imageHidden}" class="panel md-padding">
      <div>
        <md-input-container style="width:100%">
          <md-select ng-model="number1" placeholder="number 1">
            <md-option ng-repeat="number in ['one','two','three','four','five','six','seven']" value="{{number}}">{{number}}</md-option>
          </md-select>
        </md-input-container>
        <br />
        <md-input-container style="margin-top: 0px;width:100%">
          <md-select ng-disabled="!number1" ng-model="number2" placeholder="numbe 2">
            <md-option ng-repeat="number in ['one','two','three','four','five','six','seven']" value="{{number}}">{{number}}</md-option>
          </md-select>
        </md-input-container>
      </div>
    </div

JS (scroll spy):

app.directive('scroll', function($window) {
  return function(scope, element, attrs) {
    angular.element($window).bind('scroll', function() {
      if (this.pageYOffset >= 320) {
        scope.imageHidden = true;
      } else {
        scope.imageHidden = false;
      }
      scope.$apply();
    });
  };
});

Before side-block is fixed, Material select works fine, but as soon as you scroll and it becomes fixed, select starts to act weirdly.
GIF: http://recordit.co/i72EaaVxJf
Plunker: http://plnkr.co/edit/lfik78wR2FqPoSFSCNlz?p=preview

How can I fix it?

like image 515
Michael Avatar asked Nov 09 '22 20:11

Michael


1 Answers

Add this to your controller instead of scroll directive:

var body = document.querySelector('body');
angular.element($window).bind('scroll', function() {
  if (body.style.position !== 'fixed') {
    $scope.isFixed = window.scrollY > 330;
    $scope.$applyAsync();
  }
});
like image 114
Michael Avatar answered Nov 15 '22 06:11

Michael