I am turning some jQuery into a plugin but i am coming up with errors. Here is the mormal jQuery code.
var current = ($("#gallery a.show")? $("#gallery a.show") : $("#gallery a:first"));
Here is the jQuery plugin code
var current = ($(this).find("a.show")? $(this).find("a.show") : $(this).find("a:first"));
Help would be much appreciated.
Here is ALL the jQuery plugin code.
$(window).load(function(){
$("#gallery").csstubeslider();
});
$.fn.csstubeslider = function(){
$(this).animate({'opacity': '1'}, 500);
$(this).find(".caption").css("opacity", 0.7);
$(this).find("a").css("opacity", "0");
$(this).find("a.show").css("opacity", "1");
var hasplayed = false;
if(hasplayed == false){
setTimeout(function hello(){
$(this).find(".caption").animate({"height": 0, "opacity": 0}, 500);
hasplayed == true;
}, 4500);
}
setInterval(function(){
var current = ($(this).find("a.show")? $(this).find("a.show") : $(this).find("a:first"));
var next = ((current.next())? (current.next().hasClass("caption"))? $(this).find("a:first") : current.next() : $(this).find("a:first"));
var content = next.find("img").attr("rel");
next.addClass("show").animate({"opacity": "1.0"}, 500);
current.removeClass('show').animate({"opacity": "0"}, 500);
setTimeout(function(){
$(this).find(".caption").animate({"height": 0, "opacity": 0}, 500);
}, 4500);
$(this).find(".content").html(content);
}, 1000);
}
and here is the jQuery code.
$(window).load(function(){
$("#gallery").animate({'opacity': '100'}, 5000);
$("#gallery .caption").css("opacity", 0.8);
slideshow();
});
function slideshow(){
$("#gallery a").css("opacity", "0");
$("#gallery a.show").css("opacity", "1");
var content = $("#gallery a.show").find("img").attr("rel");
$("#gallery .content").html(content);
var hasplayed = false;
if(hasplayed == false){
setTimeout(function hello(){
$("#gallery .caption").animate({"height": 0, "opacity": 0}, 500);
hasplayed == true;
}, 4500);
}
setInterval("playimages()", 5500);
}
function playimages(){
var current = ($("#gallery a.show")? $("#gallery a.show") : $("#gallery a:first"));
var next = ((current.next())? (current.next().hasClass("caption"))? $("#gallery a:first") : current.next() : $("#gallery a:first"));
var content = next.find("img").attr("rel");
next.addClass("show").animate({"opacity": "1.0"}, 500);
current.removeClass('show').animate({"opacity": "0"}, 2000);
$("#gallery .caption").css("height", 0).animate({"height": 60, "opacity": 0.8}, 500);
setTimeout(function hello(){
$("#gallery .caption").animate({"height": 0, "opacity": 0}, 500);
}, 4500);
$("#gallery .content").html(content);
}
This doesn’t do what you expect it to:
That is equivalent to
var current = $('#gallery a.show');since$(x)will never be false, it might have length of zero but it won’t be false. That means that your plugin doesn’t do what you expect either, you want to check the length:Or better:
Your next problem is that
thisisn’t what you expect it to be inside thesetIntervalandsetTimeoutcallbacks,thiswill probably bewindowwhen the callback is triggered. You want to do something like this:The above also applies to your
setIntervalcalls.Furthermore, inside your plugin,
thisis already a jQuery object so you don’t need to$(this), justthiswill do. And your plugin isn’t chainable but you can fix that with a simplereturn this;:You might run into trouble if you try to bind your plugin to multiple things at once, the usual pattern is this: