Skip to content
This repository was archived by the owner on Feb 17, 2023. It is now read-only.

Commit 1c78ee1

Browse files
committedDec 29, 2014
added custom animation option; removed images
1 parent 162e3b9 commit 1c78ee1

9 files changed

+100
-37
lines changed
 

‎README.md

+3-6
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,12 @@ _Default: true_
7777

7878
If set to true, the previous and the next image of the currently shown image will be preloaded if not already in cache.
7979

80-
### `{loadingImg: bool}`
80+
### `{loadingAnimation: bool || string || number}`
8181
_Default: true_
8282

83-
If set to true, the loading-gif will be shown until the image is loaded. Feel free to replace the GIF inside the img-folder. Note: This is disabled for IE8 due to bugs with transparent backgrounds and performance issues.
83+
If set to true, an animation will be rendered until the image is loaded. The animation is using CSS3, so it will not work in IE8/9. Alternatively you can pass milliseconds as number, which defines the interval the `span`-elements inside the `jslghtbx-loading-animation`-container get the class `jslghtbx-active` (the default interval is 200ms). You can use this to apply your own styling via CSS. You can also pass a link to an GIF-image, which then replaces the animation. This is disabled for IE8 due to bugs with transparent backgrounds and performance issues. If set to false, no animation is shown.
8484

