如何防止图片滚动?

huangapple go评论119阅读模式
英文:

How to keep pictures from scrolling?

问题

I put a 1920 * 1280 picture in the HTML, and I want the picture to fill the entire screen. And the image is fixed on the screen, and will not change position with the sliding of the mouse.

.mainpage {
  overflow-y: hidden;
  overflow-x: hidden;
}

I also try to use this code, but it doesn't work.

position: fixed;
top: 0;
left: 0;

Should I change the size of the picture, is there another way to stop scrolling?

英文:

I put a 1920 * 1280 picture in the HTML, and I want the picture to fill the entire screen. And the image is fixed on the screen, and will not change position with the sliding of the mouse.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.mainpage {
  overflow-y: hidden;
  overflow-x: hidden;
}

<!-- language: lang-html -->

&lt;a-assets&gt;
  &lt;img id=&quot;mainpage&quot; src=&quot;https://static.dezeen.com/uploads/2017/04/aia-honour-im-pei-grande-louvre-25-year_dezeen_2.jpg&quot;&gt;
&lt;/a-assets&gt;

<!-- end snippet -->

I also try to use this code, but it doesn't work.

    position: fixed;
    top: 0;
    left: 0;

Should I change the size of the picture, is there another way to stop scrolling?

答案1

得分: 1

你可以简单地使用以下代码来设置高度为100vh和宽度为100vw,而不是使用位置属性:

#mainpage {
  width: 100vw;
  height: 100vh;
  object-fit: cover;
}
<a-assets>
  <img id="mainpage" src="https://static.dezeen.com/uploads/2017/04/aia-honour-im-pei-grande-louvre-25-year_dezeen_2.jpg">
</a-assets>
英文:

Instead of using position you can simply set height 100 vh and width 100vw property like below:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

#mainpage {
  width: 100vw;
  height: 100vh;
  object-fit: cover;
}

<!-- language: lang-html -->

&lt;a-assets&gt;
  &lt;img id=&quot;mainpage&quot; src=&quot;https://static.dezeen.com/uploads/2017/04/aia-honour-im-pei-grande-louvre-25-year_dezeen_2.jpg&quot;&gt;
&lt;/a-assets&gt;

<!-- end snippet -->

答案2

得分: 0

为了使图像填充整个屏幕并保持固定位置,您可以使用以下CSS:

#mainpage {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

请注意,在HTML代码中,您应该使用ID选择器#mainpage,而不是类选择器.mainpage,因为您已将图像元素的ID设置为mainpage。

英文:

To make the image fill the entire screen and stay fixed in place, you can use the following CSS

With this code, the image stays in place even if the user scrolls the page. The top and left properties position the image at the top left corner of the screen. The width and height properties are set to 100%, so the image fills the entire screen. Lastly, the object-fit property set to cover scales the image to cover the entire element.

Note that you should use the ID selector #mainpage instead of the class selector .mainpage because in your HTML code you have set the ID of the image element to mainpage.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

#mainpage {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

<!-- language: lang-html -->

&lt;a-assets&gt;
  &lt;img id=&quot;mainpage&quot; src=&quot;https://static.dezeen.com/uploads/2017/04/aia-honour-im-pei-grande-louvre-25-year_dezeen_2.jpg&quot;&gt;
&lt;/a-assets&gt;

<!-- end snippet -->

答案3

得分: 0

位置固定背景图像

CSS属性position的值可以定位DOM元素,支持值如[tag:相对定位],[tag:绝对定位],[tag:固定定位],[tag:粘性定位]。

您可以使用 position: fixed; 来实现您想要的效果。

CSS ID选择器是 #,因此您不能使用 .mainpage 来选择ID属性。

CSS类选择器是 .,您在此处使用了。

*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

.bg {    
  background: url('https://scx1.b-cdn.net/csz/news/800/2017/theoreticala.jpg');
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  position: fixed;
  height: 100vh;
  width: 100vw;
}

.content {
  position: absolute;
}
<div class="bg"></div>
<div class="content">
  <!-- 内容 -->
</div>
英文:

Position Fixed Background Image

CSS property position value can position the dom element with values like [tag:reletive],[tag:absolute],[tag:fixed],[tag:sticky].

You could use position : fixed; to achieve what you want

CSS id selector is # so you cannot use .mainpage to select id attribute.

CSS class selector is . which you're using here

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

*, *::before, *::after {
  box-sizing: border-box;
  margin:0;
  padding:0;
}

.bg {    
  background: url(&#39;https://scx1.b-cdn.net/csz/news/800/2017/theoreticala.jpg&#39;);
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  position: fixed;
  height: 100vh;
  width: 100vw;
}

.content {
  position: absolute;
}

<!-- language: lang-html -->

&lt;div class=&quot;bg&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;content&quot;&gt;
  &lt;!-- content --&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月12日 14:02:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75711326.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定