js实现运动logo图片效果及运动元素对象sportBox使用方法
2012-12-25 00:00
1416 查看
初始化(元素id, 元素移动的方向用123456789分别表示左上、上、右上、左、中、右、左下、下、右下。
撞击位置,9个数字分别表示9个地点,按顺序左上角、上、右上角、左、中、右、左下角、下、右下角,5居中。
运动的logo
/****************************************************************
* name:运动的元素对象sportBox
* DATA --2012-12-20-- 美工
*****************************************************************
*/
var sportBox = function() {}
sportBox.prototype = {
//初始化(元素id, 元素移动的方向用123456789分别表示左上、上、右上、左、中、右、左下、下、右下)
init: function(id, direction) {
var obj = this._getId(id), dir = direction || 9; //用1-9代表九个方向,5表示居中停止
obj.style.position = "absolute";
obj.timer = null;
this._sportDirection(obj, dir);
var _this = this;
obj.onmouseover = function() {
clearInterval(obj.timer);
};
obj.onmouseout = function() {
_this._sportDirection(obj);
};
},
_getId: function(id) {
return typeof id === "string" ? document.getElementById(id) : id;
},
//撞击的边界(返回新的运动方向)
_hitBorder: function(obj, direction) {
var d = direction || 9,b = obj,db = document.body || document.documentElement, h = 5,
hitsite = 5; //撞击位置,9个数字分别表示9个地点,按顺序左上角、上、右上角、左、中、右、左下角、下、右下角,5居中
if (b.offsetLeft 0 && b.offsetLeft+b.offsetWidth < db.clientWidth) {
hitsite = 2;
(b.offsetTop + b.offsetHeight >= db.clientHeight) && (hitsite = 8)
} else if (b.offsetLeft +b.offsetWidth >= db.clientWidth) {
hitsite = 6;
(b.offsetTop = db.clientHeight) && (hitsite = 9);
}
switch(hitsite) {
case 1:
b.style.left = 0+'px';
b.style.top = 0+'px';
(d ==1) && (h = 9);
(d ==2) && (h = 8);
(d ==8) && (h = 2);
break;
case 2:
b.style.top = 0+'px';
(d ==3) && (h = 9);
(d ==2) && (h = 8);
(d ==1) && (h = 7);
break;
case 3:
b.style.left = db.clientWidth-b.offsetWidth+'px';
b.style.top = 0+'px';
(d ==3) && (h = 9);
(d ==2) && (h = 8);
(d ==8) && (h = 2);
break;
case 4:
b.style.left = 0+'px';
(d ==7) && (h = 9);
(d ==4) && (h = 6);
(d ==1) && (h = 3);
break;
case 5://
h = 5;
break;
case 6:
b.style.left = db.clientWidth-b.offsetWidth+'px';
(d ==9) && (h = 7);
(d ==6) && (h = 4);
(d ==3) && (h = 1);
break;
case 7:
b.style.left = 0+'px';
b.style.top = db.clientHeight-b.offsetHeight+'px';
(d ==7) && (h = 9);
(d ==2) && (h = 8);
(d ==8) && (h = 2);
break;
case 8:
b.style.top = db.clientHeight-b.offsetHeight+'px';
(d ==9) && (h = 3);
(d ==7) && (h = 1);
(d ==8) && (h = 2);
break;
case 9:
b.style.left = db.clientWidth-b.offsetWidth+'px';
b.style.top = db.clientHeight-b.offsetHeight+'px';
(d ==8) && (h = 2);
(d ==6) && (h = 4);
(d ==9) && (h = 1);
break;
}
this._sportDirection(b, h);
},
//运动方向(运动方向)
_sportDirection: function(obj, direction) {
var db = document.body || document.documentElement, mX = 0, mY = 0, _this = this, h = direction || (Math.round(Math.random()) ? 9 : 7);
switch(h) {
case 1:
mX = -5;
mY = -5;
break;
case 2:
mX = 0;
mY = -5;
break;
case 3:
mX = 5;
mY = -5;
break;
case 6:
mX = 5;
mY = 0;
break;
case 9:
mX = 5;
mY = 5;
break;
case 8:
mX = 0;
mY = 5;
break;
case 7:
mX = -5;
mY = 5;
break;
case 5:
mX = 0;
mY = 0;
break;
case 4:
mX = -5;
mY = 0;
break;
default:
//alert("你输入的方向不对,只允许1-9,5表停止");
}
obj.timer = setInterval(function() {
if (obj.offsetLeft < 0 || obj.offsetTop < 0 || obj.offsetWidth+obj.offsetLeft > db.clientWidth || obj.offsetHeight+obj.offsetTop > db.clientHeight) {
clearInterval(obj.timer);
_this._hitBorder(obj, h);
} else {
obj.style.left = obj.offsetLeft + mX +'px';
obj.style.top = obj.offsetTop + mY +'px';
}
},10);
}
}
/* 创建一个调用的实例 */
var s = new sportBox();
s.init('box', 4); //参数表示元素id和方向(方向缺默向右下)
s.init('box2');
[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
撞击位置,9个数字分别表示9个地点,按顺序左上角、上、右上角、左、中、右、左下角、下、右下角,5居中。
运动的logo
/****************************************************************
* name:运动的元素对象sportBox
* DATA --2012-12-20-- 美工
*****************************************************************
*/
var sportBox = function() {}
sportBox.prototype = {
//初始化(元素id, 元素移动的方向用123456789分别表示左上、上、右上、左、中、右、左下、下、右下)
init: function(id, direction) {
var obj = this._getId(id), dir = direction || 9; //用1-9代表九个方向,5表示居中停止
obj.style.position = "absolute";
obj.timer = null;
this._sportDirection(obj, dir);
var _this = this;
obj.onmouseover = function() {
clearInterval(obj.timer);
};
obj.onmouseout = function() {
_this._sportDirection(obj);
};
},
_getId: function(id) {
return typeof id === "string" ? document.getElementById(id) : id;
},
//撞击的边界(返回新的运动方向)
_hitBorder: function(obj, direction) {
var d = direction || 9,b = obj,db = document.body || document.documentElement, h = 5,
hitsite = 5; //撞击位置,9个数字分别表示9个地点,按顺序左上角、上、右上角、左、中、右、左下角、下、右下角,5居中
if (b.offsetLeft 0 && b.offsetLeft+b.offsetWidth < db.clientWidth) {
hitsite = 2;
(b.offsetTop + b.offsetHeight >= db.clientHeight) && (hitsite = 8)
} else if (b.offsetLeft +b.offsetWidth >= db.clientWidth) {
hitsite = 6;
(b.offsetTop = db.clientHeight) && (hitsite = 9);
}
switch(hitsite) {
case 1:
b.style.left = 0+'px';
b.style.top = 0+'px';
(d ==1) && (h = 9);
(d ==2) && (h = 8);
(d ==8) && (h = 2);
break;
case 2:
b.style.top = 0+'px';
(d ==3) && (h = 9);
(d ==2) && (h = 8);
(d ==1) && (h = 7);
break;
case 3:
b.style.left = db.clientWidth-b.offsetWidth+'px';
b.style.top = 0+'px';
(d ==3) && (h = 9);
(d ==2) && (h = 8);
(d ==8) && (h = 2);
break;
case 4:
b.style.left = 0+'px';
(d ==7) && (h = 9);
(d ==4) && (h = 6);
(d ==1) && (h = 3);
break;
case 5://
h = 5;
break;
case 6:
b.style.left = db.clientWidth-b.offsetWidth+'px';
(d ==9) && (h = 7);
(d ==6) && (h = 4);
(d ==3) && (h = 1);
break;
case 7:
b.style.left = 0+'px';
b.style.top = db.clientHeight-b.offsetHeight+'px';
(d ==7) && (h = 9);
(d ==2) && (h = 8);
(d ==8) && (h = 2);
break;
case 8:
b.style.top = db.clientHeight-b.offsetHeight+'px';
(d ==9) && (h = 3);
(d ==7) && (h = 1);
(d ==8) && (h = 2);
break;
case 9:
b.style.left = db.clientWidth-b.offsetWidth+'px';
b.style.top = db.clientHeight-b.offsetHeight+'px';
(d ==8) && (h = 2);
(d ==6) && (h = 4);
(d ==9) && (h = 1);
break;
}
this._sportDirection(b, h);
},
//运动方向(运动方向)
_sportDirection: function(obj, direction) {
var db = document.body || document.documentElement, mX = 0, mY = 0, _this = this, h = direction || (Math.round(Math.random()) ? 9 : 7);
switch(h) {
case 1:
mX = -5;
mY = -5;
break;
case 2:
mX = 0;
mY = -5;
break;
case 3:
mX = 5;
mY = -5;
break;
case 6:
mX = 5;
mY = 0;
break;
case 9:
mX = 5;
mY = 5;
break;
case 8:
mX = 0;
mY = 5;
break;
case 7:
mX = -5;
mY = 5;
break;
case 5:
mX = 0;
mY = 0;
break;
case 4:
mX = -5;
mY = 0;
break;
default:
//alert("你输入的方向不对,只允许1-9,5表停止");
}
obj.timer = setInterval(function() {
if (obj.offsetLeft < 0 || obj.offsetTop < 0 || obj.offsetWidth+obj.offsetLeft > db.clientWidth || obj.offsetHeight+obj.offsetTop > db.clientHeight) {
clearInterval(obj.timer);
_this._hitBorder(obj, h);
} else {
obj.style.left = obj.offsetLeft + mX +'px';
obj.style.top = obj.offsetTop + mY +'px';
}
},10);
}
}
/* 创建一个调用的实例 */
var s = new sportBox();
s.init('box', 4); //参数表示元素id和方向(方向缺默向右下)
s.init('box2');
[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
相关文章推荐
- js实现运动logo图片效果及运动元素对象sportBox使用方法
- 使用MVC中的HtmlHelper对象的ActionLink方法,实现一个Javascript:void(0)效果,去调用一个JS方法
- HTML使用FileReader对象的readAsDataURL()方法实现图片的上传预览效果
- JS 使用 window对象的print方法css实现分页打印
- 使用js实现有光源扫描图片效果
- 使用JS实现图片展示瀑布流效果
- swipe.js工具包实现手机web滑动轮播效果存在jQuery对象不能使用问题并解决(个人项目经验)
- JS实现FLASH幻灯片图片切换效果的方法
- js实现缓冲运动效果的方法
- ScrollReveal.js – 帮助你实现超炫的元素运动效果
- 两种方法使用js读写cookie实现一个底部广告浮层效果
- CSS3 animation实现图片轮播效果 自动显示 无需使用js 含代码(图片轮播效果一)
- C#在WinForm中使用WebKit传递js对象实现与网页交互的方法
- js实现使用鼠标拖拽切换图片的方法
- js实现使用鼠标拖拽切换图片的方法
- JS实现淡入淡出图片效果的方法分析
- js实现图片漂浮效果的方法
- 使用js实现图片渐出效果
- JS上传图片前实现图片预览效果的方法
- 原生JS实现图片无缝滚动方法(附带封装的运动框架)