【发布时间】:2015-04-14 08:47:55
【问题描述】:
我正在尝试使用(主要)CSS 生成一个“漂亮”的 CSS 菜单,但也有一个 tiny 位 jQuery:
我的总体思路是:
+------------------------+
| |
| |
| +---+ |
| | | |
| |___| | <-- Hover this center piece
| |
| |
| |
+------------------------+
+------------------------+
| _ |
| |\ | <-- All start moving up to top of screen
| \ +---+ |
| | | |
| |___| |
| |
| |
| |
+------------------------+
+------------------------+
| +---+ |
| | | |
| |___| |
| |
| || All, but one |
| || moves down |
| \/ |
| |
+------------------------+
+------------------------+
| +---+ |
| | | |
| |___| |
| |
| One stays, |
| +---+ the rest move this way
| | | ---> |
| |___| |
+------------------------+
+------------------------+
| +---+ |
| | | |
| |___| ^ | The rest move up
| | |
| | |
| +---+ +---+ |
| | | | | |
| |___| |___| |<-- Another stays
+------------------------+
完成:
+------------------------+
| +---+ +---+ |
| | 1 | | 4 | |
| |___| |___| |
| |
| |
| +---+ +---+ |
| | 2 | | 3 | |
| |___| |___| |
+------------------------+
但是,假设会有四个 div 孩子,所以我试图在 jQuery 中生成一种“确定角度/位置”的方法(老实说,这种方法效果不太好)。
类似的设计:
所以最后,由于有四个 div,它们将与中心相隔 90 度(360/4 div = 90 度)。
假设有六个子 div;
360/6 = 60 degrees
因此它们将以 60 度的间隔均匀分布。
我将在它们之间添加动画,因此我一直在玩旋转等,但我似乎无法掌握它:
我目前的样本是:
$(".wrap").hover(function(){
var x =$(this).children().length; //Counts '.circles'
var degree = 360 / x; //Gets angle
var percent = 100 / x;
var curPercent = percent;
$(this).children().each(function (index) {
$(this).css("transform","rotate(" + degree*index + "deg)");
$(this).css("top",percent + "px");
$(this).css("left",percent + "px");
percent = percent + curPercent;
});
});
.wrap{
height: 300px;
width: 300px;
background: red;
position: relative;
transform-origin: center center;
transition: all 0.8s;
}
.wrap:hover .circle{
top: 0;
left: 0;
}
.circle{
transition: all 0.8s;
position: absolute;
height: 50px;
width: 50px;
top: calc(50% - 25px);
left: calc(50% - 25px);
background: tomato;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrap">
<div class="circle">1</div>
<div class="circle">2</div>
<div class="circle">3</div>
<div class="circle">4</div>
</div>
有人愿意吗:
(A):知道如何让 div '旋转'与 jQuery 代码中指定的父级相关的指定角度或距离吗?
(B):让“动画”在悬停时重置?
- (C):
知道我在说什么吗?
类似的实现(虽然不准确):
- Here
- This more so - 但这使用了Sass(不需要)
【问题讨论】:
-
你知道@keyframes吗?因为看起来你可以完全用 css 做你想做的事......
-
@Pa3k.m: 偶数度计算基于no。孩子?
-
@Pa3k.m:关键帧的唯一问题是它会寻找一个结束位置(直到 jquery 运行我才知道),而且还有很多前缀要添加.
-
@downvoter 你能解释一下为什么吗?
-
那句话一点都不清楚,这就是Pa3k.m问的原因。您希望制作动画的精灵/div 的最大和最小数量是多少?你什么时候想确定角度?在动画期间,之前还是之后?何时确定 div 的数量 - 生成 HTML 时、页面加载时还是用户使用时? (如果是最后一个,具体是什么时候?)
标签: jquery css css-position css-transitions