Javascript Detect Screen Resolution, change css, crop images accordingly

五迷三道 提交于 2019-12-24 09:45:17

问题


So I know how to change the css depending on the resolution via javascript.

How would one go about 'cropping' an image depending on the screen resolution?


回答1:


Well you can get the screen details from window.screen - though personally I would recommend just finding out how big the current window is, the only reason not to is if you are going to resize the window and that is very frowned upon.

Once you know the sizes and how big you need to make your images, I find that images are cropped easiest by placing them inside a containing DIV with overflow: hidden; set. You can then size the containing DIV to the size required and set the top and left CSS attributes od the image to the negative values of the coordinate you want for the top-left visible corner.

<div class="crop-container" style="width: 200px; height: 200px; overflow: hidden;">
    <img src="something-400x400.jpg" style="top: -100px; left: -100px;" width="400" height="400" alt="Something" />
</div>


来源:https://stackoverflow.com/questions/4272786/javascript-detect-screen-resolution-change-css-crop-images-accordingly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!