根据设备方向更改背景位置

Renz Potpot Sugue

谁能告诉我如何实现这个技巧,在肖像上使用智能手机时,您会在网页上获得此背景:

肖像你在你的网页上得到这个背景

然后在横向模式下,图像将具有更宽的外观,如下所示:

然后在横向模式下,图像将具有更宽的外观,如下所示

治安官德里克

为了彻底,有很多方法可以做到这一点。在设置 img 源的某些情况下,JavaScript 可能更有用。也就是说,在大多数情况下,您会想要使用 CSS。您可以在此处阅读所有 CSS 选项:

https://developer.mozilla.org/en-US/docs/Web/CSS/@media

body {
  background: url(https://placehold.it/500x1000);
  background-size: cover;
  background-position: center center;
}

@media screen and (orientation: landscape) {
  body {
    background: url(https://placehold.it/1000x2000);
  }
}

@media screen and (min-width: 1200px) {
  body {
    background: url(https://placehold.it/2000x3000);
  }
}
<!-- body tag is included by default... -->

<!-- hey StackSnippet warriors(trolls)... key example here of a bad time to use it -->

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章