美文网首页让前端飞
滚动js触发css3动画

滚动js触发css3动画

作者: 砚婉儿 | 来源:发表于2017-03-28 10:49 被阅读0次

css代码:

.section,.section .section-content p{
    -webkit-transition: all 2s ease-out;
    -moz-transition: all 2s ease-out;
    -ms-transition: all 2s ease-out;
    -o-transition: all 2s ease-out;
    transition: all 2s ease-out;
}
body{background-color: #48cfad;color: #fff;overflow-x: hidden;}
header{height: 100vh;text-align: center;line-height: 100vh;}
.page-wrapper{width: 80%;margin: 50px auto;box-sizing: border-box;}
.section{
    width: 100%;height: 100vh;padding: 40px 0;opacity: 0;
    -webkit-transform: translateY(50px);
    -moz-transform: translateY(50px);
    -ms-transform: translateY(50px);
    -o-transform: translateY(50px);
    transform: translateY(50px);
}
.section h2{font-size: 30px;text-align: center;}
.section .section-content{width: 80%;margin: 0 auto;text-align: center;}
.section .section-content p{
    max-width: 80%;margin: 20px auto 50px;
    -webkit-transform: translateX(-100px);
    -moz-transform: translateX(-100px);
    -ms-transform: translateX(-100px);
    -o-transform: translateX(-100px);
    transform: translateX(-100px);
}
.section.animation{
    opacity: 1;
    -webkit-transform: translateY(0);
    -moz-transform: translateY(0);
    -ms-transform: translateY(0);
    -o-transform: translateY(0);
    transform: translateY(0);
}
.section.animation .section-content p{
    -webkit-transform: translateX(0);
    -moz-transform: translateX(0);
    -ms-transform: translateX(0);
    -o-transform: translateX(0);
    transform: translateX(0);
}

html代码:

<header>
    <h1>Animation on Scroll</h1>
</header>
<main class="page-wrapper">
    <section class="section">
        <h2>SECTION</h2>
        <div class="section-content">
            <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ad animi at, atque commodi cupiditate doloremque eius exercitationem, maiores modi obcaecati officia quod, recusandae sed sequi sit soluta sunt vitae! Temporibus.</p>
        </div>
    </section>
    
    <br /><br /><br /><br /><br />
    
    <section class="section">
        <h2>SECTION</h2>
        <div class="section-content">
            <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ad animi at, atque commodi cupiditate doloremque eius exercitationem, maiores modi obcaecati officia quod, recusandae sed sequi sit soluta sunt vitae! Temporibus.</p>
        </div>
    </section>
</main>

js代码:

function revealOnScroll() {
    var scrolled = $(window).scrollTop();// 视窗,即viewport,页面可视范围的窗口

    $(".section").each(function() {
        var current = $(this), // 当前元素
            w_height = $(window).outerHeight(), //视窗高度
            offsetTop = current.offset().top; //当前元素离顶部的高度

        // 计算高度差(此处预留50是为了看效果)
        // 当元素进入视窗时,添加class
        if (scrolled + w_height - 50 > offsetTop) {
            current.addClass("animation");
        } else {
            current.removeClass("animation");
        }
    });
}
$(window).on("scroll", revealOnScroll);

…………END…………

谢谢支持,喜欢就点个❤

相关文章

  • 滚动js触发css3动画

    css代码: html代码: js代码: …………END………… 谢谢支持,喜欢就点个❤

  • AnimationEnd 事件侦听

    CSS3 动画结束时是有触发事件的,这个之前竟然不了解。。除了JS动画如果做纯css3动画的时候使用 delay ...

  • AOS.js页面滚动动画插件(CSS3)

    aos.js是一个用于在页面滚动的时候呈现元素动画的工具库,AOS是 CSS3 动画驱动的库,当你滚动页面的时候能...

  • vue 中使用wow.js

    wow.js可以在页面向下滚动时加载css动画,并且可以触发animate.css的动画效果。 wow.jsGit...

  • 滚动条触发CSS3动画

    近几年web前端的,关于鼠标滑动到当前页面从而触发动画效果十分的火热,今天我们就来学习下这一效果。 一、首先,我们...

  • JS触发滚动

    滚动的话, document级别的用window.scrollTo(), element内部的, 直接设置elem...

  • 2018-11-27第九天总结

    一、 动画网页动画可以通过以下几种方式实现(gif、flash 除外),css3 动画SVG 动画JS 动画(包括...

  • 2019-01-02 css3过渡动画 css3圆角阴影透明度

    一、 动画网页动画可以通过以下几种方式实现(gif、flash 除外),css3 动画SVG 动画JS 动画(包括...

  • HTML 常用确难搜到的文档

    1.滚动到指定位置触发时间 JS建立缓存

  • 复习

    动画历史回顾:gif动画->flash动画->js动画->css3动画 过渡动画(即补间动画):用于实现两种状态的...

网友评论

    本文标题:滚动js触发css3动画

    本文链接:https://www.haomeiwen.com/subject/hcprottx.html