[原](源码)JS OOP 实现页面浮动图片,支持多图同时浮动,简单调用
2008-03-25 12:10
1166 查看
前几天女朋友所在公司网站上要加上浮动图片的显示,她在网站上找了几个"特效"代码感觉都不太好用,于是我就给写了一个,使用JS 的OOP方式实现,可以同时浮动多图,代码如下:
// 功能:在网页中浮动显示指定的图像
// 作者:yangwengang@hotmail.com (刚)
// 日期:2008-3-20
// 转载请保留本注释部分
//
// ***************************************************************************
// **承接ASP.NET开发外包**网站优化**网站架构规划**技术支持**3+年实际开发经验**
// **Email:yangwengang@hotmail.com**所在地:青岛**
// ***************************************************************************
//
// 使用方法:
// 1.在网页中添加此JS脚本文件的引用,如:<script type="text/javascript" src="floatimg.js"></script>
// 2.只需在网页加载(或单击按扭后)时调用FloatImg对象的show方法即可,如:
// <script type="text/javascript"><!--//
// var f1 = new FloatImg();//创建新的FloatImg对象
// f1.show("f1","test1.jpg","http://www.caraq.com",137,81,1,0);
// var f2 = new FloatImg();//创建新的FloatImg对象
// f2.show("f2","test2.jpg","http://www.caraq.com",132,109,0.5,1);
// --></script>
// show 方法的参数依次为:FloatImg对象名,图像地址,点击图像转向URL,图像宽度,图像高度,移动速度(默认1),图像索引(当页面中需要显示多个浮动图像时分别指定不同的数值,如:0,1,2..)
//
var FloatImg = function() {
this._interval_time= 10;//每次移动图像的间隔时间(ms),越小越平滑速度越慢,可适当修改为:10,20,30...
//以下内容若不了解请不要修改
this._x = 0;
this._y = 0;
this._interval_flag = 0;
this._speed_x = 1;
this._speed_y = 1;
this._floatID_beforeStr = "floatimg";
this._width = 0;
this._height = 0;
this._index = 0;
this._objName = "";
this._doc = document.documentElement;
}
FloatImg.prototype.show =
function(objname,img,clicktourl,width,height,speed,index){
this._speed_x = speed;
this._speed_y = speed;
this._width = width;
this._height = height;
this._index = index;
var cw = this._doc.clientWidth-10;
var ch = this._doc.clientHeight-10;
this._x = cw/(index+1);
this._y = ch/(index+1);
this._objName = objname;
if(!this.G(this._floatID_beforeStr+this._index)){
document.body.innerHTML += "<div onmouseover=""+this._objName+".stop();" onmouseout=""+this._objName+".start();" id=""+this._floatID_beforeStr+index+"" style="position:absolute;z-index:10000;left:0px;top:0px;width:"+width+"px;height:"+height+"px;overflow:hidden;"><a href=""+clicktourl+"" target=_blank><img src=""+img+"" border=0/></a></div>";
}else{
this.G(this._floatID_beforeStr+index).innerHTML = "<a href=""+clicktourl+"" target=_blank><img src=""+img+"" border=0/></a>";
}
this.start();
};
FloatImg.prototype.start=
function(){
this._interval_flag = window.setInterval("FloatImg.interval("+this._objName+")",this._interval_time);
};
FloatImg.prototype.G=
function(v){ return document.getElementById(v);};
FloatImg.interval =
function(a){
var cw = a._doc.clientWidth-10;
var ch = a._doc.clientHeight-10;
if(a.G(a._floatID_beforeStr+a._index)){
if(a._x < 0 || a._x+a._width > cw){
if(a._x + a._width > cw)
a._x = cw - a._width;
else
a._x = 0;
a._speed_x = -a._speed_x;
}
if(a._y < 0 || a._y+a._height > ch){
if(a._y+a._height > ch)
a._y = ch - a._height;
else
a._y = 0;
a._speed_y = -a._speed_y;
}
var fobj = a.G(a._floatID_beforeStr+a._index);
a._x = a._x + a._speed_x;
a._y = a._y + a._speed_y;
fobj.style.left = (a._x + a._doc.scrollLeft) + "px";
fobj.style.top = (a._y+ + a._doc.scrollTop) + "px";
}
};
FloatImg.prototype.stop=
function (){
window.clearInterval(this._interval_flag);
};
使用方法比较简单(注释部分有说明),源文件下载(8.97KB,包含示例):
http://download.csdn.net/source/394701
// 功能:在网页中浮动显示指定的图像
// 作者:yangwengang@hotmail.com (刚)
// 日期:2008-3-20
// 转载请保留本注释部分
//
// ***************************************************************************
// **承接ASP.NET开发外包**网站优化**网站架构规划**技术支持**3+年实际开发经验**
// **Email:yangwengang@hotmail.com**所在地:青岛**
// ***************************************************************************
//
// 使用方法:
// 1.在网页中添加此JS脚本文件的引用,如:<script type="text/javascript" src="floatimg.js"></script>
// 2.只需在网页加载(或单击按扭后)时调用FloatImg对象的show方法即可,如:
// <script type="text/javascript"><!--//
// var f1 = new FloatImg();//创建新的FloatImg对象
// f1.show("f1","test1.jpg","http://www.caraq.com",137,81,1,0);
// var f2 = new FloatImg();//创建新的FloatImg对象
// f2.show("f2","test2.jpg","http://www.caraq.com",132,109,0.5,1);
// --></script>
// show 方法的参数依次为:FloatImg对象名,图像地址,点击图像转向URL,图像宽度,图像高度,移动速度(默认1),图像索引(当页面中需要显示多个浮动图像时分别指定不同的数值,如:0,1,2..)
//
var FloatImg = function() {
this._interval_time= 10;//每次移动图像的间隔时间(ms),越小越平滑速度越慢,可适当修改为:10,20,30...
//以下内容若不了解请不要修改
this._x = 0;
this._y = 0;
this._interval_flag = 0;
this._speed_x = 1;
this._speed_y = 1;
this._floatID_beforeStr = "floatimg";
this._width = 0;
this._height = 0;
this._index = 0;
this._objName = "";
this._doc = document.documentElement;
}
FloatImg.prototype.show =
function(objname,img,clicktourl,width,height,speed,index){
this._speed_x = speed;
this._speed_y = speed;
this._width = width;
this._height = height;
this._index = index;
var cw = this._doc.clientWidth-10;
var ch = this._doc.clientHeight-10;
this._x = cw/(index+1);
this._y = ch/(index+1);
this._objName = objname;
if(!this.G(this._floatID_beforeStr+this._index)){
document.body.innerHTML += "<div onmouseover=""+this._objName+".stop();" onmouseout=""+this._objName+".start();" id=""+this._floatID_beforeStr+index+"" style="position:absolute;z-index:10000;left:0px;top:0px;width:"+width+"px;height:"+height+"px;overflow:hidden;"><a href=""+clicktourl+"" target=_blank><img src=""+img+"" border=0/></a></div>";
}else{
this.G(this._floatID_beforeStr+index).innerHTML = "<a href=""+clicktourl+"" target=_blank><img src=""+img+"" border=0/></a>";
}
this.start();
};
FloatImg.prototype.start=
function(){
this._interval_flag = window.setInterval("FloatImg.interval("+this._objName+")",this._interval_time);
};
FloatImg.prototype.G=
function(v){ return document.getElementById(v);};
FloatImg.interval =
function(a){
var cw = a._doc.clientWidth-10;
var ch = a._doc.clientHeight-10;
if(a.G(a._floatID_beforeStr+a._index)){
if(a._x < 0 || a._x+a._width > cw){
if(a._x + a._width > cw)
a._x = cw - a._width;
else
a._x = 0;
a._speed_x = -a._speed_x;
}
if(a._y < 0 || a._y+a._height > ch){
if(a._y+a._height > ch)
a._y = ch - a._height;
else
a._y = 0;
a._speed_y = -a._speed_y;
}
var fobj = a.G(a._floatID_beforeStr+a._index);
a._x = a._x + a._speed_x;
a._y = a._y + a._speed_y;
fobj.style.left = (a._x + a._doc.scrollLeft) + "px";
fobj.style.top = (a._y+ + a._doc.scrollTop) + "px";
}
};
FloatImg.prototype.stop=
function (){
window.clearInterval(this._interval_flag);
};
使用方法比较简单(注释部分有说明),源文件下载(8.97KB,包含示例):
http://download.csdn.net/source/394701
相关文章推荐
- js城市选择控件,可以同一页面同时存在多个控件!~~完全不冲突。。。调用超简单
- 自定义JSP标签实现语言国际化(类似struts text标签),并同时支持图片、JS文件国际化
- 教你用js超简单实现页面自动控制图片的宽度和高度
- js实现的简单图片浮动效果完整实例
- 教你用js超简单实现页面自动控制图片的宽度和高度
- 页面图片浮动左右滑动效果的简单实现案例
- 页面图片浮动左右滑动效果的简单实现案例
- C#.Net MVC 前台页面Js调用FileReader实现图片预览
- js实现同一页面可多次调用的图片幻灯切换效果
- js实现同一页面可多次调用的图片幻灯切换效果
- js最简单幻灯片切换支持同一页面多次调用
- 分享一个自己利用javascript中的window.setInterval()定时器实现页面背景图片变换的特效(同时淡入淡出效果)
- JS+AS实现真正页面加载图片进度条(带百分比)
- JavaScript实现简单图片滚动附源码下载
- 使用JQuery 加载页面时调用JS的实现方法
- 使用jsonp跨域调用百度js实现搜索框智能提示,并实现鼠标和键盘对弹出框里候选词的操作【附源码和在线测试地址】
- js实现的页面加载完毕之前loading提示效果完整示例【附demo源码下载】
- 利用线性布局和相对布局实现一个简单的页面并且使应用支持国际化语言
- 简单实现js页面切换功能
- jquery textSearch实现页面搜索 注意!!!!调用这个插件后,js事件绑定如,on、bind、live delegate全部失效,折腾了我一整天!!!