首頁  >  文章  >  web前端  >  jQuery實現的滑鼠響應緩衝動畫效果

jQuery實現的滑鼠響應緩衝動畫效果

亚连
亚连原創
2018-06-05 17:45:191690瀏覽

這篇文章主要介紹了jQuery實現的滑鼠回應緩衝動畫效果,涉及jQuery事件回應、數值運算及頁面元素動態操作相關技巧,需要的朋友可以參考下

本文實例講述了jQuery實現的滑鼠響應緩衝動畫效果。分享給大家供大家參考,具體如下:

先來看看運行效果:

具體程式碼如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>js动画-缓冲动画</title>
<script src="http://libs.baidu.com/jquery/1.10.2/jquery.js"></script>
<style>
* {
  margin: 0;
  padding: 0;
  font-family:"微软雅黑"
}
#box{
  height:100px;
  width:100px;
  background-color:#0099CC;
  margin-top:200px;
  position:relative;
  /*速速、缓冲变化*/
  left:-100px;
}
span{
  display:block;
  color:blue;
  width:25px;
  height:100px;
  background-color:#FFFF99;
  position:absolute;
  left:100px;
}
</style>
</head>
<body>
  <p id="box">
    <span>移动</span>
  </p>
<script>
window.onload=function(){
  var p1=document.getElementById("box");
  p1.onmouseover=function(){
    startMove(0);
  }
  p1.onmouseout=function(){
    startMove(-100);
  }
}
var timer=null;
function startMove(itarget){
    clearInterval(timer);
    var p1=document.getElementById("box");
    timer=setInterval(function(){
      var speed=(itarget-p1.offsetLeft)/20;
      speed=speed>0?Math.ceil(speed):Math.floor(speed);
      if(p1.offsetLeft==itarget){
        clearInterval(timer);
      }else{
        p1.style.left=p1.offsetLeft+speed+"px";
      }
    },30)
}
</script>
</body>
</html>

上面是我整理給大家的,希望今後對大家有幫助。

相關文章:

如何在vue2中設定全域變數? (詳細教學)

根據vue裡面設定全域變數或資料方法(詳細教學)

如何在JS中實作字串拼接的功能(擴充String.prototype.format)

以上是jQuery實現的滑鼠響應緩衝動畫效果的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn