2013-05-15 50 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