Created
June 12, 2015 19:33
-
-
Save andronex/2581ee59dd22e29089a9 to your computer and use it in GitHub Desktop.
Resize picture for adaptive and responsive designs. JavaScript function works on the fly.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
function resizeImg(){ | |
$( ".slides li img" ).each(function( index, element ){ | |
$( element ).removeAttr("width") | |
.removeAttr("height") | |
.removeAttr("margin-top") | |
.removeAttr("margin-left") | |
.css({ width: "", height: "", "margin-top": "", "margin-left": "" }); | |
var height = $( element ).height(); | |
var width = $( element ).width(); | |
var iw = $("#center").width(); | |
if ($(window).height() - height > 0){ | |
$( element ).css("width", width + "px"); | |
if (iw - width > 0) | |
$( element ).css("margin-left", (iw - width)/2+ "px"); | |
else if (iw - width < 0){ | |
// она слишком широкая... | |
height = height * iw/width; | |
width = iw; | |
$( element ).css("width", width + "px"); | |
$( element ).css("height",height+ "px"); | |
} | |
$( element ).css("margin-top",(($(window).height() - height ) / 2) + $(window).scrollTop() + "px"); | |
}else{ | |
width = width * $(window).height()/height; | |
$( element ).css("height", $(window).height()+ "px"); | |
$( element ).css("width", width + "px"); | |
if (iw - width > 0) | |
$( element ).css("margin-left", (iw - width)/2+ "px"); | |
else if (iw - width < 0){ | |
// она слишком широкая... | |
height = height * iw/width; | |
width = iw; | |
$( element ).css("width", width + "px"); | |
//$( element ).css("height",height+ "px"); | |
$( element ).css("height","auto"); | |
$( element ).css("margin-top",(($(window).height() - $(element).height()) / 2) /*+ $(window).scrollTop()*/ + "px"); | |
} | |
} | |
}); | |
} | |
$( window ).resize(function() { | |
resizeImg(); | |
}); | |
$( document ).ready(function() { | |
//resizeImg(); | |
$('.flexslider').flexslider({ | |
animation: "slide", | |
controlNav: false, | |
start: function(){resizeImg()} | |
}); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment