2013-05-15 51 views
1

於是我就問一個問題,前一段時間在這裏>Cons of MouseOver for webpages跑進與啓用/禁用事件的一些問題。根據帖子的回答,我應該更新我的功能作爲一個對象輕鬆地發出。試錯的幾個小時以及網上調研後不過,我還是不明白,對象是如何工作的更改JS函數對象

所以這就是我要投入到一個對象的功能,

$(function() { 
    $('#01 img:gt(0)').hide(); 
    setInterval(function() { 
     $('#01 :first-child').fadeOut(1500) 
      .next('img').fadeIn(1500) 
      .end().appendTo('#01'); 
    }, 3000); 
}); 

而且這是提供初始化我的對象的代碼,

var Slideshow = (function() { 
    this.interval; 

    this.start = function() { 
     ... 
     initialize 
     ... 
     // catch the interval ID so you can stop it later on 
     this.interval = window.setInterval(this.next, 3000); 
    }; 

    this.next = function() { 
     /* 
     * You cannot refer to the keyword this in this function 
     * since it gets executed outside the object's context. 
     */ 
     ... 
     your logic 
     ... 
    }; 

    this.stop = function() { 
     window.clearInterval(this.interval); 
    }; 
})(); 

那麼究竟應該如何實現我的功能到對象,以便它的工作?

+0

那麼你是怎麼其實嘗試?問題到底是什麼?可以是「*」但是我需要引用'this',我可以做什麼*「over」的任何內容*在該答案中提供的代碼如何工作*「to」*如何將第一個代碼片段中的代碼放入'next'功能*「...... – Bergi

+0

@Bergi我試圖將函數到對象代碼,但是我想我可能已經將它們放置在錯誤的地區或我應該把我的功能到對象時,放棄我的函數的某些部分。基本上我失去了我應該在哪裏提供我的功能。 – Damienn

+0

那麼請告訴我們什麼* *您已經嘗試... – Bergi

回答

1

我想構建這樣的:

function Slideshow(container) { 
    this.interval = undefined; 

    this.start = function() { 
     container.find("img").first().show(); 
     container.find("img:gt(0)").hide(); 
     this.interval = window.setInterval(this.next, 3000); 
    }; 

    this.next = function() { 
     var first = container.find(":first-child"); 
     first.fadeOut(1500, function() { 
      first.next("img").fadeIn(1500); 
      first.appendTo(container); 
     }); 
    }; 

    this.stop = function() { 
     window.clearInterval(this.interval); 
    }; 
} 

$(function() { 
    var div = $("#div01"); 
    var slides = new Slideshow(div); 

    div.hover(function() { 
     slides.stop(); 
    }, function() { 
     slides.start(); 
    }); 
    slides.start(); 
}); 

DEMO:http://jsfiddle.net/STcvq/5/

@Bergi的

最新版本禮貌

+0

@Bergi哈哈我在想什麼?我不知道爲什麼我把它保留爲一個返回函數的IIFE ... – Ian

+0

我已經爲構造函數移除了絕對不需要的模塊IEFE :-)如果您現在編輯它使其停止在「hover」上的點擊(作爲OP的期望)我會給它一個upvote :-) – Bergi

+0

@Bergi我*認爲*這樣做? – Ian

0

你應該做的目的,望着建議代碼,是將你的setInterval的Slideshow.next()函數中的邏輯。這基本上涵蓋了你的淡出,淡入淡出的邏輯。

所以你的函數看起來是這樣的:在世界上最簡單的

this.next = function() { 
    $('#01 :first-child').fadeOut(1500) 
     .next('img').fadeIn(1500) 
     .end().appendTo('#01'); 
}; 

理想情況下,你會想要告訴它實例化幻燈片它應該使用的ID,通過傳遞在構造函數中。也就是說,你應該能夠調用

新的幻燈片(「#01」),以及新的幻燈片(「#02」),這樣就可以真正重用。

然後,你的下一個功能將變爲看起來像(假設ID存儲在this.elementId):

this.next = function() { 
    $(this.elementId + ':first-child').fadeOut(1500) 
     .next('img').fadeIn(1500) 
     .end().appendTo('#01'); 
}; 

希望這有助於

0

改變語法:

var Slideshow = (function() { 
return { 
    interval:null, 
    start : function() { 
     // catch the interval ID so you can stop it later on 
     this.interval = window.setInterval(this.next, 3000); 
    }, 

    next: function() { 

    }, 

    stop : function() { 
     window.clearInterval(this.interval); 
    } 
}; 
})(); 

因爲你是使用jQuery,更好地答是創建一個小插件: http://learn.jquery.com/plugins/basic-plugin-creation/

+0

只需刪除不必要的模塊IEFE! – Bergi

+0

在這種情況下,是的,它是無用的。 但是,如果@Damienn想要使用IIFE,它是一種替代方法 – Horst