我的$scope
中有一个var,我需要减少它,向上滚动时增加,向下滚动时增加。我怎样才能做到这一点?我应该使用js滚动吗?有没有办法不重新发明轮子?
答案 0 :(得分:0)
使用javascript <ContentPresenter Content="{TemplateBinding SelectionBoxItem}" >
<ContentPresenter.ContentTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding}" />
<Image x:Name="imgArrow" RenderOptions.BitmapScalingMode="HighQuality">
<Image.Style>
<Style TargetType="Image">
<Setter Property="Source" Value="{DynamicResource ComboBoxArrowNormalImage}" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsDropDownOpen, RelativeSource={RelativeSource AncestorType=ComboBox}}" Value="True">
<Setter Property="Source" Value="{DynamicResource ComboBoxArrowHoverImage}" />
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
</Grid>
</DataTemplate>
</ContentPresenter.ContentTemplate>
</ContentPresenter>
功能。
window.scrollTo
&#13;
答案 1 :(得分:0)
您的问题不明确,但如果我理解正确,您希望根据滚动方向增加/减少计数器。
你可以采用的方式是点击'scroll'(jQuery)事件,如:
angular.element($window).on('scroll', function(event) {
// Do something here.
});
最好你可以编写一个自定义指令,然后发出自定义事件,然后你就可以在你的控制器中监听这个事件并对它采取行动。
注意:您也可以去除滚动事件。
P.S。 :请始终提供代码段或jsfiddle / codepen,以便其他人可以更好地理解您的问题。
angular.module('app', [])
.controller('MainController', function($rootScope, $scope) {
var changeBy = 5;
$scope.scrollCounter = 0;
// Tap into custom 'scrollHappened' event.
$rootScope.$on('scrollHappened', function(event, scrollUpHappened) {
// If the scroll direction is 'down'.
if (scrollUpHappened) {
$scope.scrollCounter += changeBy;
} else { // Scroll direction is 'up'.
$scope.scrollCounter -= changeBy;
}
});
})
.directive('scrollCounter', function($rootScope, $window) {
return {
restrict: 'A',
link: function() {
var lastScrollPos = 0;
// Add a scroll event on $window object.
angular.element($window).on('scroll', function(event) {
console.log(angular.element(event.target))
var scrollPos = angular.element(event.target).scrollTop();
// Emit custom 'scrollHappened' event.
$rootScope.$emit('scrollHappened', (scrollPos > lastScrollPos));
// As all this is happening outside of the angular world
// wrap the update logic inside $apply, so that update will
// be visible to the angular.
$rootScope.$apply(function() {
lastScrollPos = scrollPos;
});
});
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.min.js"></script>
<div data-ng-app="app" data-ng-controller="MainController">
<div scroll-counter="" style="height: 400px">
<span>Current scroll counter = {{ scrollCounter }}</span>
<br/><br/>
<span>Current scroll counter = {{ scrollCounter }}</span>
<br/><br/>
<span>Current scroll counter = {{ scrollCounter }}</span>
<br/><br/>
<span>Current scroll counter = {{ scrollCounter }}</span>
<br/><br/>
<span>Current scroll counter = {{ scrollCounter }}</span>
<br/><br/>
<span>Current scroll counter = {{ scrollCounter }}</span>
<br/><br/>
</div>
</div>