javascript 你如何在angularjs中旋转图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18840253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you rotate image in angularjs
提问by user1424508
Hi I have an image that I want to rotate. There are two buttons left and right which rotate the image 45 degrees in opposite directions. I tried creating a directive using jquery rotate library but it doesn't work. Help?
嗨,我有一个要旋转的图像。左右两个按钮可将图像向相反方向旋转 45 度。我尝试使用 jquery 旋转库创建指令,但它不起作用。帮助?
directive.js
指令.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);
});
}
}
});
});
template.html
模板.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>
controller.js
控制器.js
$scope.rotate= function(angle) {
$scope.angle=angle;
};
回答by zs2020
You can do the rotation by setting the CSS in the directive
您可以通过在指令中设置 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
});
});
}
}
});
Hope it can shed some light on.
希望它能有所启发。
回答by katmanco
I just wanted to tell you my solution about rotating.
我只是想告诉你我关于旋转的解决方案。
<img src="{{Image_URL}}" id="img{{$index}}" class="CssImage" />
<a href=""><span class="glyphicon glyphicon-repeat" id="rotateIcon" data-ng-init="rd=1;" data-ng-click="RotateImage('img'+ $index,rd);rd=rd+1;rd==4?rd=0:''"></span></a>
Here rd states for rotate degree time(x1,x2,x3,x4 -x4 =360 then resetting it to 0 ) Say that you have an ng-repeat for images in there created img ids.and in this span's ng-click I rotate the image by applying css .
这里 rd 状态为旋转度数时间(x1,x2,x3,x4 -x4 =360 然后将其重置为 0 )说你有一个 ng-repeat 图像在那里创建了 img ids.and 在这个跨度的 ng-click 我旋转通过应用 css 图像。
$scope.RotateImage = function (id,deg) {
var deg = 90 * deg;
$('#' + id).css({
'-webkit-transform': 'rotate(' + deg + 'deg)', //Safari 3.1+, Chrome
'-moz-transform': 'rotate(' + deg + 'deg)', //Firefox 3.5-15
'-ms-transform': 'rotate(' + deg + 'deg)', //IE9+
'-o-transform': 'rotate(' + deg + 'deg)', //Opera 10.5-12.00
'transform': 'rotate(' + deg + 'deg)' //Firefox 16+, Opera 12.50+
});
}
Just take as an Alternative , any comments will be appreciated .
只是作为一种选择,任何意见将不胜感激。
回答by Tomevision
$scope.RotateImageRight = function () {
z = z + 1;
var img;
if (z === 1) {
img = document.getElementById("rotate90");
img.style.transform = "rotate(-90deg)";
img.style.width = "725px";
}
else if (z === 2) {
img = document.getElementById("rotate90");
img.style.transform = "rotate(-180deg)";
img.style.width = "870px";
}
else if (z === 3) {
img = document.getElementById("rotate90");
img.style.transform = "rotate(-270deg)";
img.style.width = "725px";
}
else if (z === 4) {
img = document.getElementById("rotate90");
img.style.transform = "rotate(-360deg)";
img.style.width = "870px";
} else {
z = 0;
}
}