use*_*508 5 javascript jquery rotation angularjs angularjs-directive
嗨,我有一个我想要旋转的图像.左右两个按钮可将图像向相反方向旋转45度.我尝试使用jquery旋转库创建一个指令,但它不起作用.救命?
directive.js
.directive('rotate', function() {
return {
restrict:'A',
link: function(scope, element, attrs) {
console.log('hi');
// watch the degrees attribute, and update the UI when it changes
scope.$watch(attrs.degrees, function(rotateDegrees) {
console.log(rotateDegrees);
//transform the css to rotate based on the new rotateDegrees
element.rotate(rotateDegrees);
});
}
}
Run Code Online (Sandbox Code Playgroud)
});
template.html
<p><img degrees='angle' rotate id='image' src='myimage.jpg'/> </p>
<a ng-click="rotate('-45')">Rotate Left</a>
<a ng-click="rotate('45')">Rotate Right</a>
Run Code Online (Sandbox Code Playgroud)
controller.js
$scope.rotate= function(angle) {
$scope.angle=angle;
};
Run Code Online (Sandbox Code Playgroud)
zs2*_*020 15
您可以通过在指令中设置CSS来执行旋转
app.directive('rotate', function () {
return {
restrict: 'A',
link: function (scope, element, attrs) {
scope.$watch(attrs.degrees, function (rotateDegrees) {
console.log(rotateDegrees);
var r = 'rotate(' + rotateDegrees + 'deg)';
element.css({
'-moz-transform': r,
'-webkit-transform': r,
'-o-transform': r,
'-ms-transform': r
});
});
}
}
});
Run Code Online (Sandbox Code Playgroud)
希望它可以有所启发.