Coder Social home page Coder Social logo

2braincells2go / fitframe.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from benfoster/fitframe.js

0.0 3.0 0.0 69 KB

A lightweight, easy-to-use jQuery plugin for responsive iframes.

License: MIT License

HTML 46.92% CSS 1.87% JavaScript 51.22%

fitframe.js's Introduction

FitFrame.js

A lightweight, easy-to-use jQuery plugin for responsive iframes.

Setup

FitFrame.js requires jQuery. It has only been tested with jQuery 1.11 so if you have problems with any other versions let me know.

You can install via bower

bower install fitframe --save

If you're using mode: wrap (the default) you'll want to reference fitframe.css:

<link rel="stylesheet" href="fitframe.css" />

And of course you'll need to reference jQuery and FitFrame.js:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="fitframe.js"></script>

Usage (wrap mode)

The default mode of FitFrame.js is wrap and uses intrinsic ratios to make the iframes responsive.

By wrapping the iframe element in a div with height: 0 and padding: X (where X is the ratio between the iframe height and width) we can create a responsive iframe that maintains aspect ratio as its container resizes. So for a 16:9 video the container elements gets a height of 56.25% of it's width ((9/16) * 100). Clever stuff I know!

This is the recommended mode since no JavaScript is actually executed when the viewport size changes.

To initialize FitFrame.js just call fitFrame() on your container element:

$('.container').fitFrame();

Adding elements at runtime

If you're adding DOM elements to your container at runtime you have two options.

  1. Just call the update function to have FitFrame.js search for any new iframes that have been added and make them responsive:

     $('.container').fitFrame('update');
    
  2. If you want to wrap the iframe before you add it to the DOM you can use the wrap function, passing the iframe object to wrap:

     var wrapped = $('.container').fitFrame('wrap', $(iframeHtml));      
     $('.add-embed').before(wrapped);
    

You can see an example of this in demos/demo-wrap.html that uses embed.ly to dynamically load videos using a URL. Note that you'll need to set your embedly API key in the code.

Usage (resize mode)

The resize mode binds to the window resize event and sets the size of the iframe element to fit the container. No wrapper element is used and you don't need to add any additional CSS.

If you just want to fit your iframes to the width of the container then you are best off using the wrap mode. That said, with the use of CSS transitions we can create a more interesting experience when resizing the browser. You can see an example of this in demos/demo-resize-width.html. To use resize mode:

$('.container').fitFrame({ mode: 'resize' });

Adding elements at runtime

Resize mode also supports adding elements at runtime and again you can do these for all new iframes or individually:

  1. Resize all new iframes

     $('.container').fitFrame('update');
    
  2. Resize an individual element before adding it to the DOM:

     var resized = $('.container').fitFrame('resize', $(iframeHtml));      
     $('.add-embed').before(resized);
    

Fit iframe to container height

This was the original reason for creating the plugin since I needed to make an iframe always fit the height of its container (without any awful black bars!).

To do this, set the fitHeight option to true:

$('.container').fitFrame({
 mode: 'resize',
 fitHeight: true
});

You can see an example of this in demos/demo-resize-height.html.

Options

defaults = {
  wrapperCssClass: 'fitframe-wrap',
  iframeCssClass: 'fitframe',
  mode: 'wrap',
  fitHeight: false
};

Cleanup

Like all good citizens, FitFrame.js will cleanup after itself:

$('.container').fitFrame('destroy');

And can be safely re-initialized if required.

fitframe.js's People

Contributors

phil118 avatar benfoster avatar

Watchers

James Cloos avatar Woody avatar  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.