The Dilemma
I recently was working on a website where I needed an image to resize or scale dynamically within a liquid layout. The image needed to fit the browser window and take up as much space as possible all while keeping it’s original ratio. I was unable to find any really customizable plugins for jQuery that would accomplish this task, so I decided to build it myself.
This plugin, called maxImage, resizes the image to it’s largest possible size depending on browser size, ratio of image, and some basic options.
Options
These are the editable options and their defaults:
verticalOffset: 0
verticalOffset is the amount of space you don’t want the image to take up. So say you want 10px above the image and 10px below, your verticalOffset would be 20px.
horizontalOffset: 0
horizontalOffset is the same as the verticalOffset except for the space to the left and to the right of the image. Say you have some content to the left of the image that takes up 400px and you always want 10px to the right of the image, your horizontalOffset would be 410px.
position: ‘absolute’
This changes the positioning and surrounding space to be appropriate for either ‘absolute’ positioning or ‘relative’ positioning. Remember, if you want to use position:absolute, it will be absolutely positioned within it’s first parent that has position:relative (if none are set it will be absolutely positioned within the body).
leftSpace: 0
This is an option that automatically sets either margin-left of the image (position:relative) or just left (position:absolute).
topSpace: 0
Similar to leftSpace, but for the top.
rightSpace: 0
Similar to leftSpace, but for the right.
bottomSpace: 0
Similar to leftSpace, but for the bottom.
isBackground: false
This is an option that automatically positions the image behind all of your content and as large as the browser window. It depends on the smallest dimension according to the browser. i.e. If the browser height is smaller according to your image’s ratio than the width, the width will be a little bit cut off.
verticalAlign: ‘bottom’
This is used mostly with the isBackground:true option. If isBackground:true, this option will make it so the top of your image will get cut off if the browser width is smaller than your images width per ratio. i.e. If your browser window is wide, and you have a skinny image, you will see more of the bottom of your image than the top.
zIndex: -10
This option gives you custom control over what z-index is assigned.
maxFollows: ‘both’
This option was added so that the image max will follow the width, the height, or both. i.e. if maxFollows:width, the max will always and only be as large as the width of your browser, and the height will be disregarded; meaning the user might have to scroll down to see the bottom part of the image.
slideShow: false
Simply setting this to true will set your images to run as a background image slideshow with crossfades in between images. See slideDelay to change the length you are on each slide.
slideDelay: 5
In seconds, this number is how long each slide is shown for.
slideShowTitle: true
This option tells the slideshow whether or not to take your title=”" tags from your image and show that content as a caption in the lower right corner.
loadingGif: ‘images/loading.gif’
This is the URL to a loading graphic which is displayed when images are loading in your slideshow.
loadingGifWidth: ‘32px’
This is the width of your loading graphic.
loadingGifHeight: ‘32px’
This is the height of your loading graphic.
Examples
My first example is a very basic example of an image that resizes in a liquid layout with some copy to the left of the image.
$('img.maximage').maxImage({
verticalOffset: 91,
horizontalOffset: 490
});
View all the demos that show how this plugin can be used.
Instructions
You can view instructions on how to use this plugin at the instructions page of the demo.
Download
You can download and try out the most recent release of maxImage at jQuery Plugins.
Community Response
Please leave comments here to let me know what you think of this maxImage and if there is anything I could do to improve it.
Thanks.
Note: I should note I am also working on a dynamically resizing grid plugin for multiple columns of thumbnail images. I will probably call it maxGrid.