gpt4 book ai didi

jquery - 在悬停时创建 CSS 'path'

转载 作者:数据小太阳 更新时间:2023-10-29 09:13:30 25 4
gpt4 key购买 nike

我正在尝试使用(主要)CSS 生成一个“漂亮”的 CSS 菜单,但有一个微小位的 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 中生成一种“确定 Angular/位置”的方法(老实说,效果不太好)。


类似的设计:

Enter image description here


所以最后,由于有四个 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>

Fiddle


有人会:

  • (A):知道如何让 div 相对于 jQuery 代码中指定的父级“旋转”指定的 Angular 或距离吗?

  • (B):让“动画”在悬停时重置?

  • (C)​​: 知道我在说什么吗?

类似的实现(虽然不完全):

最佳答案

使用不同的方法,您会得到略有不同的效果。您可以使用 setTimeouttransition 的时间来修改行为。

See the fiddle

+ function() {
var to;
$(".wrap").on('mouseenter', function() {
var circles = $(this).children();
var degree = (2 * Math.PI) / circles.length; //calc delta angle
var transforms = [];

// Calculate the position for each circle
circles.each(function(index) {
var x = 100 * Math.cos(-0.5 * Math.PI + degree * (-1 * index - 0.5));
var y = 100 * Math.sin(-0.5 * Math.PI + degree * (-1 * index - 0.5));

transforms.push('translate(' + x + 'px,' + y + 'px)');
});

// Function to moves all the circles
// We'll pop a circle each time and than call this function recursively
function moveCircles() {
var transform = transforms.shift();
circles.css('transform', transform);

circles.splice(0, 1);
if (circles.length) to = setTimeout(moveCircles, 400);
}

moveCircles();
});

$(".wrap").on('mouseleave', function() {
var circles = $(this).children().css('transform', '');
clearTimeout(to);
});
}();
   .wrap {
height: 300px;
width: 300px;
background: red;
position: relative;
transform-origin: center center;
transition: all 0.8s;
}
.circle {
transition: all 0.8s;
position: absolute;
height: 50px;
width: 50px;
text-align: center;
line-height: 50px;
top: calc(50% - 25px);
left: calc(50% - 25px);
background: tomato;
border-radius: 50%;
}
<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 class="circle">5</div>
<div class="circle">6</div>
</div>

关于jquery - 在悬停时创建 CSS 'path',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28497425/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com