[轉載]JavaScript 圖片滑動轉場效果

來源:互聯網
上載者:User

在網上看到很多用JavaScript寫的圖片播放器,有很多寫不僅寫的代碼多,而且還有依賴檔案,譬如XML什麼的。有的是用Flash來實現的,這樣最大的缺陷就是瀏覽器必須要裝上Flash外掛程式,感覺不怎麼好,就算現在的瀏覽器一般都有Flash外掛程式的,整個代碼也對Flash產生依賴,很不好。今天看到的這個還真的很不錯,高手!學習中。。。在此感謝 “【cloudgamer】——腳步無法到達的地方,目光可以抵達;目光無法到達的地方,夢想可以抵達”(CSDN帳號cloudgamer)。

文章來自:http://www.cnblogs.com/cloudgamer/archive/2008/07/06/SlideTrans.html

預覽效果也見上面連結。

下載測試代碼

其他圖片展示效果:
JavaScript 圖片變換效果(ie only)
JavaScript 圖片滑動展示效果

程式說明

原理就是通過不斷設定滑動對象的left(水平切換)和top(垂直切換)來實現圖片切換的動態效果。

首先需要一個容器,程式會自動化佈建容器overflow為hidden,如果不是相對或絕對位置會同時設定position為relative,
滑動對象會設定為絕對位置:

var p = CurrentStyle(this._container).position;
p == "relative" || p == "absolute" || (this._container.style.position = "relative");
this._container.style.overflow = "hidden";
this._slider.style.position = "absolute";

如果沒有設定Change切換參數屬性,會自動根據滑動對象擷取:

this.Change = this.options.Change ? this.options.Change :
    this._slider[bVertical ? "offsetHeight" : "offsetWidth"] / this._count;

執行Run方法就會開始進入切換,其中有一個選擇性參數用來重新設定要切換的圖片索引:

index == undefined && (index = this.Index);
index < 0 && (index = this._count - 1) || index >= this._count && (index = 0);

== undefined && (index = this.Index);
index < 0 && (index = this._count - 1) || index >= this._count && (index = 0);

之後就到設定使用tween緩動時需要的參數了,
包括_target(目標值)、_t(時間)、_b(初始值)和_c(變化量):

{
function onclick()
{
this.style.display='none'; document.getElementById('Code_Closed_Text_213840').style.display='none'; document.getElementById('Code_Open_Image_213840').style.display='inline'; document.getElementById('Code_Open_Text_213840').style.display='inline';
}
}" src="Images/OutliningIndicators/ContractedBlock.gif" alt="" width="11" height="16" align="top">{
function onclick()
{
this.style.display='none'; document.getElementById('Code_Open_Text_213840').style.display='none'; getElementById('Code_Closed_Image_213840').style.display='inline'; getElementById('Code_Closed_Text_213840').style.display='inline';
}
}" src="Images/OutliningIndicators/ExpandedBlockStart.gif" alt="" width="11" height="16" align="top">Code
this._target = -Math.abs(this.Change) * (this.Index = index);
this._t = 0;
this._b = parseInt(CurrentStyle(this._slider)[this.options.Vertical ? "top" : "left"]);
this._c = this._target - this._b;

還有Duration(期間)是自訂屬性。

參數設定好後就執行Move程式開始移動了。
裡面很簡單,首先判斷_c是否有值(等於0表示不需要移動)和_t是否到達Duration,
未滿足條件就繼續移動,否則直接移動到目標值並進行下一次切換:

{
function onclick()
{
this.style.display='none'; document.getElementById('Code_Closed_Text_214256').style.display='none'; document.getElementById('Code_Open_Image_214256').style.display='inline'; document.getElementById('Code_Open_Text_214256').style.display='inline';
}
}" src="Images/OutliningIndicators/ContractedBlock.gif" alt="" width="11" height="16" align="top">{
function onclick()
{
this.style.display='none'; document.getElementById('Code_Open_Text_214256').style.display='none'; getElementById('Code_Closed_Image_214256').style.display='inline'; getElementById('Code_Closed_Text_214256').style.display='inline';
}
}" src="Images/OutliningIndicators/ExpandedBlockStart.gif" alt="" width="11" height="16" align="top">Code
if (this._c && this._t < this.Duration) {
    this.MoveTo(Math.round(this.Tween(this._t++, this._b, this._c, this.Duration)));
    this._timer = setTimeout(Bind(this, this.Move), this.Time);
}else{
    this.MoveTo(this._target);
    this.Auto && (this._timer = setTimeout(Bind(this, this.Next), this.Pause));
}

 

