简体   繁体   English

如何删除元素(加载页面+css animation 100%)?

[英]How to delete the element (loading page + css animation 100%)?

How can I tell p js and jQuery if @keyframes 100%???如果@keyframes 100%,我怎么能告诉 p js 和 jQuery ???

If @keyframes for the element == 100%, do something specific如果元素的@keyframes == 100%,做一些特定的事情

My problem is I have an element on the page,, I want to delete the element when the page loads, and if the animation is done in CSS我的问题是页面上有一个元素,我想在页面加载时删除该元素,如果 animation 在 CSS 中完成

Meaning I do not want him to delete the element when the page loads.这意味着我不希望他在页面加载时删除元素。 I just want to delete the element when the page is complete + animation 100%我只想在页面完成后删除元素 + animation 100%

The element is span (load-span)元素为跨度(load-span)

HTML: HTML:

 <a href="#">
 <div class="back-move">
     <span class="load-span"></span>
     <div class="play-move"><span></span></div>
 </div>
 <p>GG img</p>

CSS: CSS:

    .load-span {
    position: absolute;
    background-image: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.8), transparent);
    width:40px;
    height: 100%;
    top: -50%;
    left: 100%;
    animation: load-span 1s;
    animation-iteration-count: infinite;
    transform: translate(15px, 0px) skewX(35deg);
}

@keyframes load-span {
    100%{ top: 100%;left: -100%;}
}

Also I have tried:我也试过:

$(window).on("load", function () {
 $('.load-span').on("animationend", function(){
    $(this).remove();
});
})

But it didn't work ):但它没有用):

You can simply remove the animation-iteration-count: infinite;您可以简单地删除animation-iteration-count: infinite; and this below code will work.下面的代码将起作用。

 $(window).on("load", function () { $('.load-span').on("animationend", function(){ console.log('fdfdf'); $(this).remove(); }); })
 .load-span { position: absolute; background-image: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.8), transparent); width:40px; height: 100%; top: -50%; left: 100%; animation: load-span 1s; transform: translate(15px, 0px) skewX(35deg); } @keyframes load-span { 100%{ top: 100%;left: -100%;} }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <a href="#"> <div class="back-move"> <span class="load-span"></span> <div class="play-move"><span></span></div> </div> <p>GG img</p>

The reason your above snippet doesn't work is because your animationend listener is not being called as you've set the animation-iteration-count to infinite , which tells the browser that the animation is never ending.上面的代码段不起作用的原因是因为您将animation-iteration-count设置为infinite时没有调用您的animationend侦听器,这告诉浏览器 animation 永远不会结束。

There are two tricks you can use to solve this,您可以使用两个技巧来解决此问题,

1.Get rid of infinite from the iteration count and set it to 1 , then your code snippet would work 1.从迭代计数中去掉infinite并将其设置为1 ,然后你的代码片段就可以工作了

$(window).on("load", function () {
    $('.load-span').on("animationend", function(){
        $(this).remove();
    });
})

2.If you're using some sort of inifinite loader, you could use a timer, as your animation duration is 1s , you can create a setTimeout callback, that checks whether the page has fully loaded or not. 2.如果您使用某种无限加载器,您可以使用计时器,因为您的 animation 持续时间为1s ,您可以创建一个 setTimeout 回调,检查页面是否已完全加载。

setTimeout(function(){
  // Check whether page has loaded or not
}, 1000);

You were close with this.你很接近这个。 So instead of this:所以代替这个:

$(window).on("load", function () {
  $('.load-span').on("animationend", function(){
  $(this).remove();

}); }); }) })

You can try this: Use webkitAnimationEnd along with animationend你可以试试这个:使用webkitAnimationEndanimationend

    $(window).on("load", function () {
  $('.load-span').on("animationend webkitAnimationEnd", function(){
  $(this).remove();

}); }); }) })

Hope this works!希望这有效!

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

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