我创建了一个默认为static
的边栏,但在滚动到某个点时会变为fixed
。在这个块中我使用Angular-Material选择。
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(滚动间谍):
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();
});
};
});
在侧边栏fixed
之前,素材选择工作正常,但只要您滚动并变为fixed
,选择就会开始奇怪地行动。
GIF:http://recordit.co/i72EaaVxJf
Plunker:http://plnkr.co/edit/lfik78wR2FqPoSFSCNlz?p=preview
我该如何解决?
答案 0 :(得分:1)
将此添加到您的控制器而不是scroll
指令:
var body = document.querySelector('body');
angular.element($window).bind('scroll', function() {
if (body.style.position !== 'fixed') {
$scope.isFixed = window.scrollY > 330;
$scope.$applyAsync();
}
});