Angular 自定义指令 JQuery animate 不移动元素
Angular custom directive JQuery animate doesn't move the element
我有一个自定义 angular 指令,因此当您单击该元素时,它会向下移动:
angular.module('plunker', [])
.controller('AnimateCtlr', function(){
})
.directive('clickToAnimate', function(){
var linker = function(scope, element, attrs) {
element.bind('click', function(){
console.log('moving');
console.log( $( this).text() );
$(this).animate({top: '+=150'});
});
};
return {
restrict: 'AE',
replace: false,
scope: {
direction : '@'
},
link: linker
};
});
所有检测点击事件的console.log作品,但问题是,div根本不动。这里有什么问题吗?
Plunker 在这里:http://plnkr.co/edit/j1OMiZFrar71P742dY6W?p=preview
当更改 top
属性 时,您需要固定或绝对定位才能生效。你的 plunker 两者都没有。添加 position:absolute;
和初始 top:100px;
就可以了。
如果你在元素上将位置设置为绝对位置,那么它应该可以正常工作
JS:
angular.module('plunker', [])
.controller('AnimateCtlr', function(){
})
.directive('clickToAnimate', function(){
var linker = function(scope, element, attrs) {
element.bind('click', function(){
console.log('moving');
console.log( $( this).text() );
$(this).css({position: 'absolute'}) // can be done right here! But belongs in your CSS really.
$(this).animate({top: '+=150'}, 300, function(){
console.log('complete')
});
});
};
return {
restrict: 'AE',
replace: false,
scope: {
direction : '@'
},
link: linker
};
});
我有一个自定义 angular 指令,因此当您单击该元素时,它会向下移动: angular.module('plunker', [])
.controller('AnimateCtlr', function(){
})
.directive('clickToAnimate', function(){
var linker = function(scope, element, attrs) {
element.bind('click', function(){
console.log('moving');
console.log( $( this).text() );
$(this).animate({top: '+=150'});
});
};
return {
restrict: 'AE',
replace: false,
scope: {
direction : '@'
},
link: linker
};
});
所有检测点击事件的console.log作品,但问题是,div根本不动。这里有什么问题吗?
Plunker 在这里:http://plnkr.co/edit/j1OMiZFrar71P742dY6W?p=preview
当更改 top
属性 时,您需要固定或绝对定位才能生效。你的 plunker 两者都没有。添加 position:absolute;
和初始 top:100px;
就可以了。
如果你在元素上将位置设置为绝对位置,那么它应该可以正常工作
JS:
angular.module('plunker', [])
.controller('AnimateCtlr', function(){
})
.directive('clickToAnimate', function(){
var linker = function(scope, element, attrs) {
element.bind('click', function(){
console.log('moving');
console.log( $( this).text() );
$(this).css({position: 'absolute'}) // can be done right here! But belongs in your CSS really.
$(this).animate({top: '+=150'}, 300, function(){
console.log('complete')
});
});
};
return {
restrict: 'AE',
replace: false,
scope: {
direction : '@'
},
link: linker
};
});