简体   繁体   English

用 display: none class 播放 css 动画

[英]play css animation with a display: none class

I have some code using jQuery and JavaScript that when you scroll into the windows viewport any element with a css animation class is applied.我有一些使用 jQuery 和 JavaScript 的代码,当你滚动到 windows 视口时,任何带有 css 动画类的元素都会被应用。 Now I have a section of my site that is hidden with display: none and when you click a button a simple function runs applying display: none and display: block.现在,我的网站有一部分隐藏在 display: none 中,当您单击按钮时,会运行一个简单的函数,应用 display: none 和 display: block。

The problem I am having is when I click the button to reveal the hidden content all the animations play at once, when in fact they should continue the document flow and only animate when the users scrolls into the viewport.我遇到的问题是,当我单击按钮以显示所有动画一次播放的隐藏内容时,实际上它们应该继续文档流并且仅在用户滚动到视口时才动画。

I put multiple dummy elements so you can see the effect as you scroll.我放置了多个虚拟元素,以便您在滚动时可以看到效果。

How can I work my way around this?我该如何解决这个问题?

 function see_more(){ document.getElementById("hide").style.display = 'block'; document.getElementById("seeMore").style.display =`none`; document.getElementById("seeLess").style.display =`block`; } function see_less(){ document.getElementById("hide").style.display = 'none'; document.getElementById("seeMore").style.display =`block`; document.getElementById("seeLess").style.display =`none`; } jQuery(function($) { var doAnimations = function() { var offset = $(window).scrollTop() + $(window).height(), $animatables = $('.animatable'); if ($animatables.length == 0) { $(window).off('scroll', doAnimations); } $animatables.each(function(i) { $animatable = $(this); if (($animatable.offset().top + $animatable.height() +65) < offset) { $animatable.removeClass('animatable').addClass('animated'); } }); }; $(window).on('scroll', doAnimations); $(window).trigger('scroll'); });
 #seeLess{ display: none; } #hide{ display: none; } /* ANIMATION */ .animatable { /* initially hide animatable objects */ visibility: hidden; /* initially pause animatable objects their animations */ -webkit-animation-play-state: paused; -moz-animation-play-state: paused; -ms-animation-play-state: paused; -o-animation-play-state: paused; animation-play-state: paused; } /* show objects being animated */ .animated { visibility: visible; -webkit-animation-fill-mode: both; -moz-animation-fill-mode: both; -ms-animation-fill-mode: both; -o-animation-fill-mode: both; animation-fill-mode: both; -webkit-animation-duration: 1s; -moz-animation-duration: 1s; -ms-animation-duration: 1s; -o-animation-duration: 1s; animation-duration: 1s; -webkit-animation-play-state: running; -moz-animation-play-state: running; -ms-animation-play-state: running; -o-animation-play-state: running; animation-play-state: running; } @keyframes bounceIn { 0% { opacity: 0; transform: scale(.3); } 50% { transform: scale(1.05); } 70% { transform: scale(.9); } 100% { opacity: 1; transform: scale(1); } } .animated.bounceIn { -webkit-animation-name: bounceIn; -moz-animation-name: bounceIn; -o-animation-name: bounceIn; animation-name: bounceIn; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <p class=" animatable bounceIn">Lion Cut</p> <div class=" animatable bounceIn" style="margin-bottom: 300px;"> <p>This is one of our specialty Services, the Lion Cut. One of our customers favorite cuts, have your favorite pet looking like a majestic king or queen in no time! </p> </div> <p class=" animatable bounceIn">Lion Cut</p> <div class=" animatable bounceIn" style="margin-bottom: 300px;"> <p>This is one of our specialty Services, the Lion Cut. One of our customers favorite cuts, have your favorite pet looking like a majestic king or queen in no time! </p> </div> <p class=" animatable bounceIn">Lion Cut</p> <div class=" animatable bounceIn" style="margin-bottom: 300px;"> <p>This is one of our specialty Services, the Lion Cut. One of our customers favorite cuts, have your favorite pet looking like a majestic king or queen in no time! </p> </div> <p class=" animatable bounceIn">Lion Cut</p> <div class=" animatable bounceIn" style="margin-bottom: 300px;"> <p>This is one of our specialty Services, the Lion Cut. One of our customers favorite cuts, have your favorite pet looking like a majestic king or queen in no time! </p> </div> <p class=" animatable bounceIn">Lion Cut</p> <div class=" animatable bounceIn" style="margin-bottom: 300px;"> <p>This is one of our specialty Services, the Lion Cut. One of our customers favorite cuts, have your favorite pet looking like a majestic king or queen in no time! </p> </div> <p class=" animatable bounceIn">Lion Cut</p> <div class=" animatable bounceIn" style="margin-bottom: 300px;"> <p>This is one of our specialty Services, the Lion Cut. One of our customers favorite cuts, have your favorite pet looking like a majestic king or queen in no time! </p> </div> <div class="container-fluid"><button id="seeMore" onclick="see_more()">See all services</button></div> <div id="hide"> <p class=" animatable bounceIn">Lion Cut</p> <div class=" animatable bounceIn" style="margin-bottom: 300px;"> <p>This is one of our specialty Services, the Lion Cut. One of our customers favorite cuts, have your favorite pet looking like a majestic king or queen in no time! </p> </div> <p class=" animatable bounceIn">Lion Cut</p> <div class=" animatable bounceIn" style="margin-bottom: 300px;"> <p>This is one of our specialty Services, the Lion Cut. One of our customers favorite cuts, have your favorite pet looking like a majestic king or queen in no time! </p> </div> <p class=" animatable bounceIn">Lion Cut</p> <div class=" animatable bounceIn" style="margin-bottom: 300px;"> <p>This is one of our specialty Services, the Lion Cut. One of our customers favorite cuts, have your favorite pet looking like a majestic king or queen in no time! </p> </div> <p class=" animatable bounceIn">Lion Cut</p> <div class=" animatable bounceIn" style="margin-bottom: 300px;"> <p>This is one of our specialty Services, the Lion Cut. One of our customers favorite cuts, have your favorite pet looking like a majestic king or queen in no time! </p> </div> </div> <div class="container-fluid"><button id="seeLess" onclick="see_less()">See less</button></div>

