Lightbox JS 是由Lokesh Dhakar封装的类似windows xp关机效果的js脚本。对目前流行浏览器支持比较好,最新版本为Lightbox JS V2.0。
使用方法:
1、在页面头部包含 lightbox.js 文件并加载 lightbox.css 样式表文件

<script type="text/javascript" src="lightbox.js"></script>
<link href="lightbox.css" rel="stylesheet" type="text/css" />

2、在链接标签添加 rel="lightbox" 激活 lightbox ,例如:

<a href="images/image-1.jpg" rel="lightbox" title="my caption">image #1</a>

提示:可以使用链接的 title 属性来设置标题

常见问题:
文件总是在新窗口里打开
这一般是由于脚本冲突造成的,请检查body标签的onload属性。例如:

<body onload="MM_preloadImages(‘/images/menu_on.gif’)…;">

在body的onload属性追加initLightbox()函数可以快速解决该问题

<body   onload="MM_preloadImages(‘/images/menu_on.gif’)…;initLightbox()">

文件代码:
文件:lightbox.js

/*
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 = 'loading.gif';
var closeButton = '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.οnlοad=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'; }

// Hide select boxes as they will 'peek' through the image in IE
selects = document.getElementsByTagName("select");
for (i = 0; i != selects.length; i++) {
selects[i].style.visibility = "hidden";
}

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';

// make select boxes visible
selects = document.getElementsByTagName("select");
for (i = 0; i != selects.length; i++) {
selects[i].style.visibility = "visible";
}

// 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">
// <a href="#" οnclick="hideLightbox(); return false;"><img id="loadingImage" /></a>
// </div>
// <div id="lightbox">
// <a href="#" οnclick="hideLightbox(); return false;" title="Click anywhere to close image">
// <img id="closeButton" />
// <img id="lightboxImage" />
// </a>
// <div id="lightboxDetails">
// <div id="lightboxCaption"></div>
// <div id="keyboardMsg"></div>
// </div>
// </div>

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.οnlοad=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.οnlοad=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.οnlοad=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 <a href="#" οnclick="hideLightbox(); return false;"><kbd>x</kbd></a> 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

 

文件:lightbox.css

#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(overlay.png); }

* html #overlay{
background-color: #333;
back/ground-color: transparent;
background-image: url(blank.gif);
filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src="overlay.png", sizingMethod="scale");
}

 
下载:
  • lightbox.js
  • lightbox.css
  • overlay.png
  • loading.gif
  • close.gif

更多信息请参考:http://www.huddletogether.com/projects/lightbox/
Lightbox JS v2.0效果请参照:http://www.huddletogether.com/projects/lightbox2/

原文链接: http://blog.csdn.net/vince6799/article/details/1867794

转载于:https://my.oschina.net/chen106106/blog/45925

Lightbox JS 用法相关推荐

  1. Lightbox JS v2.0

    概要: Lightbox JS 是一个简单而又谦恭的用来把图片覆盖在当前页面上的脚本. 它能被快速安装并且运作于所有流行的浏览器. 最新更新 Version 2.0 图片集: 分组相关的图片并且能轻松 ...

  2. html列表拖拽排序插件,JS拖拽排序插件Sortable.js用法实例分析

    本文实例讲述了JS拖拽排序插件Sortable.js用法.分享给大家供大家参考,具体如下: 最近由于项目功能设计的原因,需要对table中的行实现拖拽排序功能,找来找去发现Sortable.js能很好 ...

  3. 灯箱效果lightbox.js的使用示例

    灯箱效果想必大家都很熟悉,平常用的也会比较多. 今天研究了下反应比较好的jquery插件lightbox.js,虽然界面效果不错,但是也有缺点 兼容性:各大浏览器和ie6+ 不足: 1由于我们使用时是 ...

  4. wow.js用法详解

    页面在向下滚动的时候,有些元素会产生细小的动画效果.虽然动画比较小,但却能吸引你的注意 刚知道wow.js这个插件,之前写的类似滚动时页面效果都是自己用jQuery写的,现在有了插件,开发更加快捷有效 ...

  5. Require.js用法

    require.js用法说明 用法一:先直接引入require.js,在使用data-main引入相应的js文件.例如: <script src="require.js" d ...

  6. clamp.js用法初窥

    众所周知,要实现文本溢出显示省略号可以用text-overflow:ellipsis;但是只能显示单行文本: 多行文本可以用-webkit-line-clamp:n;但是这是webkit的私有属性,只 ...

  7. Vue.js用法详解(一)更新中~

      前  言 前段时间为了一个数据查询的项目自学了Vue,感觉这款框架还是很不错的,今天就整理整理这个框架如何使用,希望对正在学这个框架的小伙伴有所帮助~ 首先,我们先来了解一下Vue: Vue.js ...

  8. iScroll.js 用法参考 (share)

    http://www.cnblogs.com/asqq/archive/2012/04/23/2466132.html http://www.nb88.net/Index/p/id/406 http: ...

  9. dva的用法_dva.js 用法详解:列表展示

    这次主要通过在线获取用户数据并且渲染成列表这个案例来演示dva.js. 整个开发流程概括下来应该是: 编写用户列表model(数据模型)->  编写修改model的方法 -> 编写服务接口 ...

  10. require.js用法简介

    一.为什么要用require.js? 最早的时候,所有Javascript代码都写在一个文件里面,只要加载这一个文件就够了.后来,代码越来越多,一个文件不够了,必须分成多个文件,依次加载.下面的网页代 ...

最新文章

  1. iOS 最新版 CocoaPods 的安装流程
  2. 报告老板,微服务高可用神器已祭出,您花巨资营销的高流量来了没?
  3. 倒排索引PForDelta压缩算法——基本假设和霍夫曼压缩同
  4. gorm软删除_gorm踩坑:软删除与某个字段的唯一性
  5. 四大微博OAuth认证
  6. 通过Redis、Memcache的 incr 原子操作防刷机制的使用差别
  7. frame中src怎么设置成一个变量_自动格式化打印变量HMLog介绍
  8. yum安装odbc驱动linux,在CentOS上离线配置PostgreSQL ODBC数据源
  9. 如何连接oracle 12c可插拔数据库
  10. 性能测试学习线路图(建议)
  11. [禅悟人生]有自知之明, 在深浅之间权衡做人
  12. 【亲测】原神3.2+文本教程+视频教程+GM工具
  13. Spring框架-AOP
  14. 从有线路由器连接到另一个无线路由器
  15. Linux 磁盘划分 LVM 逻辑分区管理步骤
  16. 玩转NPM,搭建私有仓库-姜威-专题视频课程
  17. 简述Java长期占据主要地位的原因
  18. 博人传:火影迷青春的证明和延续
  19. win7 激活的两种方法
  20. PostGIS数据库搭建与gdb数据导入

热门文章

  1. 「周末观赛指南」国足生死战 NBA将演“大结局”?
  2. C# 中xmlreader类的实用源码演示
  3. vnc远程控制软件配置,vnc远程控制软件怎么配置的,教程详解
  4. 台式计算机把光驱改成硬盘,台式机光驱位怎么安装硬盘
  5. 腾讯cdn设置 php,wordpress网站使用腾讯CDN的最佳缓存配置
  6. 度量衡计算工具_度量衡计量单位换算转换器
  7. 科技论文之Introduction部分写作
  8. 传奇服务器修改物品名字,幽冥传奇服务端目录说明及幽冥传奇开服修改文件目录...
  9. h5前端兼容性问题及解决方法集合
  10. 合天网络靶场-大规模网络环境仿真服务平台