简体   繁体   English

如何使用css / js旋转后如何定位/调整图像大小,以使图像不位于容纳图像或裁剪图像的div之外

[英]how to position/resize image after rotation using css/js so that the image is not positioned outside the div holding it or cropped out

Here's a jsfiddle i'm working on. 这是我正在研究的jsfiddle

The jsfiddle rotates an image upon button click by 90 degrees in clockwise or anti-clockwise direction. jsfiddle在单击按钮时将图像按顺时针或逆时针方向旋转90度。 The rotated image is displayed outside the parent div when rotated. 旋转后,旋转后的图像显示在父div的外部。 The image may be of any height and width. 图像可以是任何高度和宽度。

How can the rotation be modified to display the image within its parent div maintaining its aspect ratio? 如何修改旋转角度以在其父div内显示图像并保持其长宽比?

 var app = angular.module('rotationApp', []); app.controller('rotationCtrl', ['$scope', function($scope) { $scope.angle = 0; $scope.rotate = function(angle) { $scope.angle += angle; if (($scope.angle === 360)) { $scope.angle = 0; } if ($scope.angle < 0) { $scope.angle += 360; } } } ]); app.directive('rotate', function() { return { restrict: 'A', link: function(scope, element, attrs) { scope.$watch(attrs.degrees, function(rotateDegrees) { var r = 'rotate(' + rotateDegrees + 'deg)'; element.css({ '-moz-transform': r, '-webkit-transform': r, '-o-transform': r, '-ms-transform': r, transform: r }); }); } } }); 
 img { border: green solid 1px; } .container { margin-top: 100px; } .img-holder { border: red solid 1px; height: 500px; width: 500px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div ng-app='rotationApp' ng-controller="rotationCtrl"> <div class="container"> <div class="row"> <div class="col-md-6 img-holder"> <img rotate degrees='angle' class="img-responsive" src="http://vignette1.wikia.nocookie.net/uncyclopedia/images/3/3a/Pac_man_pie_chart.jpg/revision/latest?cb=20100508212204"> </div> <div class="col-md-6"> <button type="button" class="btn btn-lg btn-primary" ng-click="rotate(-90)"> rotate left </button> <button type="button" class="btn btn-lg btn-primary" ng-click="rotate(90)"> rotate right </button> </div> </div> </div> </div> 

You can add a CSS margin-top property to do this like this : 您可以添加CSS margin-top属性来执行以下操作:

        var r = 'rotate(' + rotateDegrees + 'deg)';
        element.css({
            '-moz-transform': r,
            '-webkit-transform': r,
            '-o-transform': r,
            '-ms-transform': r,
            transform: r,
            'margin-top' : '100px'
        });

And if you want to keep the image stick to the top when it is not rotate, you can add this condition : 如果要在不旋转图像时将图像保持在顶部,则可以添加以下条件:

        if(r == "rotate(0deg)" || r == "rotate(180deg)"){
            element.css('margin-top',0);
        }

See the result here 在这里查看结果

Note : If you remove that last condition, it is perfectly centered to the border. 注意 :如果删除最后一个条件,则它会完美地位于边框的中心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM