Skip to content

Instantly share code, notes, and snippets.

@j0lvera
Last active December 28, 2015 20:29
Show Gist options
  • Save j0lvera/7558023 to your computer and use it in GitHub Desktop.
Save j0lvera/7558023 to your computer and use it in GitHub Desktop.
litebox, minimalistic jQuery plugin that lightbox any DOM element
<!doctype html>
<html>
<head>
<title>jQueryPlugins</title>
<style>
body {
background: #fafafa;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
color: #333;
}
.hero-unit {
display: none;
margin: 50px auto 0 auto;
width: 300px;
font-size: 18px;
font-weight: 200;
line-height: 30px;
background-color: #eee;
border-radius: 6px;
padding: 60px;
}
.hero-unit h1 {
font-size: 60px;
line-height: 1;
letter-spacing: -1px;
}
.browsehappy {
margin: 0.2em 0;
background: #ccc;
color: #000;
padding: 0.2em 0;
}
</style>
</head>
<body>
<div class="hero-unit">
<h1>'Allo, 'Allo!</h1>
<p>You now have</p>
<ul>
<li>HTML5 Boilerplate</li>
</ul>
</div>
<button class="btn">Click ME!</button>
<div class="span8">
<h1>Alice in Wonderland, part dos</h1>
<p>'You ought to be ashamed of yourself for asking such a simple question,' added the Gryphon; and then they both sat silent and looked at poor Alice, who felt ready to sink into the earth. At last the Gryphon said to the Mock Turtle, 'Drive on, old fellow! Don't be all day about it!' and he went on in these words:
'Yes, we went to school in the sea, though you mayn't believe it—'
'I never said I didn't!' interrupted Alice.
'You did,' said the Mock Turtle.</p>
<div>
<span class="badge badge-success">Posted 2012-08-02 20:47:04</span><div class="pull-right"><span class="label">alice</span> <span class="label">story</span> <span class="label">blog</span> <span class="label">personal</span></div>
</div>
<hr>
<h1>Revolution has begun!</h1>
<p>'I am bound to Tahiti for more men.'
'Very good. Let me board you a moment—I come in peace.' With that he leaped from the canoe, swam to the boat; and climbing the gunwale, stood face to face with the captain.
'Cross your arms, sir; throw back your head. Now, repeat after me. As soon as Steelkilt leaves me, I swear to beach this boat on yonder island, and remain there six days. If I do not, may lightning strike me!'A pretty scholar,' laughed the Lakeman. 'Adios, Senor!' and leaping into the sea, he swam back to his comrades.</p>
<div>
<span class="badge badge-success">Posted 2012-08-02 20:47:04</span><div class="pull-right"><span class="label">alice</span> <span class="label">story</span> <span class="label">blog</span> <span class="label">personal</span></div>
</div>
<hr>
</div>
<div class="span8">
<h1>Alice in Wonderland, part dos</h1>
<p>'You ought to be ashamed of yourself for asking such a simple question,' added the Gryphon; and then they both sat silent and looked at poor Alice, who felt ready to sink into the earth. At last the Gryphon said to the Mock Turtle, 'Drive on, old fellow! Don't be all day about it!' and he went on in these words:
'Yes, we went to school in the sea, though you mayn't believe it—'
'I never said I didn't!' interrupted Alice.
'You did,' said the Mock Turtle.</p>
<div>
<span class="badge badge-success">Posted 2012-08-02 20:47:04</span><div class="pull-right"><span class="label">alice</span> <span class="label">story</span> <span class="label">blog</span> <span class="label">personal</span></div>
</div>
<hr>
<h1>Revolution has begun!</h1>
<p>'I am bound to Tahiti for more men.'
'Very good. Let me board you a moment—I come in peace.' With that he leaped from the canoe, swam to the boat; and climbing the gunwale, stood face to face with the captain.
'Cross your arms, sir; throw back your head. Now, repeat after me. As soon as Steelkilt leaves me, I swear to beach this boat on yonder island, and remain there six days. If I do not, may lightning strike me!'A pretty scholar,' laughed the Lakeman. 'Adios, Senor!' and leaping into the sea, he swam back to his comrades.</p>
<div>
<span class="badge badge-success">Posted 2012-08-02 20:47:04</span><div class="pull-right"><span class="label">alice</span> <span class="label">story</span> <span class="label">blog</span> <span class="label">personal</span></div>
</div>
<hr>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
// litebox!
;(function(e,t,n){"use strict";e.fn.litebox=function(t){function s(e){var t={position:"fixed",top:"50%",left:"50%",marginLeft:e.outerWidth()/-2,marginTop:e.outerHeight()/-2,boxShadow:"0 0 20px"+n.shadow};o(e);e.css(t)}function o(t){var i={position:"absolute",bottom:"-45px",right:"0px",cursor:"pointer",color:"#fff",background:"#333",fontSize:"14px",padding:"0 10px",boxShadow:"0 0 20px"+n.shadow,borderRadius:"4px"};r.close=e('<div class="litebox-close">close</div>');t.append(r.close.css(i))}function u(){var t={position:"fixed",top:"0",left:"0",right:"0",bottom:"0",background:"rgba(0,0,0,"+n.opacity+")",display:"none"};r.lbg=e('<div class="litebox-background"></div>');r.body.prepend(r.lbg);r.lbg.css(t).fadeIn(n.speed).delay(n.speed,function(){r.el.fadeIn(n.speed)})}function a(){var e=function(){r.lbg.hide()};r.el.fadeOut(r.speed,e);r.close.off("click")}var n=e.extend({speed:300,opacity:.7,shadow:"#333"},t);var r={el:this,body:e("body"),close:{},lbg:{}};var i=function(){s(r.el);u();r.close.on("click",a)};i()}})(jQuery,window,document);
</script>
<script type="text/javascript">
var btn = $('.btn'),
hero = $('.hero-unit');
btn.on('click', function(){
hero.litebox({
speed: 200,
opacity: 0.4
});
});
</script>
</body>
</html>
The MIT License (MIT)
Copyright (c) 2013 Juan Olvera
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
/*
* litebox
* Author: Juan Olvera
* Version: 0.1
*
* Licensed under the MIT license
*/
;(function($, window, document) {
'use strict';
$.fn.litebox = function(options) {
// defaults
var d = $.extend({
speed: 300,
opacity: 0.7,
shadow: '#333'
}, options);
// settings
var s = {
el: this,
body: $('body'),
close: {},
lbg: {}
};
// initialization code
var init = function() {
center(s.el);
show();
s.close.on('click', closeEverything);
};
// center the element before appear
function center(el) {
var css = {
position : 'fixed',
top : '50%',
left : '50%',
marginLeft: el.outerWidth() / -2,
marginTop: el.outerHeight() / -2,
boxShadow: '0 0 20px' + d.shadow
};
addCloseBtn(el);
el.css(css);
}
// add the close button to the element
function addCloseBtn(el) {
var css = {
position: 'absolute',
bottom: '-45px', right: '0px',
cursor: 'pointer',
color: '#fff',
background: '#333',
fontSize: '14px',
padding: '0 10px',
boxShadow: '0 0 20px' + d.shadow,
borderRadius: '4px'
};
s.close = $('<div class="litebox-close">close</div>');
el.append(s.close.css(css));
}
// show black background
// & show element
function show() {
var css = {
position: 'fixed',
top: '0', left: '0', right: '0', bottom: '0',
background: 'rgba(0,0,0,' + d.opacity + ')',
display: 'none'
};
s.lbg = $('<div class="litebox-background"></div>');
s.body.prepend(s.lbg);
s.lbg.css(css).fadeIn(d.speed).delay(d.speed, function(){
s.el.fadeIn(d.speed);
});
}
// hide element and background
function closeEverything() {
var clearBackground = function() {
s.lbg.hide();
};
s.el.fadeOut(s.speed, clearBackground);
s.close.off('click');
}
// do it
init();
};
})(jQuery, window, document);
;(function(e,t,n){"use strict";e.fn.litebox=function(t){function s(e){var t={position:"fixed",top:"50%",left:"50%",marginLeft:e.outerWidth()/-2,marginTop:e.outerHeight()/-2,boxShadow:"0 0 20px"+n.shadow};o(e);e.css(t)}function o(t){var i={position:"absolute",bottom:"-45px",right:"0px",cursor:"pointer",color:"#fff",background:"#333",fontSize:"14px",padding:"0 10px",boxShadow:"0 0 20px"+n.shadow,borderRadius:"4px"};r.close=e('<div class="litebox-close">close</div>');t.append(r.close.css(i))}function u(){var t={position:"fixed",top:"0",left:"0",right:"0",bottom:"0",background:"rgba(0,0,0,"+n.opacity+")",display:"none"};r.lbg=e('<div class="litebox-background"></div>');r.body.prepend(r.lbg);r.lbg.css(t).fadeIn(n.speed).delay(n.speed,function(){r.el.fadeIn(n.speed)})}function a(){var e=function(){r.lbg.hide()};r.el.fadeOut(r.speed,e);r.close.off("click")}var n=e.extend({speed:300,opacity:.7,shadow:"#333"},t);var r={el:this,body:e("body"),close:{},lbg:{}};var i=function(){s(r.el);u();r.close.on("click",a)};i()}})(jQuery,window,document);
{
"name": "litebox",
"description": "minimalistic jQuery plugin that lightbox any DOM element.",
"dependencies": {
"jquery": "1.10.2"
},
"keywords": [
"lightbox",
"jquery plugin"
],
"license": "MIT"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment