recursive.codes

recursive.codes

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

Sending Push Notifications from Oracle Notification Service with Oracle Functions and Pushover

Sending Push Notifications from Oracle Notification Service with Oracle Functions and Pushover

The Oracle Notification Service is an extraordinary service. I’ve blogged about it in the past (see the Complete Guide to the Oracle Notification Service...

Tracking & Analyzing Water Usage Data in the Cloud with a Flow Sensor, Microcontroller & Autonomous DB

Tracking & Analyzing Water Usage Data in the Cloud with a Flow Sensor, Microcontroller & Autonomous DB

This past summer, I was lucky enough to get spend some virtual time with some of the awesome interns here at Oracle. It makes me happy to see so many college...

Instance and Resource Principal Authentication With The OCI TypeScript/JavaScript SDK

Instance and Resource Principal Authentication With The OCI TypeScript/JavaScript SDK

In June, we launched the Oracle Cloud Infrastructure (OCI) SDK for TypeScript & JavaScript to enable you to work with all of your favorite cloud...

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!