【问题标题】:Add moving effect on scroll to site在滚动到站点时添加移动效果
【发布时间】:2017-04-23 20:15:44
【问题描述】:

我已在我的网站 simmona.uchenici.bg/test/ 中尝试过此代码 结果是效果不会停止。当部分标题位于窗口底部且只有图像部分时,我希望滚动效果停止 在他们赶上时继续。另外我不知道为什么我的图片在滚动时会放大...如何调试?

$(document).ready(function(){
$(document).bind('mousewheel', function(e){
if(e.originalEvent.wheelDelta /680 > 0) {
  $('#textbox').animate({
    'marginTop' : "+=50px" 
  });
}
else {
  $('#textbox').animate({
    'marginTop' : "-=10px" 
  });
}

});

$(document).bind('mousewheel', function(e){
if(e.originalEvent.wheelDelta /680 > 0) {
  $('#image').animate({
    'marginTop' : "+=50px" 
  });
}
else {
  $('#image').animate({
    'marginTop' : "-=5px" 
  });
}`}); 

});`

【问题讨论】:

  • 我注意到当我到达 margin-top 时我必须停止第一部分效果:-230px

标签: javascript jquery html css web


【解决方案1】:

您无需对动画进行排队。现在发生的事情是您向上、向下、向上向下滚动……但动画不会停止,而您已经通过滚动发出新命令。为了解决这个问题,请检查 .stop documentation 以供进一步使用。

$(document).ready(function(){

    $(document).bind('mousewheel', function(e){

        if(e.originalEvent.wheelDelta / 680 > 0) {
            $('#textbox').stop(true, false).animate({ /* 1) applied stop(true, false)*/
              'marginTop' : "+=50px" 
            }, 1000, function(){});
        }else {
            $('#textbox').stop(true, false).animate({ /* 2) applied stop(true, false)*/
              'marginTop' : "-=50px" 
            }, 1000, function(){}); 
        }

    });
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="textbox"> SOME TEXT</p>

【讨论】:

  • 我不明白如何停止效果。我不是在学习 js,它的语法对我来说很远。我也不知道为什么 img 会放大滚动? :(
  • 看看我对你的代码的回答,在你开始动画之前,只需添加这部分 .stop(true, false).animation.... //// 关于图像问题:1)你正在申请margin-top,在有图像的 div 上。 2)您使用 flex,并且因为您应用了 margin-top,所以您的 div 会调整大小。 3) 因为你使用了 background-size:cover,所以你的图像会尝试获得宽高比,因此看起来像是在调整大小。要解决调整大小问题,您必须应用固定高度,或使用 this thread 中的 padding-bottom 技术
  • 非常感谢您对图片的帮助!现在我使用你的代码,但效果错误是一样的 - 它不会停止......
猜你喜欢
  • 2015-10-11
  • 1970-01-01
  • 2013-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-27
  • 2012-01-04
  • 2023-03-19
相关资源
最近更新 更多