[英]background image change with screen size
My website's background is a huge image that covers the whole webpage, how can I make it so that, we detect user's screen size and change the background image accordingly?我的网站背景是一个覆盖整个网页的巨大图像,我怎样才能做到这一点,我们检测用户的屏幕尺寸并相应地更改背景图像?
Say for example, my background image is 1x1px, if user screen is 2x2px (this is just an example, nobody has this kind of small screen), I want stretch my background to fit 2x2.例如,我的背景图像是 1x1px,如果用户屏幕是 2x2px(这只是一个例子,没有人有这种小屏幕),我想拉伸我的背景以适应 2x2。 If user screen is say, 0.5x0.6px, then I want my background to be shown only its 0.5x0.6px part, not the whole 1x1px.
如果用户屏幕是 0.5x0.6px,那么我希望我的背景只显示它的 0.5x0.6px 部分,而不是整个 1x1px。
Use an image tag as the background give it a 100%
for width
and height
, and set it behind all of the content with z-index
give it absolute
positioning and set its top
and left
where you need it to be.使用图像标签作为背景,为其设置
100%
的width
和height
,并将其设置在所有内容的后面,使用z-index
为其提供absolute
定位,并将其top
和left
设置在您需要的位置。
<img src="yourimage" width="100%" height="100%" style="z-index:0"/>
In CSS3, we have a new property called background-size
:在 CSS3 中,我们有一个名为
background-size
的新属性:
body {
background-size: 100%;
}
looking for this?找这个?
<div style="width:100%; z-index:0; height:100%; background: url(1pximage.gif)"></div>
You can detect the users screen size and do it, also you can attach a event handler to do the same when the window size is modified.您可以检测用户的屏幕大小并执行此操作,也可以在修改 window 大小时附加事件处理程序来执行相同操作。
$(function(){
$(window).resize(function(){
var windowW = $(window).width();
var windowH = $(window).height();
//Using above variables you can deside the size of the background image to be set
}).resize();//Trigger the resize event on page load.
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.