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

标签 javascript jquery

嗨,我想在将鼠标悬停在某个 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/

相关文章:

javascript - 如何隐藏内容一和显示内容二?

Javascript 到 Perl 数组/哈希转换

javascript - 流量: Simpler way to annotate `type | typeof undefined` ?

javascript - 如何使用 Angular $resource 传递查询参数数组

javascript - 完整日历中的空白空间

javascript : creating elements on first click and deleting the created element on second click

javascript - 将 Adob​​e 创意编辑图像上传到我的服务器而不是他们的 Creative Cloud

java - 从 URL 中检索片段(散列)并将值注入(inject) bean

javascript - 使用 jQuery 实现类似光标轨迹的效果

javascript - jQuery Mobile - 动态创建表单元素