Моя статья на Хабре о том, как я писал Rikki's WP Social Icons:
WordPress Plugin длиной в одну страницу
WordPress Plugin длиной в одну страницу
Object.prototype.Inherits = function(parent)
{
if(arguments.length > 1)
parent.apply(this, Array.prototype.slice.call(arguments,1));
else
parent.call(this);
}
Function.prototype.Inherits = function(parent)
{
this.prototype = new parent();
this.prototype.constructor = this;
}
Object.prototype.Inherits = function(parent)
{
if(!parent || typeof(parent.call) != "function")
return;
if(arguments.length > 1)
parent.apply(this, Array.prototype.slice.call(arguments,1));
else
parent.call(this);
}
Function.prototype.Inherits = function(parent)
{
this.prototype = new parent();
this.prototype.constructor = this;
}
getComputedStyle = function( elem, name ) {
var ret, defaultView, computedStyle, width,
style = elem.style;
//Add this:
if(typeof(name) != "string")
return name;
//-----
$.extend({
getUrlVars: function(){
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
var hashesLength = hashes.length;
var i = 0
while(i < hashesLength)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
i++;
}
return vars;
},
UrlVars : null,
getUrlVar : function(key) {
if(!key)
return null;
if(!this.UrlVars)
this.UrlVars = this.getUrlVars();
if(typeof(this.UrlVars[key]) == "undefined")
return null;
else
return this.UrlVars[key];
}
});
function SimpleSlider(width, height, duration){
this.type = "SimpleSlider";
this.width = width;
this.height = height;
this.duration = (duration) ? duration : 1000;
this.images = new Array(); //картиночки
this.imgprefix = 'slider-img-';
this.currimage = 0;
}
function SimpleSliderItem(url, width, height, itemid){
this.type = "SimpleSliderItem";
this.url = url;
this.width = width;
this.height = height;
this.itemid = itemid;
}
SimpleSlider.prototype.addImage = function(url) {
if(this.images.length >= 9999)
return;
//Caching
var Image1= new Image(this.width,this.height);
Image1.src = url;
//Appending
this.images.push(new SimpleSliderItem(url, this.width, this.height, this.images.length));
};
SimpleSlider.prototype.insertImage = function(id) {
if (this.images.length > id){
var imgElem = document.createElement('img');
imgElem.setAttribute('id', this.imgprefix + id);
imgElem.setAttribute('class', 'slider-block');
imgElem.setAttribute('src', this.images[id].url);
imgElem.setAttribute('style', 'width: ' + this.images[id].width + '; height: ' + this.images[id].height + '; z-index: ' + (this.images.length - id) + ';');
return imgElem;
} else {
return null;
}
};
SimpleSlider.prototype.insertImages = function(imgElem) {
imgElem.setAttribute('style', 'width: '+this.width+'; height: ' + this.height + ';');
for(var img in this.images)
{
var elem = this.insertImage(img);
if(elem)
imgElem.appendChild(elem);
}
};
SimpleSlider.prototype.nextImageId = function() {
if(!this.images.length)
return null;
this.currimage++;
if(this.currimage >= this.images.length)
this.currimage = 0;
return "#" + this.imgprefix + this.currimage;
};
SimpleSlider.prototype.currImageId = function() {
if(!this.images.length)
return null;
return "#" + this.imgprefix + this.currimage;
};
SimpleSlider.prototype.changeImage = function() {
var currImage = $(slider.currImageId());
var nextImage = $(slider.nextImageId());
isLastItem = !this.currimage;
if(isLastItem)
currImage.css('z-Index', '9999');
currImage.fadeOut(this.duration, function() {
if(parseInt(currImage.css('z-Index')) == 9999)
currImage.css('z-Index', '1');
});
nextImage.show();
};
var slider = new SimpleSlider(640, 250);
function sliderTimer() {
slider.changeImage();
t=setTimeout("sliderTimer()",1000);
}
$(function(){
slider.addImage("image1.jpg");
slider.addImage("image2.jpg");
slider.addImage("image3.jpg");
slider.insertImages(document.getElementById("slider_simple"));
sliderTimer();
});
Дальше я расскажу о том, как устроен этот скрипт изнутри.