javascript 使图像(位置:固定)仅在 DIV 内可见

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26382474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 05:53:45  来源:igfitidea点击:

Make images (position:fixed) be visible only inside a DIV

javascripthtmlcssimage

提问by Johannes

I want to create a website, inspired by http://cirkateater.no/en/.
The images shall stay at the same position on the Screen (position:fixed), but inside the next div, the second image shall be visible.

我想创建一个网站,灵感来自http://cirkateater.no/en/
图像应保持在屏幕上的相同位置(位置:固定),但在下一个 div 内,第二个图像应可见。

My first solution was to use background-images, which worked.

我的第一个解决方案是使用背景图像,这很有效。

With background-images, it looked like this JSFIDDLE: http://jsfiddle.net/Ryanous/r7mLm4wg/4/(i do not own these images)
Consider the following code:

使用背景图像,它看起来像这个 JSFIDDLE:http: //jsfiddle.net/Ryanous/r7mLm4wg/4/我不拥有这些图像
考虑以下代码:

<div class="background" style='background-image:url("http://mintywhite.com/wp-content/uploads/2012/10/fond-ecran-wallpaper-image-arriere-plan-hd-29-HD.jpg");'></div><div class="background" style='background-image:url("http://vpnhotlist.com/wp-content/uploads/2014/03/image.jpg");'></div>





But I also want to move the images via JS later on!You cant manipulate backgroundimages top-offset easily, so I decided to use an img-tag instead.

Now, using the img-tag, the images seem to be independent of their divs, and overlap each other so that always only one image is visible.

但我也想稍后通过 JS 移动图像!你不能轻易地操纵背景图像的顶部偏移,所以我决定使用一个 img-tag 来代替。

现在,使用 img-tag,图像似乎独立于它们的 div,并且彼此重叠,因此始终只有一个图像可见

( id like to include my current code but stackoverflow wont let me do. four spaces dont do anything, i cant format my code.)

我喜欢包含我当前的代码,但 stackoverflow 不允许我这样做。四个空格什么都不做,我无法格式化我的代码。



So my question is:


How can I make images be only visible inside a certain div, but keep their positions fixed?



所以我的问题是:


如何使图像仅在某个 div 内可见,但保持它们的位置固定?

回答by Daan

Putting the style overflow:hidden;on the div would hide all that's outside of the div.

将样式overflow:hidden;放在 div 上会隐藏 div 之外的所有内容。