gpt4 book ai didi

javascript - jQuery 调整图像大小

转载 作者:行者123 更新时间:2023-11-28 09:20:51 25 4
gpt4 key购买 nike

我已经成功地自动调整每行图库的图像,具体取决于它是水平的(每行一个图像)还是垂直的(每行两个图像)。

现在的问题是我希望图像是可缩放的(在窗口大小调整时调整大小),但我不知道如何实现它。我该怎么做? (我正在寻找 javascript/jquery 解决方案来避免高度:自动问题...)

这是网址:http://ldlocal.web44.net/test2/gallery.html

可以在这里下载:http://ldlocal.web44.net/test2/test.zip

这是我的代码:

var gallery = new Gallery($('#gallery_images_inner'));

function Gallery(selector){

this.add_module = function(type, image){
var container = $('<div />' , {
'class' : 'gallery_container'
}).append(image);
if(type == 'horizontal'){
var h_ar = image.attr('height') / image.attr('width');
container.css({
'width' : selector.width(),
'height' : selector.width()*h_ar
})
}
if(type == 'vertical'){
container.css({
'width' : v_width,
'height' : v_height
})
}
container.appendTo(selector);
container.children('img').fitToBox();
}
var _this = this;
var gutter = 0;
// start vars for counting on vertical images
var v_counter = 0;
var w_pxls = 0;
var h_pxls = 0;
// iterates through images looking for verticals
selector.children('img').each(function(){
if($(this).attr('width') < $(this).attr('height')){
v_counter++;
h_pxls += $(this).attr('height');
w_pxls += $(this).attr('width');
}
})
// calculates average ar for vertical images (anything outside from aspect ratio will be croped)
var h_avrg = Math.floor(h_pxls/v_counter);
var w_avrg = Math.floor(w_pxls/v_counter);
var v_ar = h_avrg/w_avrg;
var v_width = (selector.width())/2;
var v_height = v_width*v_ar;
selector.children('img').each(function(){
if(parseInt($(this).attr('width')) > parseInt($(this).attr('height'))){
_this.add_module('horizontal', $(this));
}else{
_this.add_module('vertical', $(this));
}
})
selector.isotope({
masonry: {
columnWidth: selector.width() / 2
}
});

}

最佳答案

更新所有新代码:

http://jsfiddle.net/vYGGN/

HTML:

<div id="content">
<img class="fluidimage" src="http://thedoghousediaries.com/comics/uncategorized/2011-04-06-1b32832.png"
/>
</div>

CSS:

        body {
text-align:center;
}
img {
float: right;
margin: 0px 10px 10px 10px;
}
#content {
width:70%;
margin: 0px auto;
text-align: left;
}

JS:

        $(document).ready(function () {
function imageresize() {
var contentwidth = $('#content').width();
if ((contentwidth) < '300') {
$('.fluidimage').attr('height', '300px');
} else {
$('.fluidimage').attr('height', '600px');
}
}

imageresize(); //Triggers when document first loads

$(window).bind("resize", function () { //Adjusts image when browser resized
imageresize();
});

});

在这篇文章中找到了这一点:

http://buildinternet.com/2009/07/quick-tip-resizing-images-based-on-browser-window-size/

关于javascript - jQuery 调整图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15011442/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com