【问题标题】:I want to play gif every time I click每次点击都想播放gif
【发布时间】:2019-04-10 18:47:36
【问题描述】:

当前状态

我有一个只播放一次的 gif。
每次点击按钮时我都想播放 gif。
目前,一旦它完成播放 gif,它会在第二次之后停止移动。


当前代码

CSS

    html { font-size: 62.5%; }

.target {
  position: absolute;
  bottom: 0;
  right: 0;
  width: 37.6rem;
  height: 31.1rem;
  background: url(https://cdn-ak.f.st-hatena.com/images/fotolife/O/O2_milk/20190410/20190410164103.gif);
}

@mixin ghost {
  background-color: #fff;
  background-color: rgba(255, 255, 255, 0.4);
  border-color: #fff;
  border-color: rgba(255, 255, 255, 0.4);
  transition: background-color 0.3s ease-in, border-color 0.3s ease-in;
  color: rgba(0, 0, 0, 1);
}

@keyframes inhale {
  from {
    opacity: 1;
    top: 50%;
    left: 50%;
    transform: scale(1) translate(-50%, -50%);
  }
  to {
    opacity: 0;
    top: calc(100% - 100px);
    left: calc(100% - 100px);
    transform: scale(0) translate(-50%, -50%);
  }
}

iframe {
  transform-origin: 0% 0%;
}

.clsbtn {
  outline: none;
  position: absolute;
  top: 0;
  text-align: center;
  width: 100%;
  height: 3.5rem;
  line-height: 3.5;
  background: white;
  -webkit-transition: .3s;
  transition: .3s;
  background-color: rgba(255, 255, 255, 0);
  border-color: rgba(255, 255, 255, 0);
  color: rgba(255, 255, 255, 0);
  z-index: 111;
  cursor: pointer;
  font-family: Arial, Helvetica, sans-serif;
  &:hover {
    @include ghost;
  }
}

.inhale {
  animation: inhale 1s cubic-bezier(0, 1, .56, 1);
}

jQuery

var timeStamp  = new Date().getTime();
        $(".target").attr('background','url(../img/score.gif' + '?' + timeStamp + ')');

全图

function closeBtn() {
    $(".clsbtn").on('click', function() {
        var target = $("<div>").addClass('target');

        // here
        var timeStamp  = new Date().getTime();
        $(".target").attr('background','url(../img/score.gif' + '?' + timeStamp + ')');

        $(this).hide();
        $("iframe").addClass('inhale').before(target).one('animationend', function() {
            $("iframe").removeClass('inhale').empty(target);
            $("#items").magnificPopup('close');
        });
    });
}

HTML

<div id="items">
    <section class="item" data-mfp-src="data1.html">
      <img src="img/a.png" alt="a" />
      <h2>title1</h2>
      <p>description1</p>
    </section>
    <section class="item" data-mfp-src="data2.html">
      <img src="img/b.png" alt="b" />
      <h2>title2</h2>
      <p>description2</p>
    </section>
...
</div>

就我而言,如何每次点击播放 gif?


添加

JSFiddle
codepen

当我尝试使用 JSFiddle 和 codepen 时,它工作正常。 :O
(每次点击都会播放一个 gif)

gif 在本地环境中不能正常工作吗?

【问题讨论】:

  • 请添加您的html
  • var timeStamp = new Date().getTime(); 可能第二次过时了。尝试将new Date() 更改为Date.now()
  • @לבני מלכה 我不使用 HTML,所以没有..
  • @zer00ne 谢谢。我把它改成了Date.now (),但是关闭按钮不起作用,所以它不起作用.. :(
  • 你不使用 HTML 而是使用 jQuery 来定位 HTML 元素/类/ID?

标签: javascript jquery css animation


【解决方案1】:

元素没有原生background 属性,而不是$(".target").attr('background', ...) 设置$(".target").css('background', ...),并且设置它不会触发任何原生操作。

【讨论】:

  • 非常感谢。改成.css()还是不行。。我也试过别人的.one().on()的建议,但是第二次点击后,gif还是不动。。x(
  • @POP closeBtn 函数曾经执行过吗?
  • 是的,每次按钮都按预期关闭,但只有 gif 会在第二次后停止。但是,当我使用 JSFiddle 和 Codepen 进行尝试时,gif 被正确执行。 (?!) gif 在本地不能正常工作吗?
  • @POP 小提琴/codepen 工作,因为 CSS/JQuery 已完成。您不能认真地为该问题发布半生不熟的代码,然后再发布工作代码,这是浪费时间。顺便说一句,.attr()/.css() 部分 Teemu 是正确的,因为 .attr() 是完全错误的,而且.css() 是正确的,但在 OP 代码的上下文中,无论哪种方式都完全没用。我从工作代码中删除了它,它仍然有效。当然,OP最初提供的代码很少有人知道吗?
  • @zer00ne 对不起.. 我明白了。我的本地环境是 chrome,但是当我尝试用 safari 阅读它时,每次点击都会播放 gif。所以,我认为这可能是 chrome 中的一个错误..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-03
  • 2018-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-27
相关资源
最近更新 更多