简体   繁体   English

如何在JS中旋转后更改图像css?

[英]How to change image css after rotating it in JS?

There is nice css trick to always position image in the center of div regardless of image size or aspect ratio. 无论图像大小或纵横比如何,总是将图像定位在div的中心有一个很好的CSS技巧。

<style>
.img_wrap {
    padding-bottom: 56.25%;
    width: 100%; 
    position: relative;
    overflow: hidden;
}
#imgpreview {
    display: block;
    max-height: 100%;
    max-width: 100%;
    width: auto;
    height: auto;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    margin: auto;
}
</style>
<div class="img_wrap">
  <img id="imgpreview" src="http://i.imgur.com/RRUe0Mo.png" alt="your image" />
</div>

Then I added jquery code for rotating image 然后我添加了用于旋转图像的jquery代码

$(document).ready(function(){
    var rotation = 0;

    jQuery.fn.rotate = function(degrees) {
        $(this).css({'-webkit-transform' : 'rotate('+ degrees +'deg)',
                     '-moz-transform' : 'rotate('+ degrees +'deg)',
                     '-ms-transform' : 'rotate('+ degrees +'deg)',
                     'transform' : 'rotate('+ degrees +'deg)'});
    };

    $('#imgpreview').click(function() {
        rotation += 90;
        $(this).rotate(rotation);
        // $(this).toggleClass("imgpreview");
    });
});

However, when I am rotation image, it gets cropped. 但是,当我是旋转图像时,它会被裁剪。 I want to avoid that. 我想避免这种情况。

I tried to play with addClass feature but with no success. 我尝试使用addClass功能,但没有成功。 If someone could suggest any solution would appreciate a lot. 如果有人可以建议任何解决方案会很感激。

I have created jsfidlle for this question. 我为这个问题创建了jsfidlle Here the updated fiddle 这里更新了小提琴

You can achieve this with jquery by altering a little bit your code: 您可以通过更改一些代码来使用jquery实现此目的:

$('#imgpreview').click(function() {
    if (rotation === 360) {
        rotation = 0
    } else {
        rotation += 90;
    }
    $(this).rotate(rotation);
    if(rotation === 90 || rotation === 270) {
        $('.img_wrap').css('height', $(this).width());
    } else {
        $('.img_wrap').css('height', 'auto');
    }
});

There's maybe the need to alter your css also but it depends what is the end result that you wish to have. 可能还需要改变你的CSS,但这取决于你希望拥有的最终结果。

There are many ways to do that. 有很多方法可以做到这一点。
The jQuery below is determine whether it is vertical or not. 下面的jQuery确定它是否是垂直的。
You just need to add this line into your function(degrees) 你只需要在你的函数中添加这一行(度)

((degrees/90) == 1 || (degrees/90) == 3)? $(this).css('width','56.25%'):$(this).css('width','auto');

Jsfiddle 的jsfiddle

So this is what I did to your code: 这就是我对你的代码所做的:

  1. Removed the overflow: hidden for img_wrap . 删除overflow: hiddenimg_wrap

  2. In JS I did this: 在JS我这样做:

      $('.imgpreview').click(function() { rotation += 90; rotation %= 360; $(this).rotate(rotation); // when rotation is 90 or 270 if ((rotation / 90) & 1) { $(this).css({ 'width': $('.img_wrap').innerHeight(), 'max-height': $('.img_wrap').innerWidth() }); } else { $(this).css({ 'width': 'auto', 'max-height': '100%' }); } }); 
  3. Note that the width / height calculations are done after the call to rotate function. 请注意, width / height计算是在调用rotate功能后完成的。 After rotation, width is height and vice-versa for the imgpreview and so we have to allow height to adjust while setting width of imgpreview - hence the max-height style adjustment. 旋转后, widthheight ,反之亦然为imgpreview ,所以我们必须允许height ,同时设置调节widthimgpreview -因此max-height风格的调整。

See demo below: 见下面的演示:

 $(document).ready(function() { var rotation = 0; jQuery.fn.rotate = function(degrees) { $(this).css({ '-webkit-transform': 'rotate(' + degrees + 'deg)', '-moz-transform': 'rotate(' + degrees + 'deg)', '-ms-transform': 'rotate(' + degrees + 'deg)', 'transform': 'rotate(' + degrees + 'deg)' }); }; $('.imgpreview').click(function() { rotation += 90; rotation %= 360; $(this).rotate(rotation); // when rotation is 90 or 270 if ((rotation / 90) & 1) { $(this).css({ 'width': $('.img_wrap').innerHeight(), 'max-height': $('.img_wrap').innerWidth() }); } else { $(this).css({ 'width': 'auto', 'max-height': '100%' }); } }); }); 
 body { margin: 0; } .img_wrap { width: 100%; position: relative; border: 3px solid black; padding-bottom: 56.25%; /*overflow: hidden;*/ } .imgpreview { display: block; max-height: 100%; max-width: 100%; width: auto; height: auto; position: absolute; top: 0; bottom: 0; left: 0; right: 0; margin: auto; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="img_wrap" id="P"> <img class="imgpreview" id="Pim" src="https://3.bp.blogspot.com/-W__wiaHUjwI/Vt3Grd8df0I/AAAAAAAAA78/7xqUNj8ujtY/s1600/image02.png" alt="your image" /> </div> <br/> <div class="img_wrap"> <img class="imgpreview" src="http://i.imgur.com/RRUe0Mo.png" alt="your image" /> </div> 

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

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