gpt4 book ai didi

javascript - 向上滑动并淡入淡出动画

转载 作者:太空宇宙 更新时间:2023-11-03 21:08:55 24 4
gpt4 key购买 nike

我正在尝试在 CSS 或 JS 中创建类似于此的动画:http://oshinewptheme.com/v29/

因此,当用户向下滚动到图像时,它会稍微上升并淡入。我为此创建了基本结构:

HTML

<body>
<div class="gallery" align="center">
<img src="1.jpg">
<img src="3.jpg">
<img src="4.jpg">
<img src="5.jpg">
<img src="8.jpg">
<img src="9.jpg">
</div>
</body>

CSS

.gallery{
align-content: center;
margin-left: 200px;
margin-right: 200px;
margin-top: 500px;
}

我不确定如何让动画工作,如果你有任何想法,这将是很棒的元素:https://codepen.io/Hamzaw_GD/pen/zRQxZo

提前非常感谢您,我只有 13 岁,非常渴望找到解决方案,所以请耐心等待。

最佳答案

其他答案涉及悬停叠加层或一般动画,但我认为您对让图像淡入并在向下滚动时向上移动特别感兴趣。

这就是诀窍。我评论过JS。当心,它使用 jquery。如果您有任何问题,请告诉我。

$('.gallery img').addClass('hidden');

$(window).on('scroll', function(){

// Get the vertical offset of the first gallery image that is hidden.
var p = $('.gallery .hidden').offset();
p = p.top;

// Check for the current scroll position.
var s = $('body').scrollTop();

// Get the height of the window.
var h = $(window).height();

// Just logging the results for better understanding.
console.clear();
console.log(s + h, p);

// Triggering the fade in when the image is partially reached by scrolling (in this case 100px)
if((s + h) >= p) {

// Find the first element within <gallery> that has the class 'hidden'. Then fade it in by adding the class 'fadeIn' and removing the class 'hidden' so it is taken out of the rotation.
$('.gallery').find('.hidden').first().addClass('fadeIn').removeClass('hidden');
}

else {}
});
.gallery {
margin-left: auto;
margin-right:auto;
margin-top:100vh;
max-width:600px;
position:relative;
}

.gallery__help {
text-align:center;
transform: translateY(-50vh);
}

.gallery img {
opacity:0;
max-width:600px;
}

.hidden {
transform: translateY(100px);
}

.fadeIn {
opacity:1 !important;
transform: translateY(0);
-webkit-transition: all 2s ease;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="gallery">
<p class="gallery__help">Please Scroll Down</p>
<img src="https://media.springernature.com/full/nature-static/assets/v1/image-assets/531S56a-i1.jpg">
<img src="http://www.samoaobserver.ws/images/cache/600x400/crop/images%7Ccms-image-000009332.jpg">
<img src="http://www.worddive.com/blog/wp-content/uploads/2014/06/nature-and-environment-course.jpg">
<img src="https://cmkt-image-prd.global.ssl.fastly.net/0.1.0/ps/4037855/300/200/m2/fpc/wm0/ofj73imzdnhtoxcvlmowaxbdmbbtf2yixqwhacy3wwsjctvxybw0ggbjs9y7hr1h-.jpg?1519205483&s=10ea078a417885590d11783ca8a43b52">
<img src="https://lucysmagazine.com/wp-content/uploads/2017/04/Screen-Shot-2017-04-01-at-8.58.36-AM.jpg">
<img src="http://img.fotocommunity.com/alte-hoehenburg-9eebdd0f-f85d-40d7-9944-c15672ca0242.jpg?height=400">
</div>

关于javascript - 向上滑动并淡入淡出动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49096625/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com