javascript调整背景图像大小

发布于 2024-09-02 09:45:36 字数 321 浏览 1 评论 0原文

是否可以使用 javascript 在加载时调整背景图像的大小?我不关心根据窗口大小或任何内容动态调整图像大小,我只想拍摄大图像并将其调整为特定的宽度和高度,以便完整图像适合特定的布局。

解决方法:

我做了下面的人所说的,并使用了一个常规标签,我相应地调整了大小并绝对定位在包含的 div 中。然后我使用 z-index 属性将其推到后台。

img.background {
    position: absolute;
    top: 0;
    left: 0;
    width: 960px;
    z-index: 0;
}

Is it possible to resize a background image on load using javascript? I don't care about dynamically resizing the image according to window size or anything, I just want to take large images and resize them to a specific width and height so that the full image fits inside a specific layout.

WORK AROUND:

I did what the people below said and used a regular tag that I sized accordingly and positioned absolutely in the containing div. I then used the z-index property to push it to the background.

img.background {
    position: absolute;
    top: 0;
    left: 0;
    width: 960px;
    z-index: 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

↘紸啶 2024-09-09 09:45:36

此时不可能 [可靠地,x-browser] 以任何方式缩放背景图像,但它可以在 CSS3 中使用 (spec)所以未来存在希望。

如果您想缩放,请使用实际值,在这种情况下,当然您可以随时调整大小。

It's not possible to [reliably, x-browser] scale a background image in any way at this point, but it is available in CSS3 (spec) so hope exists for the future.

Use an actual if you want to scale, in which case yes of course you can resize whenever you wish.

怀念你的温柔 2024-09-09 09:45:36

背景图像无法调整大小。

最好的选择是将 position:absolute 一起使用

Background images cannot be resized.

Your best call is to use an <img> with position:absolute

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文