Giter Site home page Giter Site logo

respio's Introduction

Respio

Pixel perfect responsive images on every device. THX to the Google cache ใ‚ท

  • Retina ready
  • Pixelated placeholder
  • Lazyloading
  • Works with images and background images

Version

2.3.0

Demo

Requires

  • [jQuery]

Pixel perfect responsive images

1. Markup

<img data-respio-img="http://dev.janwagner-design.de/respio/image.jpg">

We need to remove the src attribute of your image and replace it by a new custom data attribute to prevent it from loading. Your images should have a max-width of 100% and should also be wider than your content/container.

2. Google Proxy Magic (THX to @coolaj86)

function googleProxy(width, refresh, url) {
    return 'https://images1-focus-opensocial.googleusercontent.com/gadgets/proxy'
    + '?container=focus'
    + '&refresh=' + refresh
    + '&resize_w=' + width
    + '&url=' + url
    ;
}

3. Script

$(window).on('load', respio);
$(window).on('scroll', respio);

function googleProxy(width, refresh, url) {
    return 'https://images1-focus-opensocial.googleusercontent.com/gadgets/proxy'
    + '?container=focus'
    + '&refresh=' + refresh
    + '&resize_w=' + width
    + '&url=' + url
    ;
}

function respio() {

    var windowBottom = $(window).scrollTop() + $(window).height();

    $.each($('img[data-respio-src]'), function(i, img) {
        var $img = $(img);

        if($img.attr('src', '')) {
            var thumbUrl = googleProxy(10, 604800, $img.data('respio-src'));
            $img.attr('src', thumbUrl);
        }

        setTimeout(function() {
            var imgTop = $img.offset().top;

            if(imgTop >= windowBottom * 1.5) { return; }

            var devicePixelRatio = (window.devicePixelRatio > 1) ? window.devicePixelRatio : 1;
            var imgWidth = $img.parent().width() * devicePixelRatio;

            var originUrl = $img.data('respio-src');
            var originImgUrl = googleProxy(imgWidth, 604800, originUrl);
            var $originImg = $('<img/>').hide().appendTo($('body'));

            var isLoaded = false;
            var isResized = false;

            var originImg = $originImg.get(0);
            originImg.onload = function() { isLoaded = true; };
            originImg.src = originImgUrl;

            var waitInterval = setInterval(function() {
                if (isLoaded) {
                    clearInterval(waitInterval);
                    $img.attr('src', originImgUrl);
                    $originImg.remove();
                    $img.removeAttr('data-respio-src');
                    $img.css('height', '');
                }
            }, 0);
        }, 0);
    });

}

Pixel perfect background images

1. Markup (width and height required)

<div data-respio-bg="http://dev.janwagner-design.de/respio/image.jpg" width="xxx" height="xxx"></div>

2. Script

$(window).on('load', respio);
$(window).on('scroll', respio);

function googleProxy(width, refresh, url) {
    return 'https://images1-focus-opensocial.googleusercontent.com/gadgets/proxy'
    + '?container=focus'
    + '&refresh=' + refresh
    + '&resize_w=' + width
    + '&url=' + url
    ;
}

function respio() {

    var windowBottom = $(window).scrollTop() + $(window).height();

    $.each($('[data-respio-bg]'), function(i, img) {
        var $img = $(img);

        var thumbUrl = googleProxy(10, 604800, $img.data('respio-bg'));
        $img.css('background-image', 'url(' + thumbUrl + ')');

        setTimeout(function() {
            var imgTop = $img.offset().top;

            if(imgTop >= windowBottom * 1.5) { return; }

            var devicePixelRatio = (window.devicePixelRatio > 1) ? window.devicePixelRatio : 1;
            var imgWidth = $img.parent().width() * devicePixelRatio;

            var originUrl = $img.data('respio-bg');
            var originImgUrl = googleProxy(imgWidth, 604800, originUrl);
            var $originImg = $('<img/>').hide().appendTo($('body'));

            var isLoaded = false;
            var isResized = false;

            var originImg = $originImg.get(0);
            originImg.onload = function() { isLoaded = true; };
            originImg.src = originImgUrl;

            var waitInterval = setInterval(function() {
                if (isLoaded) {
                    clearInterval(waitInterval);
                    $img.css('background-image', 'url(' + originImgUrl + ')');
                    $originImg.remove();
                    $img.removeAttr('data-respio-bg');
                    $img.css('height', '');
                }
            }, 0);
        }, 0);
    });

}

respio's People

Contributors

janwagner avatar

Watchers

 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.