【问题标题】:Rotate an element in a rotating div在旋转的 div 中旋转元素
【发布时间】:2019-07-03 18:58:59
【问题描述】:

我现在正在做一个项目,我必须在<img> 标签周围画八边形。这些八边形用作菜单项。如果打开一个菜单,中心节点应该旋转,但只有八角形而不是<img>

使用以下代码在图像周围绘制八边形:

.octagonWrap{
    width: 70px;
    height: 70px;
    float: left;
    position: absolute;
    overflow: hidden;
}
.octagon{
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    overflow: hidden;
    transform: rotate(45deg);
    background-color: transparent;
    border: 2px solid #ff7a00;
}

.octagon:before{
    position: absolute;
    top: -2px;
    right: -2px;
    bottom: -2px;
    left: -2px;
    transform: rotate(-45deg);
    content: '';
    border: inherit;
}

所以基本结构是这样的:

<div class="octagonWrap">
   <div class="octagon">
      <img style="transform:rotate(-45deg)">
   </div>
</div>

如果一个八边形被点击,最外层的 div 会得到 .rotating 类:

.rotating {
   -webkit-animation: rotating 8s linear infinite;
   -moz-animation: rotating 8s linear infinite;
   -ms-animation: rotating 8s linear infinite;
   -o-animation: rotating 8s linear infinite;
}

该类使用以下 webkit 函数:

@-webkit-keyframes rotating{
from {
    -webkit-transform: rotate(0deg);
    -o-transform: rotate(0deg);
   }

to {
    -webkit-transform: rotate(36deg);
    -o-transform: rotate(360deg);
   }
}

@keyframes rotating {
    from {
    -ms-transform: rotate(0deg);
    -moz-transform: rotate(0deg);
    -webkit-transform: rotate(0deg);
    -o-transform: rotate(0deg);
   }

to {
    -ms-transform: rotate(360deg);
    -moz-transform: rotate(360deg);
    -webkit-transform: rotate(360deg);
    -o-transform: rotate(360deg);
   }
}

为了防止内部图像旋转,我编写了一个名为.counterrotation 的类,它与.rotation 类基本相同,但逆时针方向。所以八边形的边框会转动,但图像是静止的。

问题

因为在菜单打开的整个过程中图像都是逆时针旋转的,所以样式属性transform:rotate(-45deg) 不再起作用了。

正如您在此处看到的那样,八边形转动,图像静止,但图像在不应该转动的地方转动了 45 度。

有没有办法转动图像,使其水平对齐而不与其他八边形一起转动?

如果没有,是否可以首先防止图像旋转?

编辑 1: 不知何故工作代码: https://jsfiddle.net/mrcrane/rz285mw9/13/

【问题讨论】:

  • 你能分享一个完整的工作代码吗?这对我们来说比单独的代码部分更容易
  • 代码嵌入在一个更大的 ASP.NET 项目中,这就是为什么 jsfiddle 中的布局有点破损,但我提供了一个工作示例。
  • 这是你想要的jsfiddle.net/mzkrjf8b 吗?
  • 为什么不把图片和八边形分开呢?如果图像只是在八角形顶部(而不是在其中)渲染,则旋转八角形不会对图像产生任何影响。
  • @TemaniAfif 完全正确!谢谢!

标签: html css css-animations css-transforms


【解决方案1】:

由于图像已经旋转-45deg,只需更改反向旋转动画

@keyframes counterrotating {
  from {
   transform: rotate(-45deg);
  }
  to {
    transform: rotate(-405deg);
  }
}

$(document).on("click", '.octa', function(e) {
  $(e.target.parentElement).parent().addClass("open rotating");
  $(e.target).addClass("counterrotating");
});
.octagonWrap {
  width: 70px;
  height: 70px;
  float: left;
  position: absolute;
  overflow: hidden;
}

.octagon {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  overflow: hidden;
  transform: rotate(45deg);
  background-color: transparent;
  border: 2px solid #ff7a00;
}

.octagon::before {
  position: absolute;
  top: -2px;
  right: -2px;
  bottom: -2px;
  left: -2px;
  transform: rotate(45deg);
  content: '';
  border: inherit;
}

@keyframes rotating {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

.rotating {
  -webkit-animation: rotating 8s linear infinite;
  -moz-animation: rotating 8s linear infinite;
  -ms-animation: rotating 8s linear infinite;
  -o-animation: rotating 8s linear infinite;
}

@keyframes counterrotating {
  from {
    transform: rotate(-45deg);
  }
  to {
    transform: rotate(-405deg);
  }
}

.counterrotating {
  -webkit-animation: counterrotating 8s linear infinite;
  -moz-animation: counterrotating 8s linear infinite;
  -ms-animation: counterrotating 8s linear infinite;
  -o-animation: counterrotating 8s linear infinite;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="graphContainer" style="position:absolute; width:100%;height:100%;cursor:default;">
  <div id="add" style="position:absolute; top:10% ;left:50%" class='octagonWrap menuItem open'>
    <div class='octagon' style="width:100%;height:100%;">
      <img class="octa" src="https://image.flaticon.com/icons/svg/149/149098.svg" style="position:absolute; top:0;left:0;bottom:0;right:0; margin:auto; transform: rotate(-45deg); width:50px; height:50px" />
    </div>
  </div>
</div>

【讨论】:

  • 与 Afif 在 cmets 中提出的策略相同。也有效。
猜你喜欢
  • 2011-07-12
  • 1970-01-01
  • 2023-01-18
  • 1970-01-01
  • 1970-01-01
  • 2011-08-24
  • 2021-12-01
  • 1970-01-01
  • 2014-02-27
相关资源
最近更新 更多