GithubHelp home page GithubHelp logo

myalban / image-scale Goto Github PK

View Code? Open in Web Editor NEW

This project forked from gestixi/image-scale

0.0 2.0 0.0 144 KB

Scale images to fit or fill any target container via two simple properties: scale and align

License: MIT License

image-scale's Introduction

Image Scale

Scale images to fit or fill any target container via two simple properties: scale and align.

This plugin is greatly inspired from Sproutcore SC.ImageView.

Usage

image-scale depends on jQuery. To use it, include this in your page :

<script src="jquery.js" type="text/javascript"></script>
<script src="image-scale.js" type="text/javascript"></script>

If you want to identify the images that you want to scale, you can add a class to them. In this example we are adding a class call scale.

You can also set the data-scale and data-align attributes directly to the images if you want to override the default setting.

<div class="image-container">
  <img class="scale" data-scale="best-fit-down" data-align="center" src="img/example.jpg">
</div>

Now add this JavaScript code to your page :

$(function() {
  $("img.scale").imageScale();
});

You're done.

##Properties##

####scale####

Determines how the image will scale to fit within its containing space. Possible values:

  • fill - stretches or compresses the source image to fill the target frame
  • best-fill - fits the shortest side of the source image within the target frame while maintaining the original aspect ratio
  • best-fit - fits the longest edge of the source image within the target frame while maintaining the original aspect ratio
  • best-fit-down - same as best-fit but will not stretch the source if it is smaller than the target
  • none - the source image is left unscaled
Type: String
Default: best-fill

####align####

Align the image within its frame. Possible values:

  • left
  • right
  • center
  • top
  • bottom
  • top-left
  • top-right
  • bottom-left
  • bottom-right
Type: String
Default: center

####parent####

A jQuery Object against which the image size will be calculated. If null, the parent of the image will be used.

Type: jQuery Object
Default: null

####hideParentOverflow####

A boolean determining if the parent should hide its overflow.

Type: Boolean
Default: true

####fadeInDuration####

A duration in milliseconds determining how long the fadeIn animation will run when your image is scale for the firstTime.

Set it to 0 if you don't want any animation.

Type: Number or String
Default: 0

####rescaleOnResize####

A boolean indicating if the image size should be rescaled when the window is resized.

The window size is checked using requestAnimationFrame for good performance.

Type: Boolean
Default: true

####callback####

A function that will be call once the image has been load and scale.

Must be either a function or an object. If an object, it must has a target (an object) and an action (the name of the method in the target) as property.

Here is an example:

$images.imageScale({ 
  callback: {
    target: this,
    action: 'didScale'
  }
});
Type: Function or Object
Default: null

####debug####

A number indicating the debug level :

  1. error
  2. error & warning
  3. error & warning & notice
Type: Number
Default: 0

##Methods##

####scale####

Main method. Used to scale the images.

When rescaleOnResize is set to true, this method is executed each time the windows size changes.

If rescaleOnResize is set to false, you may want to call it manually. Here is an example on how you should do it:

$image.imageScale('scale');

####destroy####

Removes the data for the element.

Here is an example on how you can call the destroy method:

$image.imageScale('destroy');

Demo

See it in action on our home page.

You can also check out the Sproutcore Automatic Image Scaling demo to understand the difference between all the different options.

Size

Original Size: 3.33KB gzipped (10.43KB uncompressed)

Compiled Size: 1.6KB gzipped (3.94KB uncompressed)

image-scale's People

Contributors

nicolasbadia avatar

Watchers

James Cloos avatar Alban DOUSSAU de GUYONNET avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.