recursive.codes


The Personal Blog of Todd Sharp

Super Simple "Click To Enlarge Image" With jQuery, Bootstrap and CSS

Posted By: Todd Sharp on 2/18/2020 7:18 GMT
Tagged: HTML, JavaScript

I've been spending a little free time lately showing this blog some love. Over the weekend I created a custom script to syndicate all of my posts from my work blog here on my personal blog, just for some cross promotion and to keep my personal blog updated with all of the knowledge and content that I share on that blog. Last night I added another feature that I had been wanting to add for a long time: the ability to view a full size image by clicking on an image in a post. I use Bootstrap for my blog, and typically add img-responsive to my images so that they look good on all screens. But sometimes that makes it a bit hard to see what's going on in an image and seeing as how almost all of the images that I share are some sort of screenshot that demos something, that simply wasn't a great user experience.

Creating this feature was really easy. So easy, in fact, that you might think it's strange that I'm blogging about it. Thing is - in an industry that is prone to overengineering - sometimes the simple solutions are the ones worth sharing. Also, I wanted to point out that I'm using jQuery (gasp...) to accomplish it.  Let me know when you're done clutching your pearls, and I'll explain. You see, there's simply nothing wrong with jQuery. In fact, 10+ years in - there is still nothing better at DOM traversal and manipulation. And although we've come a long way since the dark ages of the internet, browsers still aren't fully compatible with one another. So for simple solutions like this - and sometimes things that aren't so simple - I still believe in and trust jQuery. Enough pontificating, on to the code. 

It begins with a simple Bootstrap modal dialog:

I set the overflow-x to scroll so that the image wouldn't overflow the modal - users can scroll to view the entire details. Next, I listen for clicks on images and set the image in to the modal, remove the img-responsive class and show it:

The only other bit was to add a bit of text below each image to let the user know that they can click an image to view it full size (because what good is a feature if the user doesn't know it exists). 

Note: You must wrap the image in a block element because the img tag does not support child elements.

Finally, the CSS to add the text:

Update: Kenny on Twitter pointed out that Macs typically hide the scrollbar when using a trackpad, so I added a bit of CSS to make it visible at all times on the imgViewer element.

And that's all it takes. Here's an example - resize your browser or view on mobile to see how it works.


Image by jpeter2 from Pixabay



Related Posts

OCI SDK For TypeScript Is Now Available - Here's How To Use It In Your JavaScript Projects

OCI SDK For TypeScript Is Now Available - Here's How To Use It In Your JavaScript Projects

I have blogged quite a bit about our OCI Java SDK in the past. It's easy to use and a very intuitive and consistent SDK that can perform just about any...

Resource Principal Auth With Node.JS For Easy OCI REST API Access From Your Oracle Functions

Resource Principal Auth With Node.JS For Easy OCI REST API Access From Your Oracle Functions

The first rule of serverless is to keep your functions light and simple. Part of keeping a function light is avoiding unnecessary external dependencies...

Sending & Receiving SMS In Your Node-RED Flows With Ring Central

Sending & Receiving SMS In Your Node-RED Flows With Ring Central

As you can tell if you've been reading my blog lately, I've been spending some time playing with Ring Central. One of the use cases that I've mentioned...

Note: Comments are currently closed on this blog. Disqus is simply too bloated to justify its use with the low volume of comments on this blog. Please visit my contact page if you have something to say!