html - 以 Angular 6 旋转框架内的矩形图像

标签 html css angular

我想像这样在一个相同大小的框架内旋转一个矩形图像:

<div style="border:1px solid #d3d3d3;width:208px;height:250px">
 <img style="width:208px;height:250px" src="https://www.webslake.com/w_img/t_i/lpw.png" [style.transform]="styles"> </div>

   <button type="button" class="btn btn-undo btn-lg" (click)="rotateImage('left')">

和旋转图像的按钮:

rotateImage(direction) {    
    if (direction === 'left') {
      this.rotationAmount = this.rotationAmount + -90;
    } else if(direction === 'right') {
      this.rotationAmount = this.rotationAmount + 90;
    }
    this.styles =  'rotate(' + this.rotationAmount + 'deg)';

  }

如何在不改变宽度和高度的情况下在 div 框架内进行旋转,因为如果我旋转 (new)width = old(height) 反之亦然,我希望在旋转过程中具有相同的宽度和高度.

最佳答案

我在样式中添加了计算顶部和左侧的逻辑:

rotateImage(direction: string) {

 if (direction === 'left') {
   let flag = this.width;
   this.width = this.height;
   this.height = flag;
   this.rotationAmount = this.rotationAmount + -90;
   if (this.rotationAmount == -360) {
    this.rotationAmount = 0;
   }
  console.log(this.rotationAmount);
   if (this.rotationAmount === -180 || this.rotationAmount === 0) {
     this.top = 0;
     this.left = 0;
   } else {
     this.top = (this.width-this.height)/2 * (-1); 
     this.left = (this.width-this.height)/2;
     console.log(this.top, this.left)
   }
} 

 else if(direction === 'right') {
   this.rotationAmount = this.rotationAmount + 90;
 }
 this.styles =  'rotate(' + this.rotationAmount + 'deg)';
}

这是 stackblitz 的链接 https://stackblitz.com/edit/angular-gz9zqs?embed=1&file=src/app/app.component.ts

关于html - 以 Angular 6 旋转框架内的矩形图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53259421/

相关文章:

html - HTML 表格列的宽度分配取决于行数

html - 垂直居中水平导航

ios - UIWebView 和 page-break-* css 不适用于 Airprint

css - float div 的问题

javascript - 在页面加载后在 li 上添加类删除

javascript - 浏览器登录自动完成

css - 添加 :last-child or :last-of-type to nav menu

angular - 使用@angular/router 3.0.0-rc.1向路由添加防护时出错

angular - Ansible:未找到ng

json - 在 JSON.stringify () 处将循环结构转换为 JSON