lytebox.js
来自「一款基于ajax技术实现的相册源文件代码.」· JavaScript 代码 · 共 693 行 · 第 1/3 页
JS
693 行
//**************************************************************************************************/
// LyteBox v3.10
//
// Author: Markus F. Hay
// Website: http://www.dolem.com/lytebox
// Date: May 28, 2007
// License: Creative Commons Attribution 2.5 License (http://creativecommons.org/licenses/by/2.5/)
//**************************************************************************************************/
Array.prototype.removeDuplicates = function () { for (var i = 1; i < this.length; i++) { if (this[i][0] == this[i-1][0]) { this.splice(i,1); } } }
Array.prototype.empty = function () { for (var i = 0; i <= this.length; i++) { this.shift(); } }
function LyteBox() {
/*** Start Global Configuration ***/
this.theme = 'grey'; // themes: grey (default), red, green, blue, gold
this.hideFlash = true; // controls whether or not Flash objects should be hidden
this.outerBorder = false; // controls whether to show the outer grey (or theme) border
this.resizeSpeed = 8; // controls the speed of the image resizing (1=slowest and 10=fastest)
this.borderSize = 12; // if you adjust the padding in the CSS, you will need to update this variable
this.maxOpacity = 80; // higher opacity = darker overlay, lower opacity = lighter overlay
this.navType = 1; // 1 = "Prev/Next" buttons on top left and left (default), 2 = "<< prev | next >>" links next to image number
this.autoResize = true; // controls whether or not images should be resized if larger than the browser window dimensions
/*** End Global Configuration ***/
/*** Configure Slideshow Options ***/
this.slideInterval = 5000; // Change value (milliseconds) to increase/decrease the time between "slides"
this.showNavigation = true; // true to display Next/Prev buttons/text during slideshow, false to hide
this.showClose = true; // true to display the Close button, false to hide
this.showDetails = true; // true to display image details (caption, count), false to hide
this.showPlayPause = true; // true to display pause/play buttons next to close button, false to hide
this.autoEnd = true; // true to automatically close Lytebox after the last image is reached, false to keep open
this.pauseOnNextClick = false; // true to pause the slideshow when the "Next" button is clicked
this.pauseOnPrevClick = true; // true to pause the slideshow when the "Prev" button is clicked
/*** End Slideshow Configuration ***/
if(this.resizeSpeed > 10) { this.resizeSpeed = 10; }
if(this.resizeSpeed < 1) { resizeSpeed = 1; }
this.resizeDuration = (11 - this.resizeSpeed) * 0.15;
this.resizeWTimerArray = new Array();
this.resizeWTimerCount = 0;
this.resizeHTimerArray = new Array();
this.resizeHTimerCount = 0;
this.showImageTimerArray = new Array();
this.showImageTimerCount = 0;
this.overlayTimerArray = new Array();
this.overlayTimerCount = 0;
this.imageTimerArray = new Array();
this.imageTimerCount = 0;
this.timerIDArray = new Array();
this.timerIDCount = 0;
this.slideshowIDArray = new Array();
this.slideshowIDCount = 0;
this.imageArray = new Array();
this.activeImage = null;
this.slideArray = new Array();
this.activeSlide = null;
this.checkFrame();
this.isSlideshow = false;
/*@cc_on
/*@if (@_jscript)
this.ie = (document.all && !window.opera) ? true : false;
/*@else @*/
this.ie = false;
/*@end
@*/
this.ie7 = (this.ie && window.XMLHttpRequest);
this.initialize();
}
LyteBox.prototype.initialize = function() {
if (!document.getElementsByTagName) { return; }
var anchors = (this.isFrame) ? window.parent.frames[window.name].document.getElementsByTagName('a') : document.getElementsByTagName('a');
for (var i = 0; i < anchors.length; i++) {
var anchor = anchors[i];
var relAttribute = String(anchor.getAttribute('rel'));
if (anchor.getAttribute('href')) {
if (relAttribute.toLowerCase().match('lytebox')) {
anchor.onclick = function () { myLytebox.start(this, false); return false; }
} else if (relAttribute.toLowerCase().match('lyteshow')) {
anchor.onclick = function () { myLytebox.start(this, true); return false; }
}
}
}
var objBody = this.doc.getElementsByTagName("body").item(0);
if (this.doc.getElementById('lbOverlay')) {
objBody.removeChild(this.doc.getElementById("lbOverlay"));
objBody.removeChild(this.doc.getElementById("lbMain"));
}
var objOverlay = this.doc.createElement("div");
objOverlay.setAttribute('id','lbOverlay');
objOverlay.setAttribute((this.ie ? 'className' : 'class'), this.theme);
objOverlay.style.display = 'none';
objBody.appendChild(objOverlay);
var objLytebox = this.doc.createElement("div");
objLytebox.setAttribute('id','lbMain');
objLytebox.style.display = 'none';
objBody.appendChild(objLytebox);
var objOuterImageContainer = this.doc.createElement("div");
objOuterImageContainer.setAttribute('id','lbOuterImageContainer');
objOuterImageContainer.setAttribute((this.ie ? 'className' : 'class'), this.theme);
objLytebox.appendChild(objOuterImageContainer);
var objImageContainer = this.doc.createElement("div");
objImageContainer.setAttribute('id','lbImageContainer');
objOuterImageContainer.appendChild(objImageContainer);
var objLyteboxImage = this.doc.createElement("img");
objLyteboxImage.setAttribute('id','lbImage');
objImageContainer.appendChild(objLyteboxImage);
var objLoading = this.doc.createElement("div");
objLoading.setAttribute('id','lbLoading');
objImageContainer.appendChild(objLoading);
var objImageDataContainer = this.doc.createElement("div");
objImageDataContainer.setAttribute('id','lbImageDataContainer');
objImageDataContainer.setAttribute((this.ie ? 'className' : 'class'), this.theme);
objLytebox.appendChild(objImageDataContainer);
var objImageData =this.doc.createElement("div");
objImageData.setAttribute('id','lbImageData');
objImageData.setAttribute((this.ie ? 'className' : 'class'), this.theme);
objImageDataContainer.appendChild(objImageData);
var objImageDetails = this.doc.createElement("div");
objImageDetails.setAttribute('id','lbImageDetails');
objImageData.appendChild(objImageDetails);
var objCaption = this.doc.createElement("span");
objCaption.setAttribute('id','lbCaption');
objImageDetails.appendChild(objCaption);
var objHoverNav = this.doc.createElement("div");
objHoverNav.setAttribute('id','lbHoverNav');
objImageContainer.appendChild(objHoverNav);
var objBottomNav = this.doc.createElement("div");
objBottomNav.setAttribute('id','lbBottomNav');
objImageData.appendChild(objBottomNav);
var objPrev = this.doc.createElement("a");
objPrev.setAttribute('id','lbPrev');
objPrev.setAttribute((this.ie ? 'className' : 'class'), this.theme);
objPrev.setAttribute('href','#');
objHoverNav.appendChild(objPrev);
var objNext = this.doc.createElement("a");
objNext.setAttribute('id','lbNext');
objNext.setAttribute((this.ie ? 'className' : 'class'), this.theme);
objNext.setAttribute('href','#');
objHoverNav.appendChild(objNext);
var objNumberDisplay = this.doc.createElement("span");
objNumberDisplay.setAttribute('id','lbNumberDisplay');
objImageDetails.appendChild(objNumberDisplay);
if (this.navType == 2) {
var objNavDisplay = this.doc.createElement("span");
objNavDisplay.setAttribute('id','lbNavDisplay');
objImageDetails.appendChild(objNavDisplay);
}
var objClose = this.doc.createElement("a");
objClose.setAttribute('id','lbClose');
objClose.setAttribute((this.ie ? 'className' : 'class'), this.theme);
objClose.setAttribute('href','#');
objBottomNav.appendChild(objClose);
var objPause = this.doc.createElement("a");
objPause.setAttribute('id','lbPause');
objPause.setAttribute((this.ie ? 'className' : 'class'), this.theme);
objPause.setAttribute('href','#');
objPause.style.display = 'none';
objBottomNav.appendChild(objPause);
var objPlay = this.doc.createElement("a");
objPlay.setAttribute('id','lbPlay');
objPlay.setAttribute((this.ie ? 'className' : 'class'), this.theme);
objPlay.setAttribute('href','#');
objPlay.style.display = 'none';
objBottomNav.appendChild(objPlay);
};
LyteBox.prototype.start = function(imageLink, doSlide) {
if (this.ie && !this.ie7) { this.toggleSelects('hide'); }
if (this.hideFlash) { this.toggleFlash('hide'); }
var pageSize = this.getPageSize();
var objOverlay = this.doc.getElementById('lbOverlay');
var objBody = this.doc.getElementsByTagName("body").item(0);
objOverlay.style.height = pageSize[1] + "px";
objOverlay.style.display = '';
this.appear('lbOverlay', 0);
this.imageArray = [];
this.imageNum = 0;
this.slideArray = [];
this.slideNum = 0;
if (!document.getElementsByTagName){ return; }
var anchors = (this.isFrame) ? window.parent.frames[window.name].document.getElementsByTagName('a') : document.getElementsByTagName('a');
if((imageLink.getAttribute('rel') == 'lytebox')) {
this.imageArray.push(new Array(imageLink.getAttribute('href'), imageLink.getAttribute('title')));
} else {
if (imageLink.getAttribute('rel').indexOf('lytebox') != -1) {
for (var i = 0; i < anchors.length; i++) {
var anchor = anchors[i];
if (anchor.getAttribute('href') && (anchor.getAttribute('rel') == imageLink.getAttribute('rel'))) {
this.imageArray.push(new Array(anchor.getAttribute('href'), anchor.getAttribute('title')));
}
}
this.imageArray.removeDuplicates();
while(this.imageArray[this.imageNum][0] != imageLink.getAttribute('href')) { this.imageNum++; }
}
if (imageLink.getAttribute('rel').indexOf('lyteshow') != -1) {
for (var i = 0; i < anchors.length; i++) {
var anchor = anchors[i];
if (anchor.getAttribute('href') && (anchor.getAttribute('rel') == imageLink.getAttribute('rel'))) {
this.slideArray.push(new Array(anchor.getAttribute('href'), anchor.getAttribute('title')));
}
}
this.slideArray.removeDuplicates();
while(this.slideArray[this.slideNum][0] != imageLink.getAttribute('href')) { this.slideNum++; }
}
}
var object = this.doc.getElementById('lbMain');
object.style.top = (this.getPageScroll() + (pageSize[3] / 15)) + "px";
object.style.display = '';
if (!this.outerBorder) {
this.doc.getElementById('lbOuterImageContainer').style.border = 'none';
this.doc.getElementById('lbImageDataContainer').style.border = 'none';
} else {
this.doc.getElementById('lbOuterImageContainer').style.borderBottom = '';
this.doc.getElementById('lbOuterImageContainer').setAttribute((this.ie ? 'className' : 'class'), this.theme);
}
this.doc.getElementById('lbOverlay').onclick = function() { myLytebox.end(); return false; }
this.doc.getElementById('lbClose').onclick = function() { myLytebox.end(); return false; }
this.doc.getElementById('lbPause').onclick = function() { myLytebox.togglePlayPause("lbPause", "lbPlay"); return false; }
this.doc.getElementById('lbPlay').onclick = function() { myLytebox.togglePlayPause("lbPlay", "lbPause"); return false; }
this.isSlideshow = doSlide;
this.isPaused = (this.slideNum != 0 ? true : false);
if (this.isSlideshow && this.showPlayPause && this.isPaused) {
this.doc.getElementById('lbPlay').style.display = '';
this.doc.getElementById('lbPause').style.display = 'none';
}
if (this.isSlideshow) {
this.changeImage(this.slideNum);
} else {
this.changeImage(this.imageNum);
}
};
LyteBox.prototype.changeImage = function(imageNum) {
if (this.isSlideshow) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?