图片自适应手机横屏竖屏的宽高

本文详细介绍了如何使用JavaScript确保轮播图片在手机横屏和竖屏状态下都能自适应屏幕大小,通过动态调整图片宽度和高度,实现了良好的用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

由于某些原因,图片的宽度不能设置成自适应的,想要轮播图片自适应手机横屏竖屏,只能用js来控制图片的大小了。

 

<div id="mgg" style="width: ${ width }px;">
	<ul class="tag-pic" style="width: 100000px;">
		<c:forEach var="ad" items="${ adList }" >
			<li>
				<img width="${width }" height="${height }" src="${ ad.url }">
			</li>
		</c:forEach>
	</ul>
</div>

 

<script type="text/javascript">
//默认值
Mgg = {
	setWidth : '${width }',
	setHeigth : '${height }',
	step : $(document).width() == '' ? 324 : parseInt($(document).width())+4,
	imgs : 3		//图片的数量
}; 

Mgg.changeSize() = function(){
	//取当前屏幕宽度
	var screenWidth = $(document).width();
	Mgg.step = screenWidth == '' ? 324 : parseInt(screenWidth)+4;
	//计算高度
	var height = (Mgg.setHeigth / Mgg.setWidth) * screenWidth;
	$('#mgg').css("width", screenWidth);
	$('#mgg').css("height", height);
	$('#mgg .tag-pic').width((Mgg.imgs + 2)*Mgg.step);
	$('#mgg .tag-pic img').each(function(){
		$(this).attr("width", screenWidth);
		$(this).attr("height", height);
	});
};

$(window).resize(function(){
	Mgg.changeSize();
});

$(document).ready(function(){
	//图片尺寸
	Mgg.changeSize();
}); 
</script>	

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值