根据屏幕分辨率设置背景图片
我希望能够根据用户使用的屏幕分辨率更改我的网页背景图像:
如果屏幕分辨率大于或等于 1200*600,则背景 = mybackground.jpg no-repeat 或其他。我该怎么做?
I would like to be able to change my webpage background image according to the screen resolution the user uses so:
if screen resolution is greater than or equal to 1200*600 then background = mybackground.jpg no-repeat or else. How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
此处讨论了效果很好的纯 CSS 方法。特别研究了两种技术,我个人更喜欢第二种,因为它不依赖 CSS3,更适合我自己的需求。
如果您的大部分/所有流量都有支持 CSS3 的浏览器,则第一种方法实施起来更快、更干净(为方便起见,Zoidberg 先生在此处的另一个答案中复制/粘贴,尽管我会访问 来源 了解其工作原理的更多背景)。
CSS 的另一种方法是使用 JavaScript 库 jQuery 来检测分辨率变化并相应地调整图像大小。 本文涵盖jQuery 技术并提供现场演示。
Supersized 是一个专用的 JavaScript 库,专为静态全屏图像以及全尺寸幻灯片而设计。
对于全屏图像的一个好技巧是事先以正确的比例缩放它们。当使用 supersized.js 时,我通常的目标是 1500x1000 的大小,或者使用其他方法时的 1680x1050 的大小,将照片的 jpg 质量设置为 60-80% 之间,从而在不过多影响质量的情况下,将文件大小控制在 100kb 或更小。 。
Pure CSS approaches that work very well are discussed here. Two techniques are examined in particular and I personally prefer the second as it not CSS3 dependent, which suits my own needs better.
If most/all of your traffic has a CSS3 capable browser, the first method is quicker and cleaner to implement (copy/pasted by Mr. Zoidberg in another answer here for convenience, though I'd visit the source for further background on why it works).
An alternative method to CSS is to use the JavaScript library jQuery to detect resolution changes and adjust the image size accordingly. This article covers the jQuery technique and provides a live demo.
Supersized is a dedicated JavaScript library designed for static full screen images as well as full sized slideshows.
A good tip for full-screen images is to scale them with a correct ratio beforehand. I normally aim for a size of 1500x1000 when using supersized.js or 1680x1050 for other methods, setting the jpg quality for photographs to between 60-80% resulting in a file size in the region of 100kb or less if possible without compromising quality too much.
删除“身体背景图像代码”,然后粘贴此代码:
Delete your "body background image code" then paste this code:
您好,这是一个 javascript 版本,它根据屏幕分辨率更改背景图像 src。您必须以正确的尺寸保存不同的图像。
Hi heres a javascript version which changes the background image src according to screen resolution. You have to have the different images saved in the right size.
我知道这是一个太老的问题,但我想回答一下,它可能会对某人有所帮助。如果你看 twitter,你会发现一些非常棘手但纯 css 的方法来实现这一点。
此背景图像适合所有尺寸。甚至iPad的纵向视图。它总是在中心调整图像。如果你缩小;图像将保持不变。
I know it's too old question but thought to answer, it might will help someone. If you see twitter, you will find something very tricky but pure css approach to achieve this.
This background images fits to all size. even portrait view of ipad. it always adjust the image in center. if you zoom out; image will remain the same.
将正文 css 设置为:
}
Set body css to :
}
我也遇到过同样的问题,但现在找到了解决方案。
诀窍是创建1920*1200的壁纸图像。
然后,当您将此壁纸应用到不同的计算机时,Windows 7 会自动调整大小以使其最适合。
希望这对大家有帮助
I've had this same issue but have now found the resolution for it.
The trick is to create a wallpaper image of 1920*1200.
When you then apply this wallpaper to the different machines, Windows 7 automatically resizes for best fit.
Hope this helps you all
放入 css 文件:
URL images/bg.jpg 是您的背景图片
Put into css file:
URL images/bg.jpg is your background image