知识点---animate()动画滞后执行的解决方案

jQuery动画:

animate 容易出现连续触发、滞后反复执行的现象;

针对 jQuery 中 slideUp、slideDown、animate 等动画运用时出现的滞后反复执行等问题的解决方法有如下:

1、在触发元素上的事件设置为延迟处理, 即可避免滞后反复执行的问题(使用setTimeout)

2、在触发元素的事件时预先停止所有的动画,再执行相应的动画事件(使用stop)推荐这种。

//第二种方式 $(".container").stop();//停止当前动画,继续下一个动画 $(".container").stop(true);//清除元素的所有动画 $(".container").stop(false, true);//让当前动画直接到达末状态 ,继续下一个动画 $(".container").stop(true, true);//清除元素的所有动画,让当前动画直接到达末状态

//例如,

1 $(".container").stop().animate({ 2 marginTop:"60px", 3   width:"100px", 4 height:"50px" 5 },500);

动画效果一、

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("#start").click(function(){
    $("div").animate({height:300},1500);
    $("div").animate({width:300},1500);
    $("div").animate({height:100},1500);
    $("div").animate({width:100},1500);
  });
  $("#stop").click(function(){
    $("div").clearQueue();
  });
});
</script>
</head>
<body>

  <button id="start">开始动画</button>
  <button id="stop">停止动画</button>
  <br><br>
  <div style="background:red;height:100px;width:100px;"></div>

</body>
</html>

动画效果二、

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("#flip").click(function(){
    $("#panel").slideToggle("slow");
  });
});
</script>

<style type="text/css">
  #panel,#flip
  {
  padding:5px;
  text-align:center;
  background-color:#e5eecc;
  border:solid 1px #c3c3c3;
  }
  #panel
  {
  padding:50px;
  display:none;
  }
</style>
</head>
<body>

  <div id="flip">点我,显示或隐藏面板。</div>
  <div id="panel">Hello world!</div>

</body>
</html>

动画效果三、

<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$(".btn1").click(function(){
$("p").animate({top:"100px"});
});
$(".btn2").click(function(){
$("p").animate({top:"0px"});
});
});
</script>
</head>
<body>

<button class="btn1">动画</button>
<button class="btn2">重置</button>
<p style="position:relative">这是一个段落。</p>

</body>
</html>

转载于:https://www.cnblogs.com/liaohongwei/p/10724034.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值