gpt4 book ai didi

javascript - 如何在悬停时切换动画

转载 作者:行者123 更新时间:2023-11-28 05:12:01 25 4
gpt4 key购买 nike

嗨,我想在将鼠标悬停在某个 div 上时进行动画切换,目前它会启动但不会停止。有人可以查看我的代码并告诉我我做错了什么吗?

$(document).ready(function() {

function loopAnimate(x, y) {
$(x)
.animate({
top: "1px",
left: "1px",
height: "290px",
width: "290px"
}, y / 1.5)
.animate({
top: "-=104px",
left: "-=104px",
height: "500px",
width: "500px"
}, y, function() {
loopAnimate(x, y);
})
}

function loopStop(x) {
$(x).stop();

$(x)
.css({
top: "1px",
left: "1px",
height: "290px",
width: "290px"
});
}


$('#button').hover(function() {
loopAnimate('.circle1', 1600)
loopAnimate('.circle2', 1700)
loopAnimate('.circle3', 1800)
}, function() {
loopStop('.circle1')
loopStop('.circle2')
loopStop('.circle3')
})

$('.wrapper').css('top', $(window).height() / 2 - 150);
$('.wrapper').css('left', $(window).width() / 2 - 150);


})
body {
padding: 0;
margin: 0;
background: gray;
overflow: hidden;
}

.wrapper {
width: 0;
position: relative;
}

.circle1 {
z-index: -1000;
position: absolute;
top: 1px;
left: 1px;
height: 290px;
width: 290px;
background: transparent;
border: 4px #eee solid;
border-radius: 100000000px;
display: inline-block;
}

.circle2 {
z-index: -1000;
position: absolute;
top: 1px;
left: 1px;
height: 290px;
width: 290px;
background: transparent;
border: 4px #eee solid;
border-radius: 100000000px;
display: inline-block;
}

.circle3 {
z-index: -1000;
position: absolute;
top: 1px;
left: 1px;
height: 290px;
width: 290px;
background: transparent;
border: 4px #eee solid;
border-radius: 100000000px;
display: inline-block;
}

#button {
position: relative;
background: black;
border-radius: 1000px;
z-index: 10;
height: 300px;
width: 300px;
transition: 0.33s;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='wrapper'>
<div class="circle1"></div>
<div class="circle2"></div>
<div class="circle3"></div>
<div id='button'></div>
</div>

我主要希望它能顺利地突然停止。我真的希望有人能帮助我。

编辑:已解决。也改变了loopStop

函数循环停止(x) { $(x).stop(true, false);

$(x)
.animate({
top: "1px",
left: "1px",
height: "290px",
width: "290px"
});

}

最佳答案

试试这个:

    $(document).ready(function() {

function loopAnimate(x, y) {
$(x)
.animate({
top: "1px",
left: "1px",
height: "290px",
width: "290px"
}, y / 1.5)
.animate({
top: "-=104px",
left: "-=104px",
height: "500px",
width: "500px"
}, y, function() {
loopAnimate(x, y);
})
}

function loopStop(x) {
x="."+x;
$(x).stop(true,false);
$(x).css({
top: "1px",
left: "1px",
height: "290px",
width: "290px"
});
}

$('#button').mouseenter(function() {
loopAnimate('.circle1', 1600)
loopAnimate('.circle2', 1700)
loopAnimate('.circle3', 1800)
});
$('#button').mouseleave(function() {
loopStop('circle1')
loopStop('circle2')
loopStop('circle3')
});

$('.wrapper').css('top', $(window).height() / 2 - 150);
$('.wrapper').css('left', $(window).width() / 2 - 150);


});
body {
padding: 0;
margin: 0;
background: gray;
overflow: hidden;
}

.wrapper {
width: 0;
position: relative;
}

.circle1 {
z-index: -1000;
position: absolute;
top: 1px;
left: 1px;
height: 290px;
width: 290px;
background: transparent;
border: 4px #eee solid;
border-radius: 100000000px;
display: inline-block;
}

.circle2 {
z-index: -1000;
position: absolute;
top: 1px;
left: 1px;
height: 290px;
width: 290px;
background: transparent;
border: 4px #eee solid;
border-radius: 100000000px;
display: inline-block;
}

.circle3 {
z-index: -1000;
position: absolute;
top: 1px;
left: 1px;
height: 290px;
width: 290px;
background: transparent;
border: 4px #eee solid;
border-radius: 100000000px;
display: inline-block;
}

#button {
position: relative;
background: black;
border-radius: 1000px;
z-index: 10;
height: 300px;
width: 300px;
transition: 0.33s;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='wrapper'>
<div class="circle1"></div>
<div class="circle2"></div>
<div class="circle3"></div>
<div id='button'></div>
</div>

关于javascript - 如何在悬停时切换动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41294478/

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