Giter Site home page Giter Site logo

dafuster / smart-cover-background Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 653 KB

Smart cover background will auto position your background cover image so that a focus point of the image that you designate will always be visible.

Home Page: http://smart-cover-background.meteor.com/

License: MIT License

HTML 24.40% CSS 10.14% JavaScript 65.46%

smart-cover-background's Introduction

smart-cover-background

With responsive web design as the standard today, background images with the background-size set to cover are frequently used when setting a background image to a container.

A common challenge with cover backgrounds is that one does not have full control of what the user is able to see in different responsive view sizes (mobile vs desktop).

Smart cover background fixes that problem by auto positioning your background cover image so that a focus point of the image that is designated will always be visible to the user.

Demo

[http://smart-background.meteor.com/] (http://smart-background.meteor.com/)

The demo was built with [Meteor] (https://www.meteor.com/).

To run it locally, you'll need Meteor installed, if you do not have it, run:

curl https://install.meteor.com/ | sh

Then navigate to demo directory and simply type:

meteor

Installation

bower install smart-background

Usage

Let's say you have an image with the dimensions of 800 width by 600 height and you want to focus the center of the image. Which translates to a 400 left and 300 top:

HTML

<div class="my-background"
  data-smart-background
  data-width="800"
  data-height="600"
  data-focus-left="400"
  data-focus-top="300">
</div>

CSS

.my-background {
  background-size: cover;
  background-image: url( 800x600-image.jpg );
}

JS

var bg = document.querySelector('[data-smart-background]'),
  smartBG = new SmartBackground(bg);

How it works

Typically when using a background image that is set to cover, the background image is stretched to fully cover the surface area of its container, without distorting the image (maintaining its aspect ratio). To do this, the browser figures out the shorter side of the image (width or height) and sets it to 100% of its container's shorter side (width or height).

Example:

  • An image has a width of 400px and height of 200px.
  • The container the image is set as a background in has a width 800px and height of 600px.

When the container is set to background-size: cover;

  • The image's height of 200px (the shorter side) is translated to equal 100% of the container's shorter side which in this case is 600px.
  • The width of the image is naturally translated to px which maintains the image's initial aspect ratio.
  • This allows for the image to fully cover its container.

Smart cover background starts by taking a focus point's position from the upper left corner of the image. This is collected in pixels as a left and top variables (see diagram).

It then calculates an offset of the background image background-position: left top; that will maintain the cover effect of the background but also ensure that the focus point designated is in the center of the container.

smart-cover-background's People

Contributors

dafuster avatar

Stargazers

T. Mjoen avatar Cris Noble avatar

Watchers

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