[英]Angular custom directive JQuery animate doesn't move the element
我有一个自定义的角度指令,因此当您单击该元素时它将向下移动: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根本没有移动。 这里有什么问题?
柱塞在这里: http ://plnkr.co/edit/j1OMiZFrar71P742dY6W?p=preview
如果您将位置设置为绝对位置,那么它应该可以正常工作
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
};
});
更改top
属性时,您需要具有固定位置或绝对位置才能使其生效。 你的傻瓜都没有。 添加position:absolute;
和初始top:100px;
做到了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.