【发布时间】:2015-09-22 10:33:31
【问题描述】:
在AngularJS中滚动DIV元素时如何防止页面滚动?
阅读此JQuery answer 后,我想在 AngularJS 指令 中应用相同的内容,但我不知道如何...有什么建议吗?
JQuery 中的示例: http://jsfiddle.net/XNwbt/458/
$( '.scrollable' ).bind( 'mousewheel DOMMouseScroll', function ( e ) {
var e0 = e.originalEvent,
delta = e0.wheelDelta || -e0.detail;
this.scrollTop += ( delta < 0 ? 1 : -1 ) * 30;
e.preventDefault();
});
谢谢!
编辑:
我尝试在 AngularJS 中做同样的事情,但 DIV 元素永远不会滚动...
'use strict';
angular
.module('core')
.directive('preventScrollBody', preventScrollBody);
function preventScrollBody() {
return{
link: link,
restrict: 'A'
};
function link(scope, element, attrs) {
element.bind( 'mousewheel DOMMouseScroll', function ( e ) {
var e0 = e.originalEvent,
delta = e0.wheelDelta || -e0.detail;
this.scrollTop += ( delta < 0 ? 1 : -1 ) * 30;
e.preventDefault();
});
}
}
【问题讨论】:
-
所以最好制作指令customscroll并将此代码放入链接功能。 (将 $( '.scrollable' ) 替换为 args 中的元素)应该可以工作,恕我直言
-
刚刚编辑了问题。我尝试在 Angular 中做同样的事情,但没有用。 (添加问题中的代码)
标签: angularjs scroll scrollbar