4

I would like to scale a large image's height/width using AngularJS so that it fits the screen on multiple devices (responsive). This website is an example of what I am trying to accomplish.

Does angular have a directive that can handle this or will I need to create a directive or factory to accomplish this task? Has anyone tried WURFL Image Tailor (WIT)?

1
  • How is this related to Angular..its only CSS Commented Apr 1, 2015 at 5:03

2 Answers 2

6

In most cases, you don't need angularjs at all. This can be accomplished with css alone, using the background-size property.

For example, in order to scale to width:

.bg {
  background-size: 100% auto;
}

and to scale to height:

.bg {
  background-size: auto 100%;
}
Sign up to request clarification or add additional context in comments.

3 Comments

The issue with doing this in pure CSS is you will then be forcing the User to download an image that is much larger that what is necessary to display.
@ElliotFehr can you direct me to an article or jsfiddle that explains how i can accomplish this with javascript and css or pure javascript?
@user3574939 You will ideally want to use a combination of both. There is nothing wrong with setting the size constraints with CSS. If you append your image URL with WURFL Image Tailor they have the option to dynamically serve a resized image that is no larger than what the client device can display. github.com/WURFL/angular-wurfl-image-tailor
1

The website you linked to uses the background-image trick: instead of resizing the image, it uses an image as the background of a div instead, then it applies the appropriate styles to the div:

height: 840px;
background-image: url('pathToImage');
background-size: cover;
background-position: center center;

This is achieved using CSS only - no need for Angular or even JavaScript.

WURFL Image Tailor does something different - it actually serves a different sized image (image size and file size) based on screen width. This is useful when you don't want browsers on smaller screens to download images in their original resolution and size. (The assumption is that smaller screens mean mobile and mobile means slower connections.)

2 Comments

I took a peek at the developer's jquery plugin an it had the below in a function so i wanted to do something similar using a angular directive, but it appears the developer is only referencing the path to his image and then loading them into a div and then using css to make it responsive. 'code' var myUrl = "url(salleedesign.com/assets" + mypages[ myindex ] + "/photo-top.jpg)"; var myUrlShort = "salleedesign.com/assets" + mypages[ myindex ] + "/photo-top.jpg"; 'code'
That code snippet only determines which image to use as a background for the div. As for how to make the image/div responsive, that is achieved using css only.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.