【问题标题】:Image Fly off Screen using jQuery使用 jQuery 的图像飞离屏幕
【发布时间】:2020-06-13 14:22:03
【问题描述】:

我试图让图像飞过屏幕一次然后停止。我可以让它循环运行,但它会分散注意力,所以我只希望它运行一次。有谁知道怎么做? (我必须使用 jQuery)

这是我目前所拥有的,但我希望它只运行一次,而不是循环:

var speedX = 20;
var speedY = 1;

setInterval(function() {
  var newLeft = $("img#bee").position().left + speedX + "px";
  var newTop = $("img#bee").position().top + speedY + "px";

  $("img#bee").css({
    left: newLeft,
    top: newTop
  });
  if ($("img#bee").position().left > $(window).width()) {
    $("img#bee").css({
      left: "0px"
    });
  }

  if ($("img#bee").position().left < 0) {
    $("img#bee").css({
      left: $(window).width() + "px"
    });
  }

  if ($("img#bee").position().top > $(window).height()) {
    $("img#bee").css({
      top: $(window).height() + "px"
    });
  }
  $(".stop-btn").click(function() {
    $("img#bee").stop();
  });

}, 20);
#bee { display:inline-block; position:absolute }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img id="bee"  src="https://res-1.cloudinary.com/crunchbase-production/image/upload/c_lpad,h_256,w_256,f_auto,q_auto:eco/v1417678932/jgbmipdnqptqxbig3ywz.jpg" />

【问题讨论】:

  • 你能分享一下你在做什么的代码 sn-p 吗?能帮到你就好多了。谢谢!
  • 已编辑,抱歉!
  • 我让你成为你应该从一开始就做的sn-p
  • 勾选左边>= (window.width-img.width)

标签: jquery image jquery-animate


【解决方案1】:

这可能不是最好的解决方案,但它会起作用。只需在 CSS 中为其添加一个过渡,然后使用 javascript/jquery 更改 css

Javascript:

 $(".nameOfElement").click(function() {
    $("#elementID").css({'right' : '0px'});
 });

HTML

<div id="elementID">Div content</div>

CSS

#elementID {
  position: absolute;
  left: 0px;
  transition: right 1500ms; //time to take to fly accros the screen
}

【讨论】:

    【解决方案2】:

    删除此if ($("img#bee").position().left &gt; $(window).width()) 条件可确保图像位置一旦超过屏幕宽度不应设置回0px,从而避免循环。

    var speedX = 20;
    var speedY = 1;
    
    setInterval(function() {
      var newLeft = $("img#bee").position().left + speedX + "px";
      var newTop = $("img#bee").position().top + speedY + "px";
    
      $("img#bee").css({
        left: newLeft,
        top: newTop
      });
     
    
      if ($("img#bee").position().left < 0) {
        $("img#bee").css({
          left: $(window).width() + "px"
        });
      }
    
      if ($("img#bee").position().top > $(window).height()) {
        $("img#bee").css({
          top: $(window).height() + "px"
        });
      }
      $(".stop-btn").click(function() {
        $("img#bee").stop();
      });
    
    }, 20);
    #bee { display:inline-block; position:absolute }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <img id="bee"  src="https://res-1.cloudinary.com/crunchbase-production/image/upload/c_lpad,h_256,w_256,f_auto,q_auto:eco/v1417678932/jgbmipdnqptqxbig3ywz.jpg" />

    【讨论】:

    • 虽然这可能会回答问题,但您应该始终解释您做了什么以及为什么它解决了 OP 的问题
    • 编辑了我的答案以解释我做了什么以及为什么。
    猜你喜欢
    • 2015-08-06
    • 2014-08-21
    • 2017-10-23
    • 2020-12-19
    • 2013-04-19
    • 1970-01-01
    • 1970-01-01
    • 2017-08-17
    • 2016-08-09
    相关资源
    最近更新 更多