使用說明

執行個體化需要3個參數,分別是容器物件,滑動對象和切換數量,之後可以直接執行Run方法運行:

new SlideTrans("idContainer", "idSlider", 3).Run();

還有以下可選屬性:
Vertical: true,//是否垂直方向(方向不能改)
Auto:  true,//是否自動
Change:  0,//改變數
Duration: 50,//滑動期間
Time:  10,//滑動延時
Pause:  2000,//停頓時間(Auto為true時有效)
onStart: function(){},//開始轉換時執行
onFinish: function(){},//完成轉換時執行
Tween:  Tween.Quart.easeOut//tween運算元

其中Vertical初始化後就不能修改,Tween運算元可參照這裡的緩動效果選擇(執行個體中選了其中3個)。

還有提供了以下方法:
Next: 切換下一個
Previous: 切換上一個
Stop: 停止自動切換
還有上面說到的Run

 

核心代碼如下:

var SlideTrans = function(container, slider, count, options) {<br /> this._slider = $(slider);<br /> this._container = $(container);//容器物件<br /> this._timer = null;//定時器<br /> this._count = Math.abs(count);//切換數量<br /> this._target = 0;//目標值<br /> this._t = this._b = this._c = 0;//tween參數</p><p> this.Index = 0;//當前索引</p><p> this.SetOptions(options);</p><p> this.Auto = !!this.options.Auto;<br /> this.Duration = Math.abs(this.options.Duration);<br /> this.Time = Math.abs(this.options.Time);<br /> this.Pause = Math.abs(this.options.Pause);<br /> this.Tween = this.options.Tween;<br /> this.onStart = this.options.onStart;<br /> this.onFinish = this.options.onFinish;</p><p> var bVertical = !!this.options.Vertical;<br /> this._css = bVertical ? "top" : "left";//方向</p><p> //樣式設定<br /> var p = CurrentStyle(this._container).position;<br /> p == "relative" || p == "absolute" || (this._container.style.position = "relative");<br /> this._container.style.overflow = "hidden";<br /> this._slider.style.position = "absolute";</p><p> this.Change = this.options.Change ? this.options.Change :<br /> this._slider[bVertical ? "offsetHeight" : "offsetWidth"] / this._count;<br />};<br />SlideTrans.prototype = {<br /> //設定預設屬性<br /> SetOptions: function(options) {<br /> this.options = {//預設值<br /> Vertical: true,//是否垂直方向(方向不能改)<br /> Auto: true,//是否自動<br /> Change: 0,//改變數<br /> Duration: 50,//滑動期間<br /> Time: 10,//滑動延時<br /> Pause: 2000,//停頓時間(Auto為true時有效)<br /> onStart: function(){},//開始轉換時執行<br /> onFinish: function(){},//完成轉換時執行<br /> Tween: Tween.Quart.easeOut//tween運算元<br /> };<br /> Extend(this.options, options || {});<br /> },<br /> //開始切換<br /> Run: function(index) {<br /> //修正index<br /> index == undefined && (index = this.Index);<br /> index < 0 && (index = this._count - 1) || index >= this._count && (index = 0);<br /> //設定參數<br /> this._target = -Math.abs(this.Change) * (this.Index = index);<br /> this._t = 0;<br /> this._b = parseInt(CurrentStyle(this._slider)[this.options.Vertical ? "top" : "left"]);<br /> this._c = this._target - this._b;</p><p> this.onStart();<br /> this.Move();<br /> },<br /> //移動<br /> Move: function() {<br /> clearTimeout(this._timer);<br /> //未到達目標繼續移動否則進行下一次滑動<br /> if (this._c && this._t < this.Duration) {<br /> this.MoveTo(Math.round(this.Tween(this._t++, this._b, this._c, this.Duration)));<br /> this._timer = setTimeout(Bind(this, this.Move), this.Time);<br /> }else{<br /> this.MoveTo(this._target);<br /> this.Auto && (this._timer = setTimeout(Bind(this, this.Next), this.Pause));<br /> }<br /> },<br /> //移動到<br /> MoveTo: function(i) {<br /> this._slider.style[this._css] = i + "px";<br /> },<br /> //下一個<br /> Next: function() {<br /> this.Run(++this.Index);<br /> },<br /> //上一個<br /> Previous: function() {<br /> this.Run(--this.Index);<br /> },<br /> //停止<br /> Stop: function() {<br /> clearTimeout(this._timer); this.MoveTo(this._target);<br /> }<br />};

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.