javascript – 如何使用CSS创建图像滚动混合效果?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 如何使用CSS创建图像滚动混合效果?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3275字,纯文字阅读大概需要5分钟。
内容图文
![javascript – 如何使用CSS创建图像滚动混合效果?](/upload/InfoBanner/zyjiaocheng/708/837af2b1839c4e67bcb1c4c890e77b90.jpg)
我在网上看到了很酷的滚动效果……
滚动部分时图像与下一图像混合的位置.我一直试图重现它,但我似乎无法弄清楚它?
如何在网上创建此效果?
这是我看到效果的链接… http://readingbuddysoftware.com/how-it-works/
我尝试过使用position:修复屏幕截图,截面的z-index高于图像,但最后一个截图始终位于顶部.
有任何想法吗?
更新:由于各种原因(包括放置,使用斜面…),我无法使用background-image css解决方案.我需要一个使用< img>的解决方案元件.
解决方法:
这可以使用background-attachement:固定和两个相似的图像来完成.
这是一个简单的例子:
body {
min-height:200vh;
margin:0;
background:url(https://picsum.photos/g/150/150?image=1069) 20px 20px no-repeat;
background-attachment:fixed;
}
.box {
margin-top:220px;
height:200px;
background:url(https://picsum.photos/150/150?image=1069) 20px 20px no-repeat,
grey;
background-attachment:fixed;
}
<div class="box">
</div>
您可以使用许多图像轻松缩放:
body {
min-height:250vh;
margin:0;
background:url(https://lorempixel.com/g/100/100/) 50px 50px/auto no-repeat;
background-attachment:fixed;
}
.box {
height:200px;
background:url(https://lorempixel.com/100/100/) 50px 50px/auto no-repeat,
grey;
background-attachment:fixed;
}
.box:first-child {
margin-top:200px;
}
<div class="box">
</div>
<div class="box" style="background-image:url(https://lorempixel.com/100/100/sports);background-color:yellow">
</div>
<div class="box" style="background-image:url(https://lorempixel.com/100/100/food);background-color:pink">
</div>
UPDATE
如果你不喜欢背景图像解决方案,你将不得不使用一些JS来移动img标签(我不认为你可以通过图像和没有JS实现这一点).
这是一个简单的例子,我用滚动更新框内图像的位置,我使用overflow:hidden,以便在需要时显示:
window.onscroll = function() {
var scroll = window.scrollY || window.scrollTop || document.getElementsByTagName("html")[0].scrollTop;
document.documentElement.style.setProperty('--scroll-var', scroll+"px");
}
:root {
--scroll-var: 0px;
}
body {
min-height: 150vh;
margin: 0;
}
img {
position: fixed;
top: 20px;
left: 20px;
}
.box {
margin-top: 220px;
height: 200px;
background: grey;
position: relative;
overflow: hidden;
}
.box img {
top: calc(-220px + 20px + var(--scroll-var));
/* margin of box + top of the other image + scroll*/
position: absolute;
}
<img src="https://picsum.photos/g/150/150?image=1069">
<div class="box">
<img src="https://picsum.photos/150/150?image=1069">
</div>
有很多图像:
window.onscroll = function() {
var scroll = window.scrollY || window.scrollTop || document.getElementsByTagName("html")[0].scrollTop;
document.documentElement.style.setProperty('--scroll-var', scroll+"px");
}
:root {
--scroll-var: 0px;
}
body {
min-height: 250vh;
margin: 0;
padding-top:200px;
}
img {
position: fixed;
top: 50px;
left: 50px;
}
.box {
height: 200px;
background: grey;
position: relative;
overflow: hidden;
}
img.f1 {
top: calc(-200px + 50px + var(--scroll-var));
position: absolute;
}
img.f2 {
top: calc(-400px + 50px + var(--scroll-var));
position: absolute;
}
img.f3 {
top: calc(-600px + 50px + var(--scroll-var));
position: absolute;
}
<img src="https://lorempixel.com/g/100/100/">
<div class="box">
<img class="f1" src="https://lorempixel.com/100/100/">
</div>
<div class="box" style="background-color:yellow;">
<img class="f2" src="https://lorempixel.com/100/100/sports">
</div>
<div class="box" style="background-color:pink;">
<img class="f3" src="https://lorempixel.com/100/100/animals">
</div>
内容总结
以上是互联网集市为您收集整理的javascript – 如何使用CSS创建图像滚动混合效果?全部内容,希望文章能够帮你解决javascript – 如何使用CSS创建图像滚动混合效果?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。