web/data/cubicweb.image.js
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Mon, 26 Jul 2010 12:06:58 +0200
branchstable
changeset 6010 9d40ee3d0551
parent 6004 d17d3b34bc12
child 6215 759cf097f5aa
permissions -rw-r--r--
[image js] fix image fit screen function
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6004
d17d3b34bc12 [image] more handy image resizing using javascript
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     1
d17d3b34bc12 [image] more handy image resizing using javascript
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     2
jQuery.fn.autoResize = function() {
d17d3b34bc12 [image] more handy image resizing using javascript
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     3
    // remove enforced with / height (by CSS and/or HTML attributes)
d17d3b34bc12 [image] more handy image resizing using javascript
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     4
    this.css("width", "auto").css("height", "auto");
d17d3b34bc12 [image] more handy image resizing using javascript
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     5
    this.removeAttr("width").removeAttr("height"); // Remove
d17d3b34bc12 [image] more handy image resizing using javascript
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     6
    // compute image size / max allowed size to fit screen
d17d3b34bc12 [image] more handy image resizing using javascript
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     7
    var imgHSize = this.width();
d17d3b34bc12 [image] more handy image resizing using javascript
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     8
    var maxHSize = $(window).width() - ($(document).width() - imgHSize);
d17d3b34bc12 [image] more handy image resizing using javascript
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     9
    var imgVSize = this.height();
6010
9d40ee3d0551 [image js] fix image fit screen function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6004
diff changeset
    10
    // we don't mind if content in [content]footer moved out of the screen
9d40ee3d0551 [image js] fix image fit screen function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6004
diff changeset
    11
    var maxVSize = $(window).height() - ($(document).height() - imgVSize) + $('#footer').height() + $('#contentfooter').height();
6004
d17d3b34bc12 [image] more handy image resizing using javascript
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    12
    if (maxHSize > 0 && maxVSize > 0) {
d17d3b34bc12 [image] more handy image resizing using javascript
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    13
	// if image don't fit screen, set width or height so that
d17d3b34bc12 [image] more handy image resizing using javascript
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    14
	// browser keep img ratio, ensuring the other dimension will
d17d3b34bc12 [image] more handy image resizing using javascript
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    15
	// also fit the screen
6010
9d40ee3d0551 [image js] fix image fit screen function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6004
diff changeset
    16
	if (imgHSize > maxHSize && ((imgVSize / imgHSize) * maxHSize) <= maxVSize) {
6004
d17d3b34bc12 [image] more handy image resizing using javascript
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    17
	    this.css("width", maxHSize);
6010
9d40ee3d0551 [image js] fix image fit screen function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6004
diff changeset
    18
	} else if (imgVSize > maxVSize && ((imgHSize / imgVSize) * maxVSize) <= maxHSize) {
6004
d17d3b34bc12 [image] more handy image resizing using javascript
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    19
	    this.css("height", maxVSize);
6010
9d40ee3d0551 [image js] fix image fit screen function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6004
diff changeset
    20
	}
9d40ee3d0551 [image js] fix image fit screen function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6004
diff changeset
    21
	else {
9d40ee3d0551 [image js] fix image fit screen function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6004
diff changeset
    22
	    // image already fit in screen, don't scale it up
9d40ee3d0551 [image js] fix image fit screen function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6004
diff changeset
    23
	}
6004
d17d3b34bc12 [image] more handy image resizing using javascript
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    24
    } else {
6010
9d40ee3d0551 [image js] fix image fit screen function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6004
diff changeset
    25
	// can't fit image in, don't do anything
6004
d17d3b34bc12 [image] more handy image resizing using javascript
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    26
    }
d17d3b34bc12 [image] more handy image resizing using javascript
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    27
};
d17d3b34bc12 [image] more handy image resizing using javascript
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    28
d17d3b34bc12 [image] more handy image resizing using javascript
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    29
d17d3b34bc12 [image] more handy image resizing using javascript
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    30
$(document).ready(function() {
d17d3b34bc12 [image] more handy image resizing using javascript
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    31
	$("img.contentimage").load(function() {$(this).autoResize()});
d17d3b34bc12 [image] more handy image resizing using javascript
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    32
    });