This is occurring for 3 reasons.发生这种情况有 3 个原因。

  1. The animated style class was never removed when the see less button is clicked, prior to setting the display to none.在将 display 设置为 none 之前,当点击 see less 按钮时,动画样式类从未被删除。

    • Therefore, the elements hid, but on re-appearing, because they had the animated class, they re-animated at the same time.因此,元素隐藏了,但在重新出现时,因为它们有动画类,它们同时重新动画。
  2. When clicking the show more button, all of these will animate at once because when at the end of the page, the browser triggers a scroll event automatically, but the correct offset doesn't update in time.当单击显示更多按钮时,所有这些都会立即动画,因为在页面末尾时,浏览器会自动触发滚动事件,但正确的偏移量不会及时更新。

    • Therefore, the browser thinks all the elements have the same offset at the end of the page, so it animates them all因此,浏览器认为所有元素在页面末尾都具有相同的偏移量,因此将它们全部设置为动画
  3. Jquery's scroll event listener stopped firing after reaching the end of the page. Jquery 的滚动事件侦听器在到达页面末尾后停止触发。 Not sure what caused it, but I know they sometimes implement their event listeners different than most browsers.不确定是什么原因造成的,但我知道他们有时会实现与大多数浏览器不同的事件侦听器。

The fix:修复:

  1. Replace jquery "scroll" event listener with native javascript/browser API scroll event listener用原生 javascript/browser API 滚动事件监听器替换 jquery “scroll” 事件监听器
  2. Remove animated class whenever the see more or see less button is clicked.每当单击“查看更多”或“查看更少”按钮时删除动画类。
    • At the end of the day, we only want the animated class when a user scrolls归根结底,我们只想要用户滚动时的动画类

(All problems resided in javascript, css and html were fine) (所有问题都存在于 javascript、css 和 html 中)

Below is a quick fix version of your javascript code.以下是您的 javascript 代码的快速修复版本。 However, I suggest doing some re-factoring of this version, to remove repeated code.但是,我建议对该版本进行一些重构,以删除重复的代码。

