jquery整页滚动没有插件

时间:2015-11-21 02:10:36

标签: javascript jquery html timer

https://jsfiddle.net/dewit/xnq0pzx0/1/

var currentLocation = 'firstPage';

$(document).scroll(function(e){
    var scrolled = $(window).scrollTop(),
        secondHeight = $('#secondPage').offset().top,
        thirdHeight = $('#thirdPage').offset().top;

    if (scrolled > 1 && currentLocation == 'firstPage') {
        currentLocation = 'secondPage';
        $('body').animate({scrollTop:$('#secondPage').offset().top}, 500);
    } else if (scrolled > secondHeight + 1 && currentLocation == 'secondPage') {
        currentLocation = 'thirdPage';
        $('body').animate({scrollTop:$('#thirdPage').offset().top}, 500);
    } else if (scrolled < thirdHeight - 1 && currentLocation == 'thirdPage') {
        currentLocation = 'secondPage'
        $('body').animate({scrollTop:$('#secondPage').offset().top}, 500);
    } else if (scrolled < secondHeight - 1 && currentLocation == 'secondPage') {
        currentLocation = 'firstPage';
        $('body').animate({scrollTop:$('#firstPage').offset().top}, 500);
    }
})

我想制作一个没有插件的整页滑块。

我希望这能检测当前页面和滚动方向并移动下一页或上一页。

问题是当幻灯片正在变化时,它会检测新位置并滚动。

结果,它上下跳动。

所以,我想在滑动时冻结这个功能。

但是,我不知道如何对待这个。

1 个答案:

答案 0 :(得分:1)

问题是当用户滚动时以及动画时触发事件侦听器。从我看到的,你只想在用户滚动时检查所有if / else语句。以下内容应该有所帮助:

var currentLocation = 'firstPage';
// No need to set these inside the event listener since they are always the same.
var firstHeight = $('#firstPage').offset().top,
    secondHeight = $('#secondPage').offset().top,
    thirdHeight = $('#thirdPage').offset().top;

// Helper so we can check if the scroll is triggered by user or by animation.
var autoScrolling = false;

$(document).scroll(function(e){
    var scrolled = $(window).scrollTop();
    
    // Only check if the user scrolled
    if (!autoScrolling) {
    	if (scrolled > 1 && currentLocation == 'firstPage') {
            scrollPage(secondHeight, 'secondPage');
        } else if (scrolled > secondHeight + 1 && currentLocation == 'secondPage') {
            scrollPage(thirdHeight, 'thirdPage');
        } else if (scrolled < thirdHeight - 1 && currentLocation == 'thirdPage') {
            scrollPage(secondHeight, 'secondPage');
        } else if (scrolled < secondHeight - 1 && currentLocation == 'secondPage') {
            scrollPage(firstHeight, 'firstPage');
        }
    }
    
    // Since they all have the same animation, you can avoid repetition
    function scrollPage(nextHeight, page) {
      currentLocation = page;

      // At this point, the page will start scrolling by the animation
      // So we switch this var so the listener does not trigger all the if/else
      autoScrolling = true;
      $('body,html').animate({scrollTop:nextHeight}, 500, function () {
          // Once the animation is over, we can reset the helper.
          // Now it is back to detecting user scroll.
          autoScrolling = false;
      });
    }

	$('h1').html(scrolled);
	$('h1').append("/" + secondHeight);
	$('h1').append("/" + thirdHeight);
})
*{
	padding: 0;
	margin: 0;
}
html,body{
    width: 100%;
    height:100%;
}
.page{
	width: 100%;
	line-height: 100vh;
	text-align: center;
}
header{
	position: fixed;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header>
	<h1></h1>
</header>
<article>
	<div class="page" id="firstPage">
		<h2>first page</h2>
	</div>
	<div class="page" id="secondPage">
		<h2>second page</h2>
	</div>
	<div class="page" id="thirdPage">
		<h2>third page</h2>
	</div>
</article>