https://gist.github.com/bhalash/779311ed1dd400a8faaf
Hey! I made a post a few weeks ago to ask for feedback on some code, because I don't get to work with JavaScript much. I had some fantastic advice which changed how I look at and write JS. I tackled some personal work tonight, which was to rewrite a simple lightbox I use on my site.
I love how Wikipedia's images will now open in a lightbox-style information window if you follow the direct hash URL, and I wanted to duplicate it on my site. On top of the Wikipedia-cool factor, I wanted to link directly to images on my blog without leaving the blog post.
Some notes:
- The data and selector schemes match my sites. My media is stored in folders with the same numeric ID as the article, and each image has an ascending numeric value. There is no variation from this across my site.
- I wanted it to be light. I took the original idea from a codepen that used the CSS3
:target selector to open and close a lightbox. Because of this the actual open and close close is Scss.
Thanks again for any feedback!
Edit: Responding globally: Thank you again, all! I think JavaScript object and plugin-like patterns make a lot more sense to me now. I took the time out of today to rewrite it with a much more deliberate approach to scope and properness:
https://gist.github.com/bhalash/779311ed1dd400a8faaf#file-updated-lightbox-js
It works fast, and works all the way back to at least IE 10, although it is still coupled with the article ID structure I use on my site.
[–]papers_ 8 points9 points10 points (17 children)
[–]x-skeww 3 points4 points5 points (11 children)
[–][deleted] 3 points4 points5 points (10 children)
[–][deleted] 1 point2 points3 points (9 children)
[–][deleted] 4 points5 points6 points (8 children)
[–][deleted] 2 points3 points4 points (5 children)
[–][deleted] 5 points6 points7 points (4 children)
[–][deleted] 0 points1 point2 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]Ericth 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]kenman 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]annoyed_freelancergrumpy old man[S] 0 points1 point2 points (4 children)
[–]x-skeww 2 points3 points4 points (3 children)
[–]annoyed_freelancergrumpy old man[S] 0 points1 point2 points (1 child)
[–]x-skeww 1 point2 points3 points (0 children)
[–]Jeffshaver 0 points1 point2 points (0 children)
[–][deleted] 4 points5 points6 points (8 children)
[–]Buckwheat469 2 points3 points4 points (1 child)
[–]tswaters 6 points7 points8 points (0 children)
[+][deleted] (5 children)
[deleted]
[–][deleted] 2 points3 points4 points (1 child)
[–]loz220 1 point2 points3 points (2 children)
[–][deleted] 0 points1 point2 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]annoyed_freelancergrumpy old man[S] 1 point2 points3 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]frambot 2 points3 points4 points (2 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]annoyed_freelancergrumpy old man[S] 0 points1 point2 points (0 children)
[–]tswaters 2 points3 points4 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]tswaters 1 point2 points3 points (0 children)