jQuery : 도움이 필요합니다 ... 애니메이션의 콜백 함수는 작동하지 않습니다.

StackOverflow https://stackoverflow.com/questions/816754

  •  03-07-2019
  •  | 
  •  

문제

애니메이션 플러그인에서 일종의 바운스 효과를 원하지만 작동하지 않습니다. 콜백은 전혀 호출되지 않습니다.

$(function() {
    var offset = $("#first").offset();
    var position = $(window).scrollTop();
    $(window).scroll(function() {
      var scroll = $(window).scrollTop();
      if (scroll>position) {
        $("body")
          .animate({
            scrollTop: offset.top + $("#first").innerHeight() + 150
          },
          1000,
          "easeInOutQuart",
          function() {
             $("body").animate({
              scrollTop: offset.top - 150
            },
            1000,
            "easeOutBounce"
            )
          })
      }
    })
  })

좋아 .. 여기 내 html 코드가 있습니다 .. 나는 당신의 것이 왜 잘 작동하는지 모르겠습니다.

<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Bounce Test Pad</title>
<link rel=stylesheet href="index.css" type= "text/css" />
<script type="text/javascript" src ="jquery-1.3.2.js"></script>
<script type = "text/javascript" src="jquery.easing.1.3.js"></script>
</head>
<body>
    <img id="lightbulb" src = "img/veioza.png">
    <div id="wrapper">
        <img id="first"  class="images" src="img/nike.jpg" />
        <img id ="second" class = "images" src="img/golden.jpg" />
        <img id = "third" class ="images" src ="img/a.jpg" />
    </div>
<script type="text/javascript">
    $(window).resize(function() {
        centerIt();
    });
    $(function() {
        centerIt();
    })
    function centerIt() {
        var viewportWidthSize = window.innerWidth;
        var pixels = (viewportWidthSize / 2) - $("#first").width() / 2;
        $("#wrapper img").css("left",  pixels);
    };


    $(function() {
        var offset = $("#first").offset();
        var prevpos = $(window).scrollTop();
        var animating = false;
        $(window).scroll(function() {
            var curpos = $(window).scrollTop();
            if (curpos>prevpos && !animating) {
                $('html').animate(
                        {scrollTop: offset.top + $("#first").height()},
                        1000,
                        "easeInOutQuart"
                    )
            }
            animating = true;
        })
    })
</script>
</body>
</html>
도움이 되었습니까?

해결책

Anhtu는 스크롤 이벤트를 제기하는 애니메이션에 대해 옳습니다.

고정 데모는 다음과 같습니다. http://jsbin.com/alede (여기에서 데모를 편집 할 수 있습니다. http://jsbin.com/alede/edit)

애니메이션이 여전히 발생하는 동안 재 시계화를 방지하려면 코드를 추가해야합니다.

var status = $('#status');
var offset = $("#downcontent").offset();
var height = $("#downcontent").height();
var animating = false;
var prevpos = $(window).scrollTop();

$(window).scroll(function(){
  var curpos = $(window).scrollTop();
  if (curpos > prevpos && !animating) {
    $('html').animate(
      {scrollTop: offset.top + height},
      1000,
      "easeInOutQuart",
      function(){
        $('html').animate(
          {scrollTop: offset.top},
          1000,
          "easeOutBounce",
          function(){
            animating = false;
            status.html('Both animations finished.<br />');
          }
        );
        status.html('First animation finished.<br />Second animation started<br />');
      }
    );
    animating = true;
    status.html('First animation started<br />');
  }
  prevpos = curpos;
});

편집하다

좋아, 나는 당신의 HTML 코드로 또 다른 데모를 만들었습니다. JavaScript를 약간 수정하고 몇 가지 CSS 규칙을 추가했습니다. http://jsbin.com/oqapa

다른 팁

콜백을 사용하지 않고 수행하십시오

if (scroll>position) {
    $("body")
      .animate({
        scrollTop: offset.top + $("#first").innerHeight() + 150
      },1000,"easeInOutQuart")
      .animate({
        scrollTop: offset.top - 150
      },1000,"easeOutBounce")
    })
 }

그러나 나는 당신이 다른 사건에 묶는 것을 하바라고 생각합니다. 신체를 애니메이션 할 때 이벤트 창을 올릴 수 있기 때문입니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top