javascript-$window不会在AngularJS中滚动触发
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript-$window不会在AngularJS中滚动触发,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3308字,纯文字阅读大概需要5分钟。
内容图文
![javascript-$window不会在AngularJS中滚动触发](/upload/InfoBanner/zyjiaocheng/685/903c38f8381b42769941a0aa0ebf1620.jpg)
当我们向下滚动时,$window函数不会触发.有人可以帮我吗?实际上,问题是,当用户向下滚动时,应该会出现一个按钮.当用户点击该按钮时,滚动条需要像回到顶部功能一样向上移动.
JavaScript代码:
app.controller('appCrtl', ['$scope', '$window', function('$scope', '$window') {
$(window).scroll(function(event) {
var scroll = $(window).scrollTop();
console.log(scroll);
if (scroll > 500 || scroll == undefined) {
$scope.showUpArrow = false;
} else {
$scope.showUpArrow = true;
}
});
}]);
解决方法:
我发现您的代码存在一些问题,尤其是在绑定滚动事件时,特别是在angular中,您使用angular.element将事件绑定到DOM类似于jQuery(它是jQLite,这是jQuery的一种轻量级实现).
angular.element($window).bind('scroll', function () {});
另外,根据您在角度上下文之外使用$scope的情况,必须用$scope.$apply(function(){});包围作用域操作,这将使对代码范围之外的那部分代码的消化成为可能有角的.
angular.element($window).bind('scroll', function () {
$scope.$apply(function () {
// do scope stuff here
});
});
最后,您可以使用pageYOffset属性将您的可见性切换回顶部按钮.
我使用您的代码制作了一个示例,以演示我之前描述的用法.
angular.module('app', [])
.controller('appCrtl', [
'$scope',
'$window',
AppCtrl
]);
function AppCtrl($scope, $window) {
$scope.goToTop = function() {
$window.scrollTo(0, 0);
};
angular.element($window).bind('scroll', function(event) {
$scope.$apply(function() {
var scroll = $scope.scroll = this.pageYOffset;
if (scroll > 100 || scroll == undefined) {
$scope.showUpArrow = true;
} else {
$scope.showUpArrow = false;
}
});
});
};
.floating {
position: fixed;
margin: 10px;
bottom: 0;
right: 0;
}
.display {
position: fixed;
margin: 0;
padding: 2px 5px;
top: 0;
width: 100%;
background-color: gray;
color: white;
}
.spacer {
padding: 10px 200px;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.6/angular.js"></script>
<div ng-app="app" ng-controller="appCrtl">
<div class="display">scrollY: {{ scroll }}</div>
<button class="floating" ng-show="showUpArrow" ng-click="goToTop()">^</button>
<div class="spacer">
<h1>HTML Ipsum Presents</h1>
<p><strong>Pellentesque habitant morbi tristique</strong> senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. <em>Aenean ultricies mi vitae est.</em> Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, <code>commodo vitae</code>, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci,
sagittis tempus lacus enim ac dui. <a href="#">Donec non enim</a> in turpis pulvinar facilisis. Ut felis.</p>
<h2>Header Level 2</h2>
<ol>
<li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</li>
<li>Aliquam tincidunt mauris eu risus.</li>
</ol>
<blockquote>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus magna. Cras in mi at felis aliquet congue. Ut a est eget ligula molestie gravida. Curabitur massa. Donec eleifend, libero at sagittis mollis, tellus est malesuada tellus, at luctus
turpis elit sit amet quam. Vivamus pretium ornare est.</p>
</blockquote>
<h3>Header Level 3</h3>
<ul>
<li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</li>
<li>Aliquam tincidunt mauris eu risus.</li>
</ul>
<pre><code>
#header h1 a {
display: block;
width: 300px;
height: 80px;
}
</code></pre>
</div>
</div>
内容总结
以上是互联网集市为您收集整理的javascript-$window不会在AngularJS中滚动触发全部内容,希望文章能够帮你解决javascript-$window不会在AngularJS中滚动触发所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。