Lightbox JS is a simple, unobtrusive script used to to overlay images on the current page. It's a snap to setup and works on all modern browsers.
http://www.huddletogether.com/projects/lightbox/
<script type="text/javascript">
/*
Lightbox JS: Fullsize Image Overlays
by Lokesh Dhakar - http://www.huddletogether.com
For more information on this script, visit:
http://huddletogether.com/projects/lightbox/
Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/
(basically, do anything you want, just leave my name and link)
Table of Contents
-----------------
Configuration
Functions
- getPageScroll()
- getPageSize()
- pause()
- getKey()
- listenKey()
- showLightbox()
- hideLightbox()
- initLightbox()
- addLoadEvent()
Function Calls
- addLoadEvent(initLightbox)
*/
//
// Configuration
//
// If you would like to use a custom loading image or close button reference them in the next two lines.
var loadingImage = 'http://www.huddletogether.com/projects/lightbox/loading.gif';
var closeButton = 'http://www.huddletogether.com/projects/lightbox/close.gif';
//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){
var yScroll;
if (self.pageYOffset) {
yScroll = self.pageYOffset;
} else if (document.documentElement && document.documentElement.scrollTop){ // Explorer 6 Strict
yScroll = document.documentElement.scrollTop;
} else if (document.body) {// all other Explorers
yScroll = document.body.scrollTop;
}
arrayPageScroll = new Array('',yScroll)
return arrayPageScroll;
}
//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
var xScroll, yScroll;
if (window.innerHeight && window.scrollMaxY) {
xScroll = document.body.scrollWidth;
yScroll = window.innerHeight + window.scrollMaxY;
} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
xScroll = document.body.scrollWidth;
yScroll = document.body.scrollHeight;
} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
xScroll = document.body.offsetWidth;
yScroll = document.body.offsetHeight;
}
var windowWidth, windowHeight;
if (self.innerHeight) { // all except Explorer
windowWidth = self.innerWidth;
windowHeight = self.innerHeight;
} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
windowWidth = document.documentElement.clientWidth;
windowHeight = document.documentElement.clientHeight;
} else if (document.body) { // other Explorers
windowWidth = document.body.clientWidth;
windowHeight = document.body.clientHeight;
}
// for small pages with total height less then height of the viewport
if(yScroll < windowHeight){
pageHeight = windowHeight;
} else {
pageHeight = yScroll;
}
// for small pages with total width less then width of the viewport
if(xScroll < windowWidth){
pageWidth = windowWidth;
} else {
pageWidth = xScroll;
}
arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
return arrayPageSize;
}
//
// pause(numberMillis)
// Pauses code execution for specified time. Uses busy code, not good.
// Code from http://www.faqts.com/knowledge_base/view.phtml/aid/1602
//
function pause(numberMillis) {
var now = new Date();
var exitTime = now.getTime() + numberMillis;
while (true) {
now = new Date();
if (now.getTime() > exitTime)
return;
}
}
//
// getKey(key)
// Gets keycode. If 'x' is pressed then it hides the lightbox.
//
function getKey(e){
if (e == null) { // ie
keycode = event.keyCode;
} else { // mozilla
keycode = e.which;
}
key = String.fromCharCode(keycode).toLowerCase();
if(key == 'x'){ hideLightbox(); }
}
//
// listenKey()
//
function listenKey () { document.onkeypress = getKey; }
//
// showLightbox()
// Preloads images. Pleaces new image in lightbox then centers and displays.
//
function showLightbox(objLink)
{
// prep objects
var objOverlay = document.getElementById('overlay');
var objLightbox = document.getElementById('lightbox');
var objCaption = document.getElementById('lightboxCaption');
var objImage = document.getElementById('lightboxImage');
var objLoadingImage = document.getElementById('loadingImage');
var objLightboxDetails = document.getElementById('lightboxDetails');
var arrayPageSize = getPageSize();
var arrayPageScroll = getPageScroll();
// center loadingImage if it exists
if (objLoadingImage) {
objLoadingImage.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - objLoadingImage.height) / 2) + 'px');
objLoadingImage.style.left = (((arrayPageSize[0] - 20 - objLoadingImage.width) / 2) + 'px');
objLoadingImage.style.display = 'block';
}
// set height of Overlay to take up whole page and show
objOverlay.style.height = (arrayPageSize[1] + 'px');
objOverlay.style.display = 'block';
// preload image
imgPreload = new Image();
imgPreload.onload=function(){
objImage.src = objLink.href;
// center lightbox and make sure that the top and left values are not negative
// and the image placed outside the viewport
var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - imgPreload.height) / 2);
var lightboxLeft = ((arrayPageSize[0] - 20 - imgPreload.width) / 2);
objLightbox.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
objLightbox.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";
objLightboxDetails.style.width = imgPreload.width + 'px';
if(objLink.getAttribute('title')){
objCaption.style.display = 'block';
//objCaption.style.width = imgPreload.width + 'px';
objCaption.innerHTML = objLink.getAttribute('title');
} else {
objCaption.style.display = 'none';
}
// A small pause between the image loading and displaying is required with IE,
// this prevents the previous image displaying for a short burst causing flicker.
if (navigator.appVersion.indexOf("MSIE")!=-1){
pause(250);
}
if (objLoadingImage) { objLoadingImage.style.display = 'none'; }
objLightbox.style.display = 'block';
// After image is loaded, update the overlay height as the new image might have
// increased the overall page height.
arrayPageSize = getPageSize();
objOverlay.style.height = (arrayPageSize[1] + 'px');
// Check for 'x' keypress
listenKey();
return false;
}
imgPreload.src = objLink.href;
}
//
// hideLightbox()
//
function hideLightbox()
{
// get objects
objOverlay = document.getElementById('overlay');
objLightbox = document.getElementById('lightbox');
// hide lightbox and overlay
objOverlay.style.display = 'none';
objLightbox.style.display = 'none';
// disable keypress listener
document.onkeypress = '';
}
//
// initLightbox()
// Function runs on window load, going through link tags looking for rel="lightbox".
// These links receive onclick events that enable the lightbox display for their targets.
// The function also inserts html markup at the top of the page which will be used as a
// container for the overlay pattern and the inline image.
//
function initLightbox()
{
if (!document.getElementsByTagName){ return; }
var anchors = document.getElementsByTagName("a");
// loop through all anchor tags
for (var i=0; i<anchors.length; i++){="" var="" anchor="anchors[i];" if="" (anchor.getattribute("href")="" &&="" (anchor.getattribute("rel")="=" "lightbox")){="" anchor.onclick="function" ()="" {showlightbox(this);="" return="" false;}="" }="" }="" the="" rest="" of="" this="" code="" inserts="" html="" at="" the="" top="" of="" the="" page="" that="" looks="" like="" this:="" <div="" id="overlay">
//
//
//
//
//
//
//
//
//
var objBody = document.getElementsByTagName("body").item(0);
// create overlay div and hardcode some functional styles (aesthetic styles are in CSS file)
var objOverlay = document.createElement("div");
objOverlay.setAttribute('id','overlay');
objOverlay.onclick = function () {hideLightbox(); return false;}
objOverlay.style.display = 'none';
objOverlay.style.position = 'absolute';
objOverlay.style.top = '0';
objOverlay.style.left = '0';
objOverlay.style.zIndex = '90';
objOverlay.style.width = '100%';
objBody.insertBefore(objOverlay, objBody.firstChild);
var arrayPageSize = getPageSize();
var arrayPageScroll = getPageScroll();
// preload and create loader image
var imgPreloader = new Image();
// if loader image found, create link to hide lightbox and create loadingimage
imgPreloader.onload=function(){
var objLoadingImageLink = document.createElement("a");
objLoadingImageLink.setAttribute('href','#');
objLoadingImageLink.onclick = function () {hideLightbox(); return false;}
objOverlay.appendChild(objLoadingImageLink);
var objLoadingImage = document.createElement("img");
objLoadingImage.src = loadingImage;
objLoadingImage.setAttribute('id','loadingImage');
objLoadingImage.style.position = 'absolute';
objLoadingImage.style.zIndex = '150';
objLoadingImageLink.appendChild(objLoadingImage);
imgPreloader.onload=function(){}; // clear onLoad, as IE will flip out w/animated gifs
return false;
}
imgPreloader.src = loadingImage;
// create lightbox div, same note about styles as above
var objLightbox = document.createElement("div");
objLightbox.setAttribute('id','lightbox');
objLightbox.style.display = 'none';
objLightbox.style.position = 'absolute';
objLightbox.style.zIndex = '100';
objBody.insertBefore(objLightbox, objOverlay.nextSibling);
// create link
var objLink = document.createElement("a");
objLink.setAttribute('href','#');
objLink.setAttribute('title','Click to close');
objLink.onclick = function () {hideLightbox(); return false;}
objLightbox.appendChild(objLink);
// preload and create close button image
var imgPreloadCloseButton = new Image();
// if close button image found,
imgPreloadCloseButton.onload=function(){
var objCloseButton = document.createElement("img");
objCloseButton.src = closeButton;
objCloseButton.setAttribute('id','closeButton');
objCloseButton.style.position = 'absolute';
objCloseButton.style.zIndex = '200';
objLink.appendChild(objCloseButton);
return false;
}
imgPreloadCloseButton.src = closeButton;
// create image
var objImage = document.createElement("img");
objImage.setAttribute('id','lightboxImage');
objLink.appendChild(objImage);
// create details div, a container for the caption and keyboard message
var objLightboxDetails = document.createElement("div");
objLightboxDetails.setAttribute('id','lightboxDetails');
objLightbox.appendChild(objLightboxDetails);
// create caption
var objCaption = document.createElement("div");
objCaption.setAttribute('id','lightboxCaption');
objCaption.style.display = 'none';
objLightboxDetails.appendChild(objCaption);
// create keyboard message
var objKeyboardMsg = document.createElement("div");
objKeyboardMsg.setAttribute('id','keyboardMsg');
objKeyboardMsg.innerHTML = 'press
x to close';
objLightboxDetails.appendChild(objKeyboardMsg);
}
//
// addLoadEvent()
// Adds event to window.onload without overwriting currently assigned onload functions.
// Function found at Simon Willison's weblog - http://simon.incutio.com/
//
function addLoadEvent(func)
{
var oldonload = window.onload;
if (typeof window.onload != 'function'){
window.onload = func;
} else {
window.onload = function(){
oldonload();
func();
}
}
}
addLoadEvent(initLightbox); // run initLightbox onLoad
</script>
<style type="text/css" media="screen">
#lightbox{
background-color:#eee;
padding: 10px;
border-bottom: 2px solid #666;
border-right: 2px solid #666;
}
#lightboxDetails{
font-size: 0.8em;
padding-top: 0.4em;
}
#lightboxCaption{ float: left; }
#keyboardMsg{ float: right; }
#closeButton{ top: 5px; right: 5px; }
#lightbox img{ border: none; clear: both;}
#overlay img{ border: none; }
#overlay{ background-image: url(http://www.huddletogether.com/projects/lightbox/overlay.png); }
* html #overlay{
background-color: #333;
background-color: transparent;
background-image: url(http://www.huddletogether.com/projects/lightbox/blank.gif);
filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src="http://www.huddletogether.com/projects/lightbox/overlay.png", sizingMethod="scale");
}
</style>
分享到:
相关推荐
Lightbox JS是一款经典的JavaScript库,专门用于创建优雅的图片弹出展示效果,它将图片以半透明背景下的全屏模式显示,提供了一种沉浸式的浏览体验。这个库因其简洁、高效和兼容性广而受到广大Web开发者的青睐。在...
Lightbox JS V2.0是一款经典的JavaScript库,用于在网页上实现优雅的图片弹出展示效果,常用于创建图片相册或展示大图。这个压缩包文件“Lightbox JS V2.0代码.rar”很可能包含了实现这一功能的所有源代码、样式表、...
Lightbox JS V2.0是一款经典的JavaScript库,用于在网页上创建优雅的、浮动式的图片查看体验。这个插件的设计理念是将用户从繁忙的网页背景中隔离出来,专注于单个图片,提供一种沉浸式的浏览体验。以下是关于...
Lightbox JS 是一款广泛使用的JavaScript库,专门设计用于创建优雅的图片预览体验。这款插件的核心功能是在网页上以弹出式、全屏或半透明背景的方式展示图片,为用户提供了无需离开当前页面就能查看大图的便利。...
Lightbox JS V2.0是一个流行且用户友好的JavaScript库,专门用于创建具有优雅过渡效果的图片查看器。这个库允许用户在当前网页上以弹出窗口的形式查看大图,而不是打开新页面或跳转到其他位置。它以其简洁的设计和...
Lightbox JS图片展示是一种流行的网页设计技术,用于在用户点击图像预览链接时,弹出一个半透明的层,以全屏或放大视图显示图片。这种效果在网站上广泛用于展示照片集、画廊或者产品细节,因为它能提供一个无干扰的...
而 `Lightbox JS` 是一个流行的JavaScript库,用于创建美观的图片弹出层,让用户可以在当前页面上查看大图,而不会被重定向到新的页面。 ### JSP 基础 JSP 是一种基于 Java 的服务器端技术,它允许开发者将静态...
- **js** 文件夹:包含LightBox JS 的JavaScript库,如`jquery.js`(可能用于依赖jQuery实现某些功能)和`lightbox.js`,这是LightBox的核心脚本,负责处理弹出层的显示、隐藏、动画效果和交互逻辑。 了解了...
"Lightbox JS"是一个实现这一功能的JavaScript库,它使得在网页上实现Lightbox效果变得简单易行。 Lightbox JS库的核心特性包括: 1. **弹出窗口**:当用户点击链接或图像时,Lightbox会创建一个透明的黑色背景,...
Lightbox JS 2.0是实现这一效果的JavaScript库之一。它允许开发者通过简单的代码集成,为网页图片添加悬停预览和全屏查看功能。Lightbox JS 2.0的核心特性包括: 1. **弹出窗口显示**:当用户点击图片链接时,图片...
<script type="text/javascript" src="js/lightbox.js"></script> 2、外调 Lightbox CSS 文件 (或添加 Lightbox 样式到你现行的样式表中). <link rel="stylesheet" href="css/lightbox.css" _fcksavedurl=""css/...
Lightbox JS V2.0是一种流行的JavaScript库,用于在网页上实现优雅的图片预览效果。这个库由David DeSandro开发,它允许用户在点击链接后,图片会在当前页面上以半透明背景覆盖的形式弹出,而不是跳转到新的页面或者...
综上所述,"As Lightbox + js 加载图片进度"是一个关于网页图片展示技术的话题,涉及JavaScript的事件监听、图片加载优化和用户体验设计等多个方面。通过理解这些知识点,开发者可以创建更加友好和高效的图片浏览...
lightBox JavaScript 内容显示效果
"js"目录可能包含Lightbox插件的核心JavaScript代码,可能包括事件处理、动画效果、媒体加载等功能。"dist"目录通常存放编译后的、可以直接在浏览器中运行的文件,如压缩和优化过的JS和CSS文件,方便网站部署。 ...
这个名为"As lightbox js实现实时加载图片进度.rar"的压缩包文件提供了一个解决方案,它结合了ActionScript(AS)、Lightbox效果以及JavaScript技术,以创建一个能够展示图片并显示加载进度的功能。下面我们将详细...
jquery.lightbox.js遮罩层图片幻灯片自适应图片 jquery.lightbox.js遮罩层图片幻灯片自适应图片 jquery.lightbox.js遮罩层图片幻灯片自适应图片 jquery.lightbox.js遮罩层图片幻灯片自适应图片
下面是使用我们在使用 lightbox 来显示图片时的基本设置: 代码如下: <a> content </a> 主要是给连接加了一个 rel=”lightbox” 属性 以上面的试设置好连接时, 当点击此连接,lightbox 将会显示连接上的图片,并...
JS插件开发之LightBox,可兼容到IE6