我有一个在div id为"myimage"下的图片。现在我想通过使用change来调整它的大小。如果选择了700X7000的选项,那么图片的尺寸将是高度700像素和宽度700像素。不需要重新加载页面。 可以有人帮我如何实现这个?
我有一个在div id为"myimage"下的图片。现在我想通过使用change来调整它的大小。如果选择了700X7000的选项,那么图片的尺寸将是高度700像素和宽度700像素。不需要重新加载页面。 可以有人帮我如何实现这个?
要改变图像的大小,只需设置图像的 width()
和 height()
:
$('#myimage').width(700); // Units are assumed to be pixels
$('#myimage').height(700);
要创建下拉框,您只需将值设置为类似于100x100
,200x200
等,然后split
选定的值以提取尺寸:
var parts = '100x100'.split('x');
var width = parseInt(parts[0], 10); // 10 forces parseInt to use base 10
var height = parseInt(parts[1], 10);
if( condition ) {
$('div#myimage img').css({'width' : '700px' , 'height' : '700px'});
}
else {
$('div#myimage img').css({'width' : '150px' , 'height' : '150px'});
};
// First way
$('#checkBox').attr('checked');
// Second way
$('#checkBox').is(':checked');
示例工作:
if( $('#checkBox').attr('checked') ) {
$('div#myimage img').css({'width' : '700px' , 'height' : '700px'});
}
else {
$('div#myimage img').css({'width' : '150px' , 'height' : '150px'});
};
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function () {
$("#photograph").click(function () {
$("img").animate({
height: '+=5px',
width: '+=5px'
});
});
});
}
</script>
</head>
<body>
<div>
<img src="photo/grass.jpg" id="photograph" style="width:304px;height:228px;"/>
</div>
</body>
</html>
写
max-width:100%
在 img 标签中
或者
函数发送对象
<img onLoad="ozhpixel(this)"
function ozhpixel(imaj){
$(imaj).width(100); // Units are assumed to be pixels
$(imaj).height(50);}