85-
### `{loadingImgSrc: bool}`
86-
_Default: 'img/jslghtbx-loading.gif'
87-
88-
Here you can set another image-URL for the loading animation.
85+
Note: The animations get an 500ms delay, so they won`t pop up on fast connections.
8986

9087
### `{carousel: bool}`
9188
_Default: true_

‎css/lightbox.css

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎css/lightbox.scss

+15-3
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ $darkblue: #3F658B;
6262
background-color: rgba(0,0,0,0.85);
6363
}
6464
}
65-
/* loading-image */
66-
.jslghtbx-loading-img {
65+
/* loading-animation */
66+
.jslghtbx-loading-animation {
6767
margin-top: -60px;
6868
margin-left: -60px;
6969
width: 120px;
@@ -73,8 +73,20 @@ $darkblue: #3F658B;
7373
display: none;
7474
position: absolute;
7575
z-index: -1;
76+
& > span {
77+
display: inline-block;
78+
width: 20px;
79+
height: 20px;
80+
border-radius: 20px;
81+
margin: 5px;
82+
background-color: #fff;
83+
@include prefixr(transition, all .3s ease-in-out);
84+
&.jslghtbx-active {
85+
margin-bottom: 60px;
86+
}
87+
}
7688
}
77-
.jslghtbx.jslghtbx-loading .jslghtbx-loading-img {
89+
.jslghtbx.jslghtbx-loading .jslghtbx-loading-animation {
7890
display: block;
7991
}
8092
.jslghtbx-nooverflow {

‎img/jslghtbx-loading.gif

-16.4 KB
Binary file not shown.

‎img/jslghtbx-next.png

-4.3 KB
Binary file not shown.

‎img/jslghtbx-prev.png

-4.3 KB
Binary file not shown.

‎index.html

+1-2
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,7 @@ <h2>Download latest version</h2>
204204
lightbox.load({
205205
responsive: true,
206206
preload: true,
207-
loadingImg: true,
208-
loadingImgSrc: 'img/jslghtbx-loading.gif',
207+
loadingAnimation: true,
209208
carousel: true,
210209
closeOnClick: true,
211210
hideOverflow: true,

‎js/lightbox.js

+79-24
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ function Lightbox () {
3434
var thumbnails = [] // thumbnails
3535
var isOpen = false // check if box is open
3636
var loadingImgSrc // path to loading image
37+
var animationEl // reference to animation-element
38+
var animationInt // animation-interval
39+
var animationChildren // childs to animate
40+
var animationTimeout // timeout until animation starts
3741
// controls
3842
var nextBtn = false
3943
var prevBtn = false
@@ -114,11 +118,6 @@ function Lightbox () {
114118
return false
115119
}
116120

117-
// check option
118-
function optIsSet(opt){
119-
120-
}
121-
122121
// lookup element in browser
123122
function exists(id){
124123
if(document.getElementById(id)) {return true}
@@ -190,6 +189,43 @@ function Lightbox () {
190189
return opt && opt[name] && typeof opt[name] === 'function'
191190
}
192191

192+
// start animation
193+
function startAnimation() {
194+
if(isIE8) return
195+
// stop any already running animations
196+
stopAnimation()
197+
var fnc = function() {
198+
addClass(that.box,'jslghtbx-loading')
199+
if(!isIE9 && typeof that.opt.loadingAnimation === 'number'){
200+
var index = 0
201+
animationInt = setInterval(function(){
202+
addClass(animationChildren[index],'jslghtbx-active')
203+
setTimeout(function(){
204+
removeClass(animationChildren[index],'jslghtbx-active')
205+
},that.opt.loadingAnimation)
206+
index = index >= animationChildren.length ? 0 : index += 1
207+
},that.opt.loadingAnimation)
208+
}
209+
}
210+
// set timeout to not show loading animation on fast connections
211+
animationTimeout = setTimeout(fnc,500)
212+
}
213+
214+
// stop animation
215+
function stopAnimation() {
216+
if(isIE8) return
217+
// hide animation-element
218+
removeClass(that.box,'jslghtbx-loading')
219+
// stop animation
220+
if(!isIE9 && typeof that.opt.loadingAnimation !== 'string' && that.opt.loadingAnimation){
221+
clearInterval(animationInt)
222+
// do not use animationChildren.length here due to IE8/9 bugs
223+
for(var i = 0; i < 4; i++) {
224+
removeClass(animationChildren[i],'jslghtbx-active')
225+
}
226+
}
227+
}
228+
193229
// init controls
194230
function initControls() {
195231
if(!nextBtn) {
@@ -332,20 +368,39 @@ function Lightbox () {
332368
})
333369
}
334370

335-
// set loading-image
336-
if(!opt || opt && !isset(opt.loadingImgSrc)) {
337-
loadingImgSrc = 'img/jslghtbx-loading.gif'
338-
} else {
339-
loadingImgSrc = opt.loadingImgSrc
340-
}
341-
if(chckOpt(opt,'loadingImg') || opt && !isset(opt.loadingImg)) {
342-
this.opt['loadingImg'] = true
343-
var el = document.createElement('img')
344-
el.setAttribute('src',loadingImgSrc)
345-
addClass(el,'jslghtbx-loading-img')
346-
this.box.appendChild(el)
371+
// set default for loading animation if none given
372+
if(!opt || opt && !isset(opt['loadingAnimation'])) {
373+
opt['loadingAnimation'] = true
374+
}
375+
// set loading animation
376+
if(isset(opt['loadingAnimation'])) {
377+
this.opt['loadingAnimation'] = opt['loadingAnimation']
378+
if(typeof this.opt['loadingAnimation'] === 'string') {
379+
// set loading GIF
380+
animationEl = document.createElement('img')
381+
animationEl.setAttribute('src',this.opt['loadingAnimation'])
382+
addClass(animationEl,'jslghtbx-loading-animation')
383+
this.box.appendChild(animationEl)
384+
} else if(this.opt['loadingAnimation']) {
385+
// set default animation time
386+
console.log(typeof this.opt['loadingAnimation'])
387+
if(typeof this.opt['loadingAnimation'] === 'boolean') this.opt['loadingAnimation'] = 200
388+
// animate via JS
389+
animationEl = document.createElement('div')
390+
391+
addClass(animationEl,'jslghtbx-loading-animation')
392+
393+
animationEl.appendChild(document.createElement('span'))
394+
animationEl.appendChild(document.createElement('span'))
395+
animationEl.appendChild(document.createElement('span'))
396+
animationEl.appendChild(document.createElement('span'))
397+
398+
// save children
399+
animationChildren = animationEl.children
400+
401+
this.box.appendChild(animationEl)
402+
}
347403
}
348-
349404
// set preload-option
350405
if(chckOpt(opt,'preload') || opt && !isset(opt.preload)) {
351406
this.opt['preload'] = true
@@ -600,8 +655,10 @@ function Lightbox () {
600655
addClass(currImage.img,'jslghtbx-animate-transition')
601656
}
602657
if(cb) cb()
603-
// remove loading-gif
604-
removeClass(that.box,'jslghtbx-loading')
658+
// stop Animation
659+
stopAnimation()
660+
// clear animation timeout
661+
clearTimeout(animationTimeout)
605662
// preload previous and next image
606663
if(that.opt.preload) {
607664
preload()
@@ -626,10 +683,8 @@ function Lightbox () {
626683
// set src
627684
currImage.img.setAttribute('src',src)
628685

629-
// add loading-gif if set and if not IE8
630-
if(this.opt.loadingImg && !isIE8) {
631-
addClass(this.box,'jslghtbx-loading')
632-
}
686+
// start loading animation
687+
startAnimation()
633688
}
634689

635690
this.close = function() {

0 commit comments

Comments
 (0)
This repository has been archived.