jSquares for jQuery October 22, 2009 130 Comments
If you’ve viewed my “lab” section, that is a perfect example of what this is. jSquares is a jQuery plugin that pops up an image and a description in an overlay on hover. It is basically identical to the image grid found on www.ted.com. Works like a charm in IE6+, FF 3+, Safari 3+ and Opera 10.
Setting it up
Make sure you’re including these files in the head of your document
<script src="http://www.google.com/jsapi" type="text/javascript"></script>
<script type="text/javascript">google.load("jquery","1.6.1");</script>
<link rel="stylesheet" href="css/jsquares.css" type="text/css" media="all" />
<script src="js/jquery.jsquares.min.js" type="text/javascript"></script>To initiate jSquares simply wrap it in the jQuerys document ready function like so.
<script type="text/javascript"> $(document).ready(function(){ $('#js-container').jsquares(); }); </script>
The default chunk of HTML will look something like this:
<div id="js-container"> <div class="js-image"> <a href="#wolf!"><img class="js-small-image" src="images/wolf-moon.jpg"/></a> <div class="js-small-caption"> <span>Howlla at cha boy!</span> </div> <div class="js-overlay-caption-content"> <h4>You can't hide from wolf boy.</h4> <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. In elementum arcu non orci fermentum nec suscipit neque dignissim... <a href="#">Read more »</a> </p> </div> </div> </div>
Optional parameters
Parameters can be added when you call jsquares like so:
$('#js-container').jsquares({ shuffle_in: false, // have the pictures all fade in on page load? fade_on_hover: false, // do we want the images to fade on hover or just change opacity? caption_slide_down: false // do we want the caption to slide down or just appear? });
Here’s a list of all of the options it can take:
| Key | Default Value | Type | Description |
|---|---|---|---|
| image | .js-image | String | The target element holding all of the info (image/caption). View the source of the demo for a better understanding. |
| caption | .js-small-caption | String | The target element holding the caption. This element is found within the element specified as js_image. |
| caption_overlay_spacing | 12 | Integer | This is the padding on the caption that pops up on hover. |
| caption_width | 400 | Integer | This is the width of the caption that pops up on hover. |
| caption_height | 130 | Integer | This is the height of the caption that pops up on hover. |
| fade_to | .3 | Integer | Float | This is the opacity that you want the images/captions to fade to when you roll over an image. |
| fade_start | 1 | Integer | Float | This is opacity that images/captions are sent back to after you roll off the image set. This normally shouldn’t be altered. |
| shuffle_in | true | Boolean | By default the images will shuffle in (fade in) randomly. If you don’t want any effects and to have them just appear, change this to false. |
| shuffle_in_speed | 130 | Integer | If js_shuffle_in is set to true, you can set the speed in which images appear one after the other. |
| fade_on_hover | true | Boolean | By default the images fade to a value set by js_fade_to, if you just want them to change opacity then change this to false. |
| caption_slide_down | true | Boolean | The caption has a slide down effect when you hover over an image. If you just want it to appear instead of sliding down, change this to false. |
| caption_slidedown_speed | fast | String | Integer | If the option key js_caption_slide_down is set to true, you can change the speed that it slides down at. |
Any comments/suggestions are definitely encouraged. Enjoy!


The js is very nicely written. Well done.
1. To be able to change the image inside the overlay, different to the one on display.
2. To change the size of the overlay in conjunction with the size of the image. I have a large image that is 250*250 and the overlay size is 400*250. My smaller image is 140*140 and I would like the overlay to be 140*400.
A great example would be the design of Tiffany the jewellers
http://uk.tiffany.com/Shopping/CategoryBrowse.aspx?cid=579740&mcat=148206
Is it possible to lightbox the images? I think if that could work, then it would be more functional, so that you could see images in more detail.
Thoughts?
Thanks!
One comment, as an additional setting, it would be better in my opinion if we could chose wether to use onClick or OnMouseOver event to launch to caption.
Thanks for this great piece of work!
@Jamie: All scripts are licensed under the GPL. You can go ahead and use it in any of your projects.
@Kathy: You would need to elaborate more, but I don't for-see any problems. JSON is just a data format.
If you have the time, could you explain how you integrate this with wordpress. I've created a loop to get posts and pass title, thumbnail and excerpt to the jSquare boxes but I end up with the same post in all the windows. I have a feeling it's something to do with the box array and needing to put the posts data into it. If you could tell me what words I should google to find out info please let me know.
Have a great weekend Jordan and thanks again
Thanks Jordan
I want to ask you something...is there any chance this jsquares to be incorporated with JOOMLA ?
I got site in joomla i want this same effect...but still haven't found proper component...
Thanks !
I'd like to ask a couple questions, though: is it possible to define the max and min width/height of images?
and another one: Is it possible to make it fit on a container div? I mean the images filling as much space as possible in the div instead of leaving all that blank space
either way, love it!
regards sub
cheers
testking 642-975
Thanks
Thanks again for you offer and thanks for publishing such a fantastic script.
how we can make the popup's position as float to right , at present depend on the image it's popup float left or in right ... but I like it only in side, could you plz let me how can I .
.js-overlay-image {
float:right;
}
thanks for reply.
I mean the mouse over popup window position I want to fit into under js-container when change value of _elementLocation >>var curleft=-250 " then it overflow to left instead of left, I like to fit it in js-container without overlapping..
plz help me.
I mean the mouse over popup window position I want to fit into under js-container when change value of _elementLocation >>var curleft=-250 " then it overflow to left instead of right, I like to fit it in js-container without overlapping..
thanks
How can I change the size of the popup window which displays the full-sized version of the original thumbnail (what you see before you hover)?
I would be incredibly grateful if you could help me figure this out - cheers
Sean.
I just wanted to add I'm trying to use slightly different sized images, some landscape some portrait - I just can't seem to get it to work properly.
I think it would be useful to have an onClick event.
Also opacity fading speed would be something to look into. Seems jagged to me a.t.m on a white bg.
Thanks for sharing and have a good 2011!
G
Super nice work. Im kind of new to this. And my question can seem for an ace like you stupid but anyhow i give it a shot.
I like to change the pictures to diffrent ones and have diffrent pictures dispalyes in each window. Do i change that in the css file and basicly how do i code it.
Is it possible to place like 70 diffrent pictures in the photo map and randomise em in the boxes everytime you logg in to the page. Or make em change if youre on the site for´for ex 20sec. And if you can randomise the pictures, can i lock some of the boxes so just some pictures in the boxes changes. ex if you put an info tag in one box and need to be locked, but the others has pictures in it that i want to be dynamic and change.
Where do i code that and how does that code look like.
You Rule
Many thanks
Keith
i will try to add it to my wordpress blog.... hope its ok!!
thanks.
Am I correct in assuming that the in-line styles, i.e. , is what positions the images and creates the grid affect? In other words, your javascript won't create the grid for me, correct?
This is due to the following attributes no linger existing in jQuery:
.attr('offsetTop') .attr('offsetLeft') .attr('tagName')Changing the _elementLocation function slightly fixes this. I had to use a small hack to make it compatible with earlier versions og jQuery as well:
function _elementLocation(obj) { var curleft = 0; var curtop = 0; var curObjTag = ''; do { curleft += obj.offset().left; curtop += obj.offset().top; obj = obj.offsetParent(); curObjTag = ($().jquery > '1.5.2') ? obj.prop('tagName') : obj.attr('tagName'); } while (curObjTag != 'BODY'); return { x: curleft, y: curtop }; }To avoid the flickering of faded elements fading in before they fade out again when you move the mouse between unrelated elements, add .stop(true, false) in the animation chain, like so:
(options.fade_on_hover) ? $(chain).stop(true, false).fadeTo(options.fade_speed, opacity_val) : $(chain).css('opacity', opacity_val);https://github.com/jboesch/jSquares/commit/2f76ddb96f3d2f1da9b788c0a715fbd74e6c620f
Thanks!
Thanks!
first of all I want to thank you for your great work! Awesome!
There is one thing not working:
The onmouseover-popups of the recent version of jsquares arent shown correctly with internet explorer 9. Is there a workaround? Thanks in advance!
Great work. I have just started with the script and it looks good, but if you could help with one issue I have, I would appreciate it.
I have set the href attribute to target="_blank" for the images and links and they work fine except of the internal 'hover image' - its link opens in the same window and I don't know how to change that. Sorry to ask such a simple question, but I am stuck.
Made a little donation to show my appreciation :)
i love your stuff! but need some help.
i'm not sure why when the mouse hover, the pop up doesn't show at all.
i followed everything closely, and even checked the codes, don't seem to miss out anything.
Any reason why? :(
Thanks in advance!
with same code, link is opening in fancybox when clicked on small image which shows before we bring mouse over image
$(document).ready(function(){ $('#js-container').jsquares(); $('#test').hide(); $('.mylink').click(function(){ $('#test').show(); }); });again, test div shows when I click on small image but it doesn't when I click on hover image. It means, its not a compability issue with fancybox...there is something in your js file which is stoping jquery to respond on hover image
Example: $('#your-jsquares-container').delegate('.js-caption-overlay', 'click', function(){
$(this).find('a').fancybox();
});
$(document).ready(function(){ $('#js-container').jsquares(); $('#js-container').delegate('.js-overlay-caption-content', 'click', function(){ $('.mylink').fancybox({ 'width' : 600, 'height' : 525, 'autoScale' : false, 'autoDimensions' : false, 'transitionIn' : 'none', 'transitionOut' : 'none', 'type' : 'iframe', 'hideOnContentClick': true, 'hideOnOverlayClick': true }); }); });didn't work :-(
$(document).ready(function(){ $('#js-container').jsquares(); $(document).delegate(".mylink", "hover", function(event){ event.preventDefault(); $(this).fancybox({ 'width' : 600, 'height' : 525, 'autoScale' : false, 'autoDimensions' : false, 'transitionIn' : 'none', 'transitionOut' : 'none', 'type' : 'iframe', 'hideOnContentClick': true, 'hideOnOverlayClick': true }); }); });How do i script the possibility of a mouseout which hides the overlay content with a fade? (slow parameter or int)
I have everything working the way it should in my browser, but if I'm looking at it in an iPad, then I switch to portrait, I have the images that are inside the class "js-image" resize using media queries, but their positions stay exactly where they were before. So when you rotate the iPad to portrait mode, the images all become smaller, but now there are huge gaps in between all of them. Is there any way to remedy this?
So what I've done is use media queries to resize the "js-image" div and the image inside it, so when the browser is 768px wide, they are all smaller than their original size. When I resize the browser to that, the images shrink as they should. However the problem is that they are all still positioned by the javascript in their original positions (based on the wider 1000px screen). So there are big gaps inbetween them.
Does that make sense?
Also, is there any js version that calls the overlay onclick or any other way of getting this to work on mobile devices.
Thanks