如何使用JavaScript将图像缩放到全屏?
我有一个图像,我想缩放到全屏。如何使用JavaScript/jQuery实现这一点?不需要动画,只需调整图像大小如何使用JavaScript将图像缩放到全屏?,javascript,image,image-resizing,Javascript,Image,Image Resizing,我有一个图像,我想缩放到全屏。如何使用JavaScript/jQuery实现这一点?不需要动画,只需调整图像大小 <img src="something.jpg" onload="scaleToFullScreen()" /> 如果在所有元素之外都有css,则只需使用css即可: 如果您没有: <img name='myImage' src="something.jpg" onload="onResize()" /> <script> window.onresize
<img src="something.jpg" onload="scaleToFullScreen()" />
如果在所有元素之外都有css,则只需使用css即可:
如果您没有:
<img name='myImage' src="something.jpg" onload="onResize()" />
<script>
window.onresize = onResize;
function onResize(e){
var img = var img = document.images.myImage;
if ( img.width > img.height )
img.width = window.innerWidth
else
img.height = window.innerHeight;
}
</script>
window.onresize=onresize;
函数onResize(e){
var img=var img=document.images.myImage;
如果(img.宽度>img.高度)
img.width=window.innerWidth
其他的
img.height=window.innerHeight;
}
唯一可靠的解决方案是使用:
您可以使用
viewerjs
,这是一个perfict库,用户可以浏览您的图像以查看全页和全屏,滑动它们并在它们之间查看幻灯片
检查此链接:
全屏还是全窗口?这是不相称的。我需要“缩放”它,而不是倾斜它。@Andrew因此,如果窗口的纵横比不同,它应该更新吗。只有改变宽度才会按比例改变高度。如果图像的高度高于宽度,我认为这不起作用。这将是一种效果,因此我尝试了这种方法,但它没有保持纵横比:(我得到最大高度,但宽图像被挤压:(
var $img = $('#content img'),
imageWidth = $img[0].width, //need the raw width due to a jquery bug that affects chrome
imageHeight = $img[0].height, //need the raw height due to a jquery bug that affects chrome
maxWidth = $(window).width(),
maxHeight = $(window).height(),
widthRatio = maxWidth / imageWidth,
heightRatio = maxHeight / imageHeight;
var ratio = widthRatio; //default to the width ratio until proven wrong
if (widthRatio * imageHeight > maxHeight) {
ratio = heightRatio;
}
//now resize the image relative to the ratio
$img.attr('width', imageWidth * ratio)
.attr('height', imageHeight * ratio);
//and center the image vertically and horizontally
$img.css({
margin: 'auto',
position: 'absolute',
top: 0,
bottom: 0,
left: 0,
right: 0
});