js實(shí)現(xiàn)交換運(yùn)動(dòng)效果的方法
本文實(shí)例講述了js實(shí)現(xiàn)交換運(yùn)動(dòng)效果的方法。分享給大家供大家參考。具體分析如下:
實(shí)現(xiàn)后的效果,點(diǎn)擊互相交換位置和距離左邊和上角的信息。
要點(diǎn)一:
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;
}
}
循環(huán)給每一塊加點(diǎn)擊事件,獲取交換雙方的信息,然后執(zhí)行運(yùn)動(dòng)函數(shù),相關(guān)信息做為參數(shù)。
最后,上代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset="gb2312" />
<title>無(wú)標(biāo)題文檔</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>
希望本文所述對(duì)大家的javascript程序設(shè)計(jì)有所幫助。
相關(guān)文章
javascript實(shí)現(xiàn)全局匹配并替換的方法
這篇文章主要介紹了javascript實(shí)現(xiàn)全局匹配并替換的方法的總結(jié),十分的簡(jiǎn)單實(shí)用,有需要的小伙伴可以參考下。2015-04-04
ESC之ESC.wsf可以實(shí)現(xiàn)javascript的代碼壓縮附使用方法
可以對(duì)javascript的大小進(jìn)行壓縮。使javascript的加載速度變快。2007-05-05
javascript實(shí)現(xiàn)無(wú)法關(guān)閉的彈框
本文分享了javascript實(shí)現(xiàn)無(wú)法關(guān)閉的彈框的實(shí)例代碼,感興趣的朋友可以看下2016-11-11
微信小程序下面商品左右滑動(dòng)上面tab也跟隨變動(dòng)功能實(shí)現(xiàn)
這篇文章主要介紹了小程序下面商品左右滑動(dòng)上面tab也跟隨變動(dòng)功能,也就是點(diǎn)擊tab切換下面的商品信息,滑動(dòng)下面的商品信息tab也進(jìn)行切換,本文給大家分享實(shí)現(xiàn)代碼,需要的朋友可以參考下2022-07-07
詳解webpack import()動(dòng)態(tài)加載模塊踩坑
這篇文章主要介紹了詳解webpack import()動(dòng)態(tài)加載模塊踩坑,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
使用JS實(shí)現(xiàn)jQuery的addClass, removeClass, hasClass函數(shù)功能
這篇文章主要介紹了使用JS實(shí)現(xiàn)jQuery的addClass, removeClass, hasClass函數(shù)功能,需要的朋友可以參考下2014-10-10
JavaScript運(yùn)動(dòng)框架 鏈?zhǔn)竭\(yùn)動(dòng)到完美運(yùn)動(dòng)(五)
這篇文章主要為大家詳細(xì)介紹了JavaScript運(yùn)動(dòng)框架的第五部分,鏈?zhǔn)竭\(yùn)動(dòng)到完美運(yùn)動(dòng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
淺談javascript對(duì)象模型和function對(duì)象
這篇文章主要簡(jiǎn)單介紹了javascript對(duì)象模型和function對(duì)象,以及相關(guān)的示例代碼,非常實(shí)用,需要的小伙伴參考下2014-12-12

