技术开发 频道

基于微软ASP.NET AJAX框架开发幻灯片播放网页


三、 创建SlideShow类
    右单击工程添加一个新的java脚本文件,并命名为JScript.js。在此,我们将创建一个称为SlideShow的类,它将负责完成所有的幻灯片操作任务—例如播放、暂住和导航幻灯片。注意,这个SlideShow类的开发是基于ASP.NET AJAX客户端脚本扩展技术,具体实现代码如下所示:
Type.registerNamespace("Demo"); //构造函数及私有变量声明 Demo.SlideShow=function(){ this._slides=new Array(); this._delay=2000; this._currentIndex=0; this._pause=false; } //原型定义部分 Demo.SlideShow.prototype= { get_Slides:function() { return this._slides; }, set_Slides:function(value) { this._slides=value; }, get_Delay:function() { return this._delay; }, set_Delay:function(value) { this._delay=value; }, get_CurrentIndex:function() { return this._currentIndex; }, set_CurrentIndex:function(value) { if(value<0) { this._currentIndex=this._slides.length-1; return; } if(value>=this._slides.length) { this._currentIndex=0; } else{ this._currentIndex=value; } }, get_IsPaused:function() { return this._pause; }, set_IsPaused:function(value) { this.pause=value; }, Pause:function() { this._pause=true; }, Play:function() { this._pause=false; window.setTimeout("slideshow.ShowImage()"this.get_Delay()); }, ShowFirst:function() { this._currentIndex=0; this.ShowImage(); }, ShowLast:function() { this._currentIndex=this._slides.length-1; this.ShowImage(); }, ShowNext:function() { var newIndex=this._currentIndex +1; this.set_CurrentIndex(newIndex); this.ShowImage(); }, ShowPrevious:function() { var newIndex=this._currentIndex -1; this.set_CurrentIndex(newIndex); this.ShowImage(); }, ShowImage:function() { var img=$get("Image1"); if(img.style.visibility=="hidden") { img.style.visibility="visible"; } var slides=this.get_Slides(); var curIndex=this.get_CurrentIndex(); img.src=slides[curIndex]; if(this.get_IsPaused()==false) { this.set_CurrentIndex(curIndex+1); this.Play(); } } } //注册类 Demo.SlideShow.registerClass("Demo.SlideShow"); //创建全局SlideShow类的实例 var slideshow=new Demo.SlideShow();



    在代码的最开始,我们先注册一个称为Demo的新的命名空间。然后,创建一个称为SlideShow的类。该SlideShow类的构造器共声明了四个私有成员变量。其中,_slides变量指向一个包含幻灯片图像URL的数组;_delay变量指示两张相邻的幻灯片播放的间隔时间(单位为毫秒);_currentIndex变量存储了当前幻灯片在_slides数组中的索引值;最后,_pause变量指示幻灯片被暂停(true)还是处于运行态(false)。

    接下来,在SlideShow类的原型中,我们定义了与前面的四个属性相关联的getter/setter方法,也就是Slides、Delay、CurrentIndex和IsPaused。其它方法都比较基本,因此我们仅介绍方法set_CurrentIndex()。这个set_CurrentIndex()属性方法负责检查提供给它的索引值。如果该值超出slides数组上下标边界,那么,它会把这个值调整到0或数组的长度减1(根据具体情况而定)。这是很关键的,这样以来,幻灯片就可以进行循环播放。

    接下来,Pause()方法简单地把成员变量_pause设置为true—这可以控制幻灯片如何暂停。
Play()方法负责播放幻灯片。它首先设置_pause变量为false,然后调用JavaScript对象windows的setTimeout()方法。该setTimeout()方法接受两个参数:在经过特定时间延迟后要执行的代码;在此代码执行完后对应的时间跨度(单位为毫秒)。在本例中,这个延迟值来自于get_Delay()属性。在此,该setTimeout()方法将调用ShowImage()方法。

    ShowImage()方法负责执行显示一个图像的核心工作。它引用了CurrentIndex和Slides两个属性,然后把图像标签的src属性设置为Slides数组中对应的适当的图像。注意,Image1是一个图像标签的ID—我们将在后面添加它。此外,还应注意$get()方法的用法,它等价于document.getElementById()方法。然后,CurrentIndex的值加1并且再次调用Play()方法。这样以来,将形成一个无限循环,而幻灯片将持续不断地播放下去。

最后的四个方法—ShowFirst(),ShowLast(),ShowNext()和ShowPrevious()方法只是简单地调整_currentIndex成员变量的值,并调用ShowImage()方法来显示一张幻灯片。

    在创建类结束后,我们使用registerClass()方法把它注册到MS AJAX框架。最后,声明一个SlideShow类的全局实例变量。
最后打开Web页面Default.aspx,选择ScriptManager控件,并且设置它的EnablePageMethods属性为true,而且还要把JScript.js文件添加到它的脚本集合中。

0
相关文章