根据浏览器大小调整图片大小可以通过以下几种方式实现:
@media screen and (max-width: 768px) {
.image {
width: 100%;
height: auto;
}
}
@media screen and (min-width: 769px) and (max-width: 1024px) {
.image {
width: 50%;
height: auto;
}
}
@media screen and (min-width: 1025px) {
.image {
width: 30%;
height: auto;
}
}
window.addEventListener('resize', function() {
var image = document.getElementById('image');
var windowWidth = window.innerWidth;
if (windowWidth < 768) {
image.style.width = '100%';
image.style.height = 'auto';
} else if (windowWidth >= 768 && windowWidth < 1024) {
image.style.width = '50%';
image.style.height = 'auto';
} else {
image.style.width = '30%';
image.style.height = 'auto';
}
});
<picture>
元素和<source>
元素来提供不同尺寸的图片,浏览器会根据窗口大小选择合适的图片进行显示。例如:<picture>
<source media="(max-width: 768px)" srcset="small.jpg">
<source media="(min-width: 769px) and (max-width: 1024px)" srcset="medium.jpg">
<source media="(min-width: 1025px)" srcset="large.jpg">
<img src="default.jpg" alt="Image">
</picture>
以上是根据浏览器大小调整图片大小的几种常见方法。根据具体的需求和场景,可以选择适合的方法来实现。腾讯云提供的相关产品和服务可以参考腾讯云官方文档:腾讯云产品与服务。
领取专属 10元无门槛券
手把手带您无忧上云