|
/* |
|
* jQuery_huwahuwaTumhuwrImage.js |
|
* |
|
* Varsion: 0.1.0 |
|
* PublishDate: 2014-07-07 02:55 |
|
* LastUpdate : 2014-07-30 23:45 |
|
* Copyright (c) 2014 ethertank.jp |
|
* Licensed under the MIT |
|
* |
|
* jQuery required (tested on 1.8.3) |
|
* |
|
*/ |
|
(function ($, undefined) { |
|
$.fn.huwahuwaTumhuwrImage = function (config) { |
|
if( |
|
(config === undefined) || |
|
((config.username === undefined) && (config.domain === undefined)) |
|
) { |
|
alert("huwahuwaTumhuwrImage: 'username' or 'domain' required."); |
|
return false; |
|
} |
|
|
|
config = $.extend({ |
|
start: "0", |
|
num: "10", |
|
interval: "6000", |
|
speed: "slow" |
|
}, config); |
|
|
|
var jsonURL, |
|
jsonParam = "", |
|
$wraps = $(this), |
|
interval = config.interval, |
|
speed = config.speed, |
|
htmlEsc = function htmlEsc(str) { |
|
return $('<div />').text( str ).html(); |
|
}, |
|
getNaturalHeight = (function() { |
|
var img = document.createElement("img"); |
|
|
|
if ("naturalHeight" in img) { |
|
return function (imgElm) { |
|
return imgElm.naturalHeight; |
|
}; |
|
} else { |
|
return function getNaturalHeight (imgElm) { |
|
var img = new Image(); |
|
img.src = imgElm.src; |
|
return img.height; |
|
}; |
|
} |
|
})(); |
|
|
|
jsonURL = config.username ? |
|
"http:\/\/" + config.username + ".tumblr.com\/api\/read\/json?type=photo": |
|
"http:\/\/" + config.domain + "/api\/read\/json?type=photo"; |
|
|
|
delete config.username; |
|
delete config.domain; |
|
delete config.interval; |
|
delete config.speed; |
|
|
|
$(config).each(function (i) { |
|
jsonParam += $.param(this); |
|
}); |
|
|
|
if (jsonParam.length) { |
|
jsonURL += "&" + jsonParam; |
|
} |
|
|
|
$.ajax({ |
|
url: jsonURL, |
|
dataType: 'jsonp', |
|
timeout: 5000 |
|
}).success(function (jsonData) { |
|
parse(jsonData); |
|
}).error(function (jqXHR, textStatus, errorThrown) { |
|
wraps.html("<p><small>error</small></p>"); |
|
}); |
|
|
|
function parse (j) { |
|
$.each(j.posts, function (i, p) { |
|
$wraps.each(function () { |
|
var $wrap = $(this); |
|
var wrapHeight = 0; |
|
|
|
$wrap.hide().append( |
|
$("<a>").attr({ |
|
href: htmlEsc(p.url) |
|
}).css({ |
|
display: "inline-block", |
|
willChange: "opacity" |
|
}).append( |
|
$("<img>").attr({ |
|
src: htmlEsc( p["photo-url-250"] ) |
|
}) |
|
) |
|
); |
|
|
|
$wrap.find("img").on("load", function() { |
|
$(this).each(function () { |
|
var $anchor = $(this).parent(), |
|
anchorHeight = $anchor.outerHeight(); |
|
|
|
if(anchorHeight > wrapHeight) { |
|
wrapHeight = anchorHeight; |
|
} |
|
|
|
$wrap.css( "height", wrapHeight + "px" ); |
|
}); |
|
}); |
|
});// End of $wrap.each() |
|
}); // End of $.each() |
|
|
|
initSlideShow(); |
|
} |
|
|
|
|
|
function initSlideShow () { |
|
$wraps.each(function () { |
|
var $wrap = $(this), |
|
timer; |
|
|
|
function slideShow () { |
|
var a = $wrap.find("a"), |
|
f = a.eq(0), |
|
s = a.eq(1); |
|
|
|
f.fadeOut(speed, function () { |
|
$wrap.append(f); |
|
s.fadeIn(speed); |
|
}); |
|
} |
|
|
|
function stopTimer () { |
|
clearInterval( timer ); |
|
} |
|
|
|
function startTimer () { |
|
timer = setInterval( slideShow, interval ); |
|
} |
|
|
|
$wrap.find("a").not(":first-child").hide(); |
|
$wrap.show(); |
|
$wrap.find("a").hover(stopTimer, startTimer); |
|
|
|
setTimeout( startTimer, interval ); |
|
}); |
|
} |
|
|
|
return this; |
|
}; // End of $.fn.huwahuwaTumhuwrImage = function (config) {}; |
|
})(jQuery); |