javascript - 你如何在angularjs中旋转图像

标签 javascript jquery angularjs rotation angularjs-directive

您好,我有一张图片要旋转。左右有两个按钮,可将图像沿相反方向旋转 45 度。我尝试使用 jquery rotate 库创建一个指令,但它不起作用。帮忙?

指令.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);    

        });
    }
}

});

模板.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

   $scope.rotate= function(angle) {

   $scope.angle=angle;
    };

最佳答案

您可以通过在指令中设置 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
                });
            });
        }
    }
});

希望它能有所启发。

Demo

关于javascript - 你如何在angularjs中旋转图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18840253/

相关文章:

jquery - 禁用 jquery 函数 x 秒

javascript - Jquery Slider 不适用于移动设备

javascript - 如何使用 jQuery 选择器获取此元素?

javascript - 如何在fancybox上播放音频文件?

javascript写入控制台日志弹出

javascript - 在查询中使用 Replace with 替换多个元素

javascript - 从具有子对象的对象数组中获取属性值

javascript - 显示的空白页面 - Angular JS

angularjs - 为什么 $rootScope.$new 不让模板进入指令?

javascript - $注入(inject)器:modulerr Failed to instantiate module bookApp due to: ReferenceError: 'when' is undefined at Anonymous function