Let me know if this helped :) !让我知道这是否有帮助:)!

 var doAnimations = function() { var offset = $(window).scrollTop() + $(window).height(), $animatables = $('.animatable'); if ($animatables.length == 0) { $(window).off('scroll', doAnimations); } $animatables.each(function(i) { $animatable = $(this); if (($animatable.offset().top + $animatable.height() +65) < offset) { $animatable.removeClass('animatable').addClass('animated'); } }); }; function see_more(){ $animatables = $('.animated'); $animatables.each(function(i) { var offset = $(window).scrollTop() + $(window).height(), $animatable = $(this); if (($animatable.offset().top + $animatable.height() +65) < offset) { $animatable.removeClass('animated').addClass('animatable'); } }); document.getElementById("hide").style.display = 'block'; document.getElementById("seeMore").style.display =`none`; document.getElementById("seeLess").style.display =`block`; $(window).on('scroll', doAnimations); } function see_less(){ $animatables = $('.animated'); $animatables.each(function(i) { var offset = $(window).scrollTop() + $(window).height(), $animatable = $(this); if (($animatable.offset().top + $animatable.height() +65) < offset) { $animatable.removeClass('animated').addClass('animatable'); } }); document.getElementById("hide").style.display = 'none'; document.getElementById("seeMore").style.display =`block`; document.getElementById("seeLess").style.display =`none`; } $(window).trigger('scroll'); window.addEventListener("scroll", doAnimations);
 #seeLess{ display: none; } #hide{ display: none; } /* ANIMATION */ .animatable { /* initially hide animatable objects */ visibility: hidden; /* initially pause animatable objects their animations */ -webkit-animation-play-state: paused; -moz-animation-play-state: paused; -ms-animation-play-state: paused; -o-animation-play-state: paused; animation-play-state: paused; } /* show objects being animated */ .animated { visibility: visible; -webkit-animation-fill-mode: both; -moz-animation-fill-mode: both; -ms-animation-fill-mode: both; -o-animation-fill-mode: both; animation-fill-mode: both; -webkit-animation-duration: 1s; -moz-animation-duration: 1s; -ms-animation-duration: 1s; -o-animation-duration: 1s; animation-duration: 1s; -webkit-animation-play-state: running; -moz-animation-play-state: running; -ms-animation-play-state: running; -o-animation-play-state: running; animation-play-state: running; } @keyframes bounceIn { 0% { opacity: 0; transform: scale(.3); } 50% { transform: scale(1.05); } 70% { transform: scale(.9); } 100% { opacity: 1; transform: scale(1); } } .animated.bounceIn { -webkit-animation-name: bounceIn; -moz-animation-name: bounceIn; -o-animation-name: bounceIn; animation-name: bounceIn; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <p class=" animatable bounceIn">Lion Cut</p> <div class=" animatable bounceIn" style="margin-bottom: 300px;"> <p>This is one of our specialty Services, the Lion Cut. One of our customers favorite cuts, have your favorite pet looking like a majestic king or queen in no time! </p> </div> <p class=" animatable bounceIn">Lion Cut</p> <div class=" animatable bounceIn" style="margin-bottom: 300px;"> <p>This is one of our specialty Services, the Lion Cut. One of our customers favorite cuts, have your favorite pet looking like a majestic king or queen in no time! </p> </div> <p class=" animatable bounceIn">Lion Cut</p> <div class=" animatable bounceIn" style="margin-bottom: 300px;"> <p>This is one of our specialty Services, the Lion Cut. One of our customers favorite cuts, have your favorite pet looking like a majestic king or queen in no time! </p> </div> <p class=" animatable bounceIn">Lion Cut</p> <div class=" animatable bounceIn" style="margin-bottom: 300px;"> <p>This is one of our specialty Services, the Lion Cut. One of our customers favorite cuts, have your favorite pet looking like a majestic king or queen in no time! </p> </div> <p class=" animatable bounceIn">Lion Cut</p> <div class=" animatable bounceIn" style="margin-bottom: 300px;"> <p>This is one of our specialty Services, the Lion Cut. One of our customers favorite cuts, have your favorite pet looking like a majestic king or queen in no time! </p> </div> <p class=" animatable bounceIn">Lion Cut</p> <div class=" animatable bounceIn" style="margin-bottom: 300px;"> <p>This is one of our specialty Services, the Lion Cut. One of our customers favorite cuts, have your favorite pet looking like a majestic king or queen in no time! </p> </div> <div class="container-fluid"><button id="seeMore" onclick="see_more()">See all services</button></div> <div id="hide"> <p class=" animatable bounceIn">Lion Cut</p> <div class=" animatable bounceIn" style="margin-bottom: 300px;"> <p>This is one of our specialty Services, the Lion Cut. One of our customers favorite cuts, have your favorite pet looking like a majestic king or queen in no time! </p> </div> <p class=" animatable bounceIn">Lion Cut</p> <div class=" animatable bounceIn" style="margin-bottom: 300px;"> <p>This is one of our specialty Services, the Lion Cut. One of our customers favorite cuts, have your favorite pet looking like a majestic king or queen in no time! </p> </div> <p class=" animatable bounceIn">Lion Cut</p> <div class=" animatable bounceIn" style="margin-bottom: 300px;"> <p>This is one of our specialty Services, the Lion Cut. One of our customers favorite cuts, have your favorite pet looking like a majestic king or queen in no time! </p> </div> <p class=" animatable bounceIn">Lion Cut</p> <div class=" animatable bounceIn" style="margin-bottom: 300px;"> <p>This is one of our specialty Services, the Lion Cut. One of our customers favorite cuts, have your favorite pet looking like a majestic king or queen in no time! </p> </div> </div> <div class="container-fluid"><button id="seeLess" onclick="see_less()">See less</button></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM