Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show a placeholder-image before the real image is downloaded?

Tags:

angularjs

The idea is to show a low-res version of an image before the real high-resolution image has been downloaded, ideally using an img tag.

<img lowres="http://localhost/low-res-image.jpg" ng-src="http://localhost/low-res-image.jpg">

Low-res image would be visible first, and be replaced with high-res image after it has been downloaded. How can this be done? Is it possible to edit img.src attribute, or should something else (e.g. a wrapper div with background, or another temporary div) be created?

like image 715
Mika Vatanen Avatar asked Apr 01 '14 16:04

Mika Vatanen


People also ask

How do I show placeholder images in react native?

import { StyleSheet, View, Image, Button } from 'react-native' 4. Create a State named as loadingImage and set its default value as False. This State is used to show default image before loading real image and if the image loaded successfully it will display us the real image.

How do you put a placeholder on a picture?

Whenever you load large image on the page and it's taken too much time, that time you can show loading image or you can say Placeholder image ( Html Image Placeholder ) within the same image tag. You can do this simply by using CSS. Use background property of css and set url of an loading image.

Can a placeholder be an image?

A placeholder image can communicate what type of image is intended to be shown in a particular place, and how it should layout, resize, and flow within the browser.

What is placeholder picture?

An image placeholder is a dummy image designed to draw attention to the need for an actual image. Wikipedia image placeholders were meant to be used on articles, especially those of living people, for the purpose of trying to obtain a freely-licensed image for them.


2 Answers

You'd probably want to create a directive, I'd actually have hires as the attribute, so that by default it starts with lores.

JS:

app.directive('hires', function() {
  return {
    restrict: 'A',
    scope: { hires: '@' },
    link: function(scope, element, attrs) {
        element.one('load', function() {
            element.attr('src', scope.hires);
        });
    }
  };
});

HTML:

<img hires="http://localhost/hi-res-image.jpg" src="http://localhost/low-res-image.jpg" />
like image 200
dave Avatar answered Oct 21 '22 05:10

dave


Because ng-src is going to replace whatever is in src why not use both?

<img src="img/placeholder.jpg" ng-src="{{actualone.img}} "  height="150px " width="300px ">

Use normal src for your placeholder. It will get replaced one the image for ng-src is ready.

like image 25
Kyle Pennell Avatar answered Oct 21 '22 04:10

Kyle Pennell