author | Pierre-Yves David <pierre-yves.david@logilab.fr> |
Mon, 25 Mar 2013 15:28:18 +0100 | |
changeset 8787 | 1b3b7284377f |
parent 6215 | 759cf097f5aa |
permissions | -rw-r--r-- |
6004
d17d3b34bc12
[image] more handy image resizing using javascript
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
1 |
jQuery.fn.autoResize = function() { |
d17d3b34bc12
[image] more handy image resizing using javascript
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
2 |
// 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
|
3 |
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
|
4 |
this.removeAttr("width").removeAttr("height"); // Remove |
d17d3b34bc12
[image] more handy image resizing using javascript
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
5 |
// 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
|
6 |
var imgHSize = this.width(); |
d17d3b34bc12
[image] more handy image resizing using javascript
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
7 |
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
|
8 |
var imgVSize = this.height(); |
6010
9d40ee3d0551
[image js] fix image fit screen function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6004
diff
changeset
|
9 |
// 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
|
10 |
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
|
11 |
if (maxHSize > 0 && maxVSize > 0) { |
6215
759cf097f5aa
[javascript] get rid of tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6010
diff
changeset
|
12 |
// if image don't fit screen, set width or height so that |
759cf097f5aa
[javascript] get rid of tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6010
diff
changeset
|
13 |
// browser keep img ratio, ensuring the other dimension will |
759cf097f5aa
[javascript] get rid of tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6010
diff
changeset
|
14 |
// also fit the screen |
759cf097f5aa
[javascript] get rid of tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6010
diff
changeset
|
15 |
if (imgHSize > maxHSize && ((imgVSize / imgHSize) * maxHSize) <= maxVSize) { |
759cf097f5aa
[javascript] get rid of tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6010
diff
changeset
|
16 |
this.css("width", maxHSize); |
759cf097f5aa
[javascript] get rid of tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6010
diff
changeset
|
17 |
} else if (imgVSize > maxVSize && ((imgHSize / imgVSize) * maxVSize) <= maxHSize) { |
759cf097f5aa
[javascript] get rid of tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6010
diff
changeset
|
18 |
this.css("height", maxVSize); |
759cf097f5aa
[javascript] get rid of tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6010
diff
changeset
|
19 |
} |
759cf097f5aa
[javascript] get rid of tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6010
diff
changeset
|
20 |
else { |
759cf097f5aa
[javascript] get rid of tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6010
diff
changeset
|
21 |
// image already fit in screen, don't scale it up |
759cf097f5aa
[javascript] get rid of tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6010
diff
changeset
|
22 |
} |
6004
d17d3b34bc12
[image] more handy image resizing using javascript
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
23 |
} else { |
6215
759cf097f5aa
[javascript] get rid of tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6010
diff
changeset
|
24 |
// 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
|
25 |
} |
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 |
$(document).ready(function() { |
6215
759cf097f5aa
[javascript] get rid of tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6010
diff
changeset
|
30 |
$("img.contentimage").load(function() {$(this).autoResize()}); |
759cf097f5aa
[javascript] get rid of tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6010
diff
changeset
|
31 |
}); |