国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

js實現交換運動效果的方法

2019-11-20 12:43:29
字體:
來源:轉載
供稿:網友

本文實例講述了js實現交換運動效果的方法。分享給大家供大家參考。具體分析如下:

實現后的效果,點擊互相交換位置和距離左邊和上角的信息。

要點一:

var now = s_pic_li[0];for(var i=0; i<s_pic_li.length; i++){s_pic_li[i].onclick = function(){if(this == now) return false;var w = now.offsetWidth;var h = now.offsetHeight;var l = now.offsetLeft;var t = now.offsetTop;var w1= this.offsetWidth;var h1 = this.offsetHeight;var l1 = this.offsetLeft;var t1 = this.offsetTop;startrun(now,{width:w1,height:h1,left:l1,top:t1});startrun(this,{width:w,height:h,left:l,top:t});now=this;}}

循環給每一塊加點擊事件,獲取交換雙方的信息,然后執行運動函數,相關信息做為參數。

最后,上代碼:

<!DOCTYPE html><html><head><meta charset="gb2312" /><title>無標題文檔</title><style><!--body,ul,li{margin:0; padding:0;font:18px/1.5 arial; color:#333;}#big_pic{width:500px; height:400px;background:#ccc; text-align:center;position:absolute;}#s_pic li{float:left; width:100px;height:80px; display:inline;background:#06c; text-align:center;position:absolute; top:310px;}--></style><script><!--window.onload = function(){ var s_pic = document.getElementById("s_pic"); var s_pic_li = s_pic.getElementsByTagName("li"); var now = s_pic_li[0]; for(var i=0; i<s_pic_li.length; i++){  s_pic_li[i].onclick = function(){   if(this == now) return false;   var w = now.offsetWidth;   var h = now.offsetHeight;   var l = now.offsetLeft;   var t = now.offsetTop;   var w1= this.offsetWidth;   var h1 = this.offsetHeight;   var l1 = this.offsetLeft;   var t1 = this.offsetTop;   startrun(now,{width:w1,height:h1,left:l1,top:t1});   startrun(this,{width:w,height:h,left:l,top:t});   now=this;  } }}function getstyle(obj,name){ if(obj.currentStyle){  return obj.currentStyle[name]; }else{  return getComputedStyle(obj,false)[name]; }}function startrun(obj,json,fn){ clearInterval(obj.timer); obj.timer = setInterval(function(){  var isall = true;   for(var attr in json){   var cur=0;   if(attr == "opacity"){    cur = Math.round(parseFloat(getstyle(obj,attr))*100);   }else{    cur = parseInt(getstyle(obj,attr));   }   var speed = (json[attr] - cur)/8   speed = speed>0?Math.ceil(speed):Math.floor(speed);   if(cur != json[attr]){    isall = false;   }   if(attr == "opacity"){    obj.style.filter = "alpha(opacity="+(cur+speed)+")";    obj.style.opacity = (cur+speed)/100;   }else{    obj.style[attr] = cur+speed+"px";   }  }  if(isall){   clearInterval(obj.timer);   if(fn){    fn();   }  } },30);}//--></script></head><body><ul id="s_pic"> <li style="left:0; top:0; width:400px; height:300px">0</div> <li style="left:0;">1</li> <li style="left:110px;">2</li> <li style="left:220px;">3</li> <li style="left:330px;">4</li></ul></body></html>

希望本文所述對大家的javascript程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 行唐县| 京山县| 都昌县| 广德县| 汽车| 沈阳市| 乌拉特后旗| 奉新县| 固安县| 惠安县| 龙游县| 天气| 云林县| 凭祥市| 文安县| 祥云县| 绥德县| 桦川县| 临澧县| 阳信县| 习水县| 山西省| 铁岭县| 浦县| 石泉县| 柳江县| 宣恩县| 定南县| 齐齐哈尔市| 邹平县| 黄浦区| 嘉定区| 鹤岗市| 长丰县| 建平县| 柘荣县| 原平市| 涿鹿县| 托克逊县| 高邮市| 巴青县|