html - 在滚动条上可见时运行 css 动画

标签 html css animation scroll visible

我在创建我的测试网页时遇到了一个问题,我的问题有很多“答案”,但我无法在我的代码中实现。我知道我必须使用 javascript,但我无法让它工作。

因此,当我向下滚动到该图片在屏幕上可见时,我需要在所选图片上运行 css 动画动画。基本上就像这个页面:https://www.photoblog.com/

所以我在 html 中有关于图片的代码:

<img class="movepic" src="pictures/test.jpg">

然后是 CSS 移动的简单代码:

.movepic {
position: relative;
animation-name: move;
animation-duration: 3s;
visibility: visible;
animation-fill-mode: forwards;
z-index:10;
}
@keyframes move {
0% { right:0px; top:150px;}
100% {right:700px; top:150px;}
}

有没有办法让它工作,这样我就不需要完全重做这个?或者,如果是这样,能否请一些人给我建议如何使用代码插图来做到这一点。

非常感谢

最佳答案

我使用这段代码来实现这种效果:

HTML:

<img class="movepic" src="pictures/test.jpg">

CSS:

.movepic {
  opacity: 0;
  margin: 25px 0 0;
  color: white;
  padding: 10px;
 }

.FadeIn {
    -webkit-animation: slideIn 0.8s ease 0.3s forwards;
    animation: slideIn 0.8s ease 0.3s forwards;
}

@keyframes slideIn {
    0% {
        -webkit-transform: translateY(40px);
        opacity: 0;
    }
    100% {
        -webkit-transform: translateY(0px);
        opacity: 1;
    }
}

JQuery:

var $fade =  $(".movepic"); //Calling the class in HTML

$(window).scroll(function () { //Using the scroll global variable
    $fade.each(function () {

        fadeMiddle = $(this).offset().top + (0.4 *$(this).height());
        windowBottom = $(window).scrollTop() + $(window).height();

        if (fadeMiddle < windowBottom) {
          $(this).addClass("FadeIn");
        }
    });
});

/* On Load: Trigger Scroll Once*/
$(window).scroll();

关于html - 在滚动条上可见时运行 css 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50626766/

相关文章:

html - 响应式背景 - 希望主页的高度为 100%,但图像无法覆盖移动尺寸

ios - 如何在转换为视频时为图像制作动画

swift - 在 Swift 中使用完成处理程序动画图像

html - 导航栏宽度,位置固定但不粘

javascript - 响应式下拉菜单

CSS 下拉项最初打开

jquery - 使用 CSS 修改 SVG 作为背景图像

css - 使用纯 CSS 隐藏/显示时的 Angular 动画

java - 基于 Web 的电子邮件回复/报价代码可用吗?

javascript - JS代码运行很慢