最近看了很多人都在使用javascript开发
自己写一个简单的类库吧
足足花了一个星期累了
实验下 体验下面向对象吧
- document.body.onload = main;
- function main(){
- var imgmove = new ImageMove();
- imgmove.start(advertPath,advertStep,advertTimer);
- var leftimg = new LeftImage();
- leftimg.start(leftadvertPath,leftadvertTimer,leftadvertTop);
- }
- /*
- 图片移动
- */
- function ImageMove(){
- /*
- x坐标是否正向移动
- */
- var xon = true;
- /*
- y坐标是否正向移动
- */
- var yon = true;
- /*
- 步长
- */
- var step;
- /*
- 图片对象
- */
- var img = new image();
- /*
- 图片的x坐标
- */
- var x = img.getX();
- /*
- 图片的y坐标
- */
- var y = img.getY();
- /*
- 时间器回调方法
- */
- this.run = function(){
- /*
- 最大宽度
- */
- var maxWidth =document.body.clientWidth-img.getWidth();
- /*
- 最大高度
- */
- var maxHeight = document.body.clientHeight-img.getHeight();
- /*
- x坐标的移动
- */
- if(xon){
- x = x+step;
- img.setX(x+document.body.scrollLeft);
- if(x>=maxWidth){
- xon = false;
- }
- }
- else{
- x = x-step;
- img.setX(x+document.body.scrollLeft);
- if(x<=0){
- xon = true;
- }
- }
- /*
- y坐标的移动
- */
- if(yon){
- y = y+step;
- img.setY(y+document.body.scrollTop);
- if(y>=maxHeight){
- yon = false;
- }
- }
- else{
- y = y-step;
- img.setY(y+document.body.scrollTop);
- if(y<=0){
- yon = true;
- }
- }
- }
- /*
- 启动图片移动的方法
- @param imgPath 图片路径
- @param var_step 步长
- @param advert_timer 时间
- */
- this.start = function(imgPath,var_step,advert_timer){
- step = var_step;
- img.isAbsolute(true);
- img.setUrl(imgPath);
- WebContainer.add(img);
- timer = new Timer(this,advert_timer);
- timer.start();
- }
- }
- /*
- 左侧广告图片
- @param 图片路径
- */
- function LeftImage(){
- /*
- 层对象
- */
- var leftimagepanel = new AbsolutePanel();
- /*
- 关闭文字对象
- */
- var textpanel = new AbsolutePanel();
- /*
- 图片对象
- */
- var img = new image();
- /*
- top坐标
- */
- var imgtop = 0;
- /*
- 时间器对象
- */
- var timer;
- /*
- 时间器回调方法
- */
- this.run = function(){
- /*
- y坐标的移动
- */
- y = .1*(document.body.scrollTop-imgtop+leftadvertTop);
- if(y>0){
- y = Math.ceil(y);
- }
- else{
- y = Math.floor(y);
- }
- leftimagepanel.setY(leftimagepanel.getY()+y);
- imgtop += y;
- }
- /*
- 启动图片
- @param imgpath 图片路径
- @param top 图片固定在窗口的y 坐标
- */
- this.start = function(imgpath,var_timer,top){
- leftimagepanel.setY(top);
- imgtop = top;
- img.setUrl(imgpath);
- textpanel.setText("关闭");
- textpanel.addStyle("cursor","pointer");
- textpanel.addActionListener(function(){
- timer.clear();
- WebContainer.remove(leftimagepanel);
- });
- textpanel.setX(10);
- textpanel.setX(leftimagepanel.getWidth()-textpanel.getWidth());
- leftimagepanel.setAlign("right");
- leftimagepanel.add(img);
- leftimagepanel.add(textpanel);
- leftimagepanel.addStyle("background-attachment","fixed");
- WebContainer.add(leftimagepanel);
- timer = new Timer(this,var_timer);
- timer.start();
- }
- }