Picture Viewer LibraryJavaScript
2011-10-14
gaogao_9
画像ビューアを実装するためのライブラリです。IE8、IE9、Firefox、Opera、GoogleChromeで動作確認済み。 使い方は<a href="フル画像URL" viewer="true"><img src="サムネ画像URL"></a>とするだけです。
Copy code
- (function(){
- var getLargestZIndex = function(){
- var largestZIndex = 0;
- var defaultView = document.defaultView;
- var func = function(tagname){
- var elems = document.getElementsByTagName(tagname), len=elems.length;
- for(var i=0; i<len; ++i){
- var elem = elems[i];
- var zIndex = elem.style.zIndex;
- if (!zIndex) {
- var css = elem.currentStyle || defaultView.getComputedStyle(elem,null);
- zIndex = css ? css.zIndex : 0;
- }
- zIndex -= 0;
- if(largestZIndex < zIndex) largestZIndex=zIndex;
- }
- };
- if(arguments.length == 0) func('*');
- else for(var i=0; i<arguments.length; ++i) func(arguments[i]);
- return largestZIndex;
- };
- jQuery(function($){
- var ImageCentering = function(){
- var ImageHeight = $("div[id='ViewerWindowForPicture']>div:eq(0)>div").height() + 19;
- var ImageWidth = $("div[id='ViewerWindowForPicture']>div:eq(0)>div").width() + 19;
- var WindowHeight = $(window).height();
- var WindowWidth = $(window).width();
- var BlockHeight = (WindowHeight - ImageHeight) / 2;
- var BlockWidth = (WindowWidth - ImageWidth) / 2;
- var BlockHeightRate = Math.floor((BlockHeight / WindowHeight) * 1000)/10 > 10
- ? Math.floor((BlockHeight / WindowHeight) * 1000)/10
- : 10;
- var BlockWidthRate = Math.floor((BlockWidth / WindowWidth) * 1000)/10 > 10
- ? Math.floor((BlockWidth / WindowWidth) * 1000)/10
- : 10;
- $("div[id='ViewerWindowForPicture']>div:eq(0)").css({
- "top" : BlockHeightRate + "%",
- "bottom" : BlockHeightRate + "%",
- "left" : BlockWidthRate + "%",
- "right" : BlockWidthRate + "%"
- });
- };
- var CreateViewerWindow = function(){
- $("body:first").prepend(
- "<div id='ViewerWindowForPicture'></div>"
- );
- var CreateBlackWindow = function(){
- $("div[id='ViewerWindowForPicture']").css({
- "display" : "none",
- "background-color" : "rgba(0, 0, 0, 0.5)",
- "filter" : "progid:DXImageTransform.Microsoft.Gradient(GradientType=0,StartColorStr=#7f000000,EndColorStr=#7f000000)",
- "position" : "fixed",
- "top" : "0px",
- "bottom" : "0px",
- "left" : "0px",
- "right" : "0px"
- });
- }();
- var CreateImageWindow = function(){
- $("div[id='ViewerWindowForPicture']").append("<div></div>");
- $("div[id='ViewerWindowForPicture']>div:eq(0)").append("<div></div>");
- $("div[id='ViewerWindowForPicture']>div:eq(0)").css({
- "overflow" : "auto",
- "position" : "fixed",
- "top" : "10%",
- "bottom" : "10%",
- "left" : "10%",
- "right" : "10%"
- });
- $("div[id='ViewerWindowForPicture']>div:eq(0)>div").css({
- "display" : "table",
- "overflow" : "auto"
- });
- $("div[id='ViewerWindowForPicture']>div:eq(0)>div>img").css({
- "position" : "relative"
- });
- }();
- var CreateCloseButton = function(){
- $("div[id='ViewerWindowForPicture']").append(
- "<div><input type='button' value='Close'></div>"
- );
- $("div[id='ViewerWindowForPicture']>div:eq(1)").css({
- "position" : "fixed",
- "top" : "0%",
- "right" : "0%"
- });
- }();
- }();
- $("a:has(img)").live("click",function(event){
- if($(this).attr("viewer") === "true"){
- var ImageHref = $(this).attr("href");
- if($("div[id='ViewerWindowForPicture']>div:eq(0)>div:has(img)").length<1){
- $("div[id='ViewerWindowForPicture']>div:eq(0)>div").append("<img>");
- }
- else{
- $("div[id='ViewerWindowForPicture']>div:eq(0)>div>img").attr("src","")
- }
- $("div[id='ViewerWindowForPicture']>div:eq(0)>div>img")
- .attr("src",ImageHref)
- .load(function(){
- $("div[id='ViewerWindowForPicture']")
- .css("z-index",getLargestZIndex()+1)
- .fadeIn("normal");
- ImageCentering();
- });
- return false;
- }
- });
- $("div[id='ViewerWindowForPicture']>div:eq(1)>input").live("click",function(event){
- $("div[id='ViewerWindowForPicture']").fadeOut("normal",function(){
- $(this).css("z-index","-1");
- });
- });
- $(window).bind("resize",ImageCentering);
- });
- })();
Recent comments: ( 0 )
Please loggin to leave a comment!!