简体   繁体   English

javascript / CSS 图片宽度=浏览器高度

[英]javascript / CSS picture width = browser height

Hello and sorry for the weird title.你好,很抱歉这个奇怪的标题。 On my website, I want to have a header image, of exactly the same height as the browser.在我的网站上,我想要一个与浏览器高度完全相同的 header 图像。 So when you open the website you should see a picture, which covers all of the screen but isn't a background image.因此,当您打开网站时,您应该会看到一张图片,它覆盖了整个屏幕,但不是背景图片。 This way you should be able to move away from the image if you scroll down.这样,如果向下滚动,您应该能够远离图像。 The problem is, that if I say "browser height = pixel height", the format of the picture will change.问题是,如果我说“浏览器高度 = 像素高度”,图片的格式就会改变。

Because of that, I need to have a function, that automatically changes the width of the picture to the height of the browser or rather the amount of vertical pixels your browser has.因此,我需要一个 function,它会自动将图片的宽度更改为浏览器的高度,或者更确切地说是浏览器的垂直像素数量。 If you scroll for example the picture shouldn't change.例如,如果您滚动图片,则不应更改。

Until now I have only tried CSS because I have no experience with javascript.到目前为止,我只尝试过 CSS,因为我没有使用 javascript 的经验。 After visiting many websites I still haven't found a solution to this problem.在访问了许多网站后,我仍然没有找到解决此问题的方法。 Here you can see what the website should look like on pc and a mobile device.在这里,您可以看到网站在 PC 和移动设备上的外观。

这个网站只包含简单的 HTML 和 css

If I however try to look at the mobile version, it looks like this, even though it should look like the first picture.但是,如果我尝试查看移动版本,它看起来像这样,即使它应该看起来像第一张图片。移动视图

Here you can see an example of a website where it is perfectly how I want it.在这里,您可以看到一个完全符合我要求的网站示例。 There you can see that the picture covers the entire screen height and doesn't change if you scroll (The only difference is, that I don't want the picture to cover all of the screen (left and right). ( https://restaurant-am-bertasee.eatbu.com/?lang=de )在那里你可以看到图片覆盖了整个屏幕高度并且如果你滚动不会改变(唯一的区别是,我不希望图片覆盖整个屏幕(左右)。( https:/ /restaurant-am-bertasee.eatbu.com/?lang=de )

Thank you very much, any help is appreciated.非常感谢,任何帮助表示赞赏。

Did you try to add media only attribute to your css?您是否尝试将仅媒体属性添加到 css? I used below max width 428 px because that is mainly on iPhone 13 but you can change it if you want.我使用的最大宽度低于 428 像素,因为这主要用于 iPhone 13,但您可以根据需要更改它。

.imageexample { .imageexample {

display: block;显示:块;

width:428px;宽度:428px;

max-width: 100%;最大宽度:100%;

height: auto;高度:自动;

margin: 0 auto;边距:0 自动;

} }

.box { 。盒子 {

display:inline-block;显示:内联块;

width:100%;宽度:100%;

text-align:center;文本对齐:居中;

} }

@media only screen and (max-width: 428px) @media only screen and (max-width: 428px)

{ imgeexample { 图像示例

{ {

width:100%;宽度:100%;

} }

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM