jQuery视差效果和滞后现象

时间:2019-08-29 13:01:41

标签: jquery parallax lag

我有一个带有标题和内容的页面。 向下滚动时,标题菜单会停留在顶部,而内容具有一种“视差”效果(它的移动速度比标题要快,这正是我所需要的)。

我的小型jQuery脚本可很好地实现“视差”效果,但是当向下滚动达到最大值时,内容开始出现停滞/滞后。 该脚本似乎一直在尝试不断地向上移动内容(至少使用Apple Magic Mouse鼠标),这会造成这种不良的副作用。

如何预防?

PS:为了清楚地表明口吃的问题,我在JSFiddle中夸大了视差效果。

PPS:测试时请确保您具有可滚动的页面(浏览器高度较小),否则,效果和问题当然不会发生。

//sticky header menu

$(window).scroll(function() {
  if ($(document).scrollTop() > 92) {
    if (!$('.fixed').length) {
      $('.menu').addClass('fixed');
    }
  } else {
    if ($('.fixed').length) {
      $('.menu').removeClass('fixed');
    }
  }
});

// Parallax of content on scroll

var iCurScrollPos = 0;
$(window).scroll(function() {
    iCurScrollPos = $(this).scrollTop();
    $('.content').css('margin-top', -iCurScrollPos * 1.2 + 'px')
});
body {
  width: 100%;
  height: 100%;
  margin: 0px;
  padding: 0px;
  background: #ccc;
}

header {
  position: relative;
  width: 100%;
  background: #fff;
  z-index: 1;
  height: 146px;
}

.void {
  position: relative;
  width: 100%;
  height: 100px;
}

.menu {
  position: relative;
  width: 100%;
  height: 54px;
  background: #aaa;
}

.fixed {
  position: fixed;
  left: 0px;
  top: 0px;
}

img {
  width: 100%
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 

<header>
  <div class="void"></div>
  <nav class="menu"></nav>
</header> 

<div class="content">
  <img src="https://farm8.staticflickr.com/7632/16990947835_3894284fd8_b.jpg">
</div>

JSFiddle

https://jsfiddle.net/v6g43mkL/1/

3 个答案:

答案 0 :(得分:2)

您可以使用滚动位置的历史记录,通过比较最后2个位置与第3个和第4个位置是否相同来确定是否发生了卡顿现象:

$(window).scroll(function() {
    if ($(document).scrollTop() > 92){
            if (!$('.fixed').length){$('.menu').addClass('fixed');}
        } 
        else {
              if ($('.fixed').length){$('.menu').removeClass('fixed');}                    
    }     
});

// Parallax of page on scroll

var iCurScrollPos = 0;
// Contains the last 4 scroll positons.
var lastPositions = [];

$(window).scroll(function () {
    iCurScrollPos = $(this).scrollTop();

    lastPositions.push(iCurScrollPos);
    // Control over when locaties are marked as duplicates. Use it to fine tune the response.
    var duplicateRange = 20;

    // The stutter bug can be caught be checking if two last locations are the same as the 3rd and 4th.
     if(Math.abs(lastPositions[0] - lastPositions[2]) < duplicateRange && Math.abs(lastPositions[1] - lastPositions[3]) < duplicateRange){
      lastPositions = [];
       return;
    }

      console.log(lastPositions);
    if(lastPositions.length === 4){
      lastPositions = [];
    }



   $('.content').css('margin-top',-iCurScrollPos*1.2+'px')
});

jsFiddle:https://jsfiddle.net/maartendev/sj5egqhd/25/

答案 1 :(得分:2)

<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<header>
    <div class="void"></div>
    <nav class="menu">
    </nav>
</header>
<div class="content">
    <!-- Picture displayed twice for the example only -->
    <!-- to be sure everyone gets scollable page-->
    <img src="https://farm8.staticflickr.com/7632/16990947835_3894284fd8_b.jpg">
    <img src="https://farm8.staticflickr.com/7632/16990947835_3894284fd8_b.jpg">
</div>
<style>
body{
      width:100%;
      height:100%;
      margin:0px;
      padding:0px;
    background: #ccc;
}

header{
      position:relative;
      width:100%;
      background: #fff;
      z-index:1;
    height:146px;
}

.void{
      position:relative;
      width:100%;
        height:100px;
}

.menu{
      position:relative;
      width:100%;
    height:54px;
    background:#aaa;
}

.fixed{
    position: fixed;
    left:0px;
    top: 0px;
}

img{
  width:100%
}
</style>




<script>
// Sticky header menu

$(window).scroll(function() {
    if ($(document).scrollTop() > 92){
        if (!$('.fixed').length){$('.menu').addClass('fixed');}
    }else {
        if ($('.fixed').length){$('.menu').removeClass('fixed');}                  
    }     
});

// Parallax of page on scroll

var iCurScrollPos = 0;
$(window).scroll(function () {
console.log(iCurScrollPos ,iCurScrollPos - screen.height,$(window).height());
var max_sc = iCurScrollPos - screen.height;
    iCurScrollPos = $(this).scrollTop();
    if(iCurScrollPos < max_sc)
        $('.content').css('margin-top',-iCurScrollPos*1.2+'px')
});
</script>

在文档末尾,您必须停止滚动事件。

要计算您的页面结束 iCurScrollPos-screen.height

找到jsfiddle链接。 https://jsfiddle.net/cpo73s5g/

答案 2 :(得分:0)

您还可以通过在 CSS

中添加以下行来实现平滑滚动行为
 html{
     scroll-behavior:smooth;
   }

有关更多详细信息,请阅读Smooth Scrolling MDNCaniuse