Javascript jQuery完整页面转换滚动条

Javascript jQuery完整页面转换滚动条,javascript,jquery,Javascript,Jquery,所以我想用jQuery实现一个完整的页面转换滚动。我知道有这个插件,但我需要在我的情况下自定义代码 //new script <script> $(document).ready(function(){ // http://stackoverflow.com/questions/4770025/how-to-disable-scrolling-temporarily // left: 37, up: 38, right: 39, down: 40, /

所以我想用jQuery实现一个完整的页面转换滚动。我知道有这个插件,但我需要在我的情况下自定义代码

//new script
<script>

  $(document).ready(function(){
    // http://stackoverflow.com/questions/4770025/how-to-disable-scrolling-temporarily

    // left: 37, up: 38, right: 39, down: 40,
    // spacebar: 32, pageup: 33, pagedown: 34, end: 35, home: 36
    var keys = {37: 1, 38: 1, 39: 1, 40: 1};

    function preventDefault(e) {
      e = e || window.event;
      if (e.preventDefault)
          e.preventDefault();
      e.returnValue = false;
    }

    function preventDefaultForScrollKeys(e) {
        if (keys[e.keyCode]) {
            preventDefault(e);
            return false;
        }
    }

    function disableScroll() {
      if (window.addEventListener) // older FF
          window.addEventListener('DOMMouseScroll', preventDefault, false);
      window.onwheel = preventDefault; // modern standard
      window.onmousewheel = document.onmousewheel = preventDefault; // older browsers, IE
      window.ontouchmove  = preventDefault; // mobile
      document.onkeydown  = preventDefaultForScrollKeys;
    }

    function enableScroll() {
        if (window.removeEventListener)
            window.removeEventListener('DOMMouseScroll', preventDefault, false);
        window.onmousewheel = document.onmousewheel = null;
        window.onwheel = null;
        window.ontouchmove = null;
        document.onkeydown = null;
    }



        /*  $(window).scroll(function(){

        }); */

              var lastScrollTop = 0;
  $(window).scroll(function(event){
     var st = $(this).scrollTop();

     if (st > lastScrollTop){
         // downscroll code
         console.log('down');
         if (($(this).scrollTop() >940) && ($(this).scrollTop() <1000)){
         disableScroll();
          $('html, body').animate({ scrollTop: $(".bg1").offset().top}, 2000);
          enableScroll();
         }

         if ($(this).scrollTop() >1548){
              disableScroll();
           $('html, body').animate({ scrollTop: $(".bg2").offset().top}, 2000);
               enableScroll();
            }
     } else {
        // upscroll code
        console.log('up');
      /*  if ($(this).scrollTop() >1548){
           disableScroll();
        $('html, body').animate({ scrollTop: $(".bg").offset().top}, 2000);
            enableScroll();
         } */
     }
     lastScrollTop = st;
  });


  }); //document.ready

</script>
//新脚本
$(文档).ready(函数(){
// http://stackoverflow.com/questions/4770025/how-to-disable-scrolling-temporarily
//左:37,上:38,右:39,下:40,
//空格键:32,向上翻页:33,向下翻页:34,结束:35,起始:36
var键={37:1,38:1,39:1,40:1};
功能默认值(e){
e=e | | window.event;
如果(如默认)
e、 预防默认值();
e、 returnValue=false;
}
CROLLKEYS(e)的默认功能{
if(键[e.keyCode]){
防止违约(e);
返回false;
}
}
函数disableScroll(){
if(window.addEventListener)//FF
addEventListener('DOMMouseScroll',preventDefault,false);
window.onwheel=preventDefault;//现代标准
window.onmouseheel=document.onmouseheel=preventDefault;//旧浏览器,即
window.ontouchmove=preventDefault;//移动
document.onkeydown=preventDefaultForScrollKeys;
}
函数enableScroll(){
if(window.removeEventListener)
removeEventListener('DOMMouseScroll',preventDefault,false);
window.onmouseheel=document.onmouseheel=null;
window.onwheel=null;
window.ontouchmove=null;
document.onkeydown=null;
}
/*$(窗口)。滚动(函数(){
}); */
var lastScrollTop=0;
$(窗口)。滚动(功能(事件){
var st=$(this.scrollTop();
如果(st>lastScrollTop){
//向下滚动代码
console.log('down');
如果($(this.scrollTop()>940)和($(this.scrollTop()1548){
禁用滚动();
$('html,body').animate({scrollTop:$(“.bg2”).offset().top},2000);
enableScroll();
}
}否则{
//upscroll码
console.log('up');
/*if($(this).scrollTop()>1548){
禁用滚动();
$('html,body').animate({scrollTop:$(“.bg”).offset().top},2000);
enableScroll();
} */
}
lastScrollTop=st;
});
});//document.ready

这是我的脚本。它检查滚动是向上还是向下,然后从指定的像素开始转换。开始进行得很好。第一次转换正在进行。但是在那之后,无论我是向上还是向下滚动,它都会转换回bg1的位置。如果我非常强烈地滚动,有时CRO发生了对bg2的攻击。我的代码有什么问题吗?

这里是一个带有注释的工作代码以及我使用的源代码

  //$(document).ready(function(){ //disables all the scrolling
  //  $('body,html').css('overflow', 'hidden');
 //  });
  // http://stackoverflow.com/questions/4770025/how-to-disable-scrolling-temporarily
  // left: 37, up: 38, right: 39, down: 40,
  // spacebar: 32, pageup: 33, pagedown: 34, end: 35, home: 36
  var keys = {37: 1, 38: 1, 39: 1, 40: 1};

  function preventDefault(e) {
    e = e || window.event;
    if (e.preventDefault)
        e.preventDefault();
    e.returnValue = false;
  }

  function preventDefaultForScrollKeys(e) {
      if (keys[e.keyCode]) {
          preventDefault(e);
          return false;
      }
  }

  function disableScroll() {
    if (window.addEventListener) // older FF
        window.addEventListener('DOMMouseScroll', preventDefault, false);
    window.onwheel = preventDefault; // modern standard
    window.onmousewheel = document.onmousewheel = preventDefault; // older browsers, IE
    window.ontouchmove  = preventDefault; // mobile
    document.onkeydown  = preventDefaultForScrollKeys;
  }

  function enableScroll() {
      if (window.removeEventListener)
          window.removeEventListener('DOMMouseScroll', preventDefault, false);
      window.onmousewheel = document.onmousewheel = null;
      window.onwheel = null;
      window.ontouchmove = null;
      document.onkeydown = null;
  }


//http://stackoverflow.com/questions/33838956/jquery-full-page-scroll-without-plugin

  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,
      fourthHeight = $('#fourthPage').offset().top,
      fifthHeight = $('#fifthPage').offset().top,
      sixthHeight = $('#sixthPage').offset().top,
      seventhHeight = $('#seventhPage').offset().top,
      eightHeight = $('#eightPage').offset().top,
      ninthHeight = $('#ninthPage').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(fourthHeight, 'fourthPage');
          }
          else if (scrolled > fourthHeight + 1 && currentLocation == 'fourthPage') {
              scrollPage(fifthHeight, 'fifthPage');
          }
          else if (scrolled > fifthHeight + 1 && currentLocation == 'fifthPage') {
              scrollPage(sixthHeight, 'sixthPage');
          }
          else if (scrolled > sixthHeight + 1 && currentLocation == 'sixthPage') {
              scrollPage(seventhHeight, 'seventhPage');
          }
          else if (scrolled > seventhHeight + 1 && currentLocation == 'seventhPage') {
              scrollPage(eightHeight, 'eightPage');
          }
          else if (scrolled > eightHeight + 1 && currentLocation == 'eightPage') {
              scrollPage(ninthHeight, 'ninthPage');
          }
          else if (scrolled < ninthHeight - 1 && currentLocation == 'ninthPage') {
              scrollPage(eightHeight, 'eightPage');
          }
          else if (scrolled < eightHeight - 1 && currentLocation == 'eightPage') {
              scrollPage(seventhHeight, 'seventhPage');
          }
          else if (scrolled < seventhHeight - 1 && currentLocation == 'seventhPage') {
              scrollPage(sixthHeight, 'sixthPage');
          }
          else if (scrolled < sixthHeight - 1 && currentLocation == 'sixthPage') {
              scrollPage(fifthHeight, 'fifthPage');
          }
          else if (scrolled < fifthHeight - 1 && currentLocation == 'fifthPage') {
              scrollPage(fourthHeight, 'fourthPage');
          }
          else if (scrolled < fourthHeight - 1 && currentLocation == 'fourthPage') {
              scrollPage(thirdHeight, 'thirdPage');
          }
          else if (scrolled < thirdHeight - 1 && currentLocation == 'thirdPage') {
              scrollPage(secondHeight, 'secondPage');
          }
          else if (scrolled < secondHeight - 1 && currentLocation == 'secondPage') {
              scrollPage(firstHeight, 'firstPage');
          }
      }//autoScrolling IF

      // 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;
        disableScroll();
        $('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;
            enableScroll();
        });
      }

    //$('h1').html(scrolled);
    //$('h1').append("/" + secondHeight);
    //$('h1').append("/" + thirdHeight);
  })//document.ready
/$(document).ready(函数(){//禁用所有滚动
//$('body,html').css('overflow','hidden');
//  });
// http://stackoverflow.com/questions/4770025/how-to-disable-scrolling-temporarily
//左:37,上:38,右:39,下:40,
//空格键:32,向上翻页:33,向下翻页:34,结束:35,起始:36
var键={37:1,38:1,39:1,40:1};
功能默认值(e){
e=e | | window.event;
如果(如默认)
e、 预防默认值();
e、 returnValue=false;
}
CROLLKEYS(e)的默认功能{
if(键[e.keyCode]){
防止违约(e);
返回false;
}
}
函数disableScroll(){
if(window.addEventListener)//FF
addEventListener('DOMMouseScroll',preventDefault,false);
window.onwheel=preventDefault;//现代标准
window.onmouseheel=document.onmouseheel=preventDefault;//旧浏览器,即
window.ontouchmove=preventDefault;//移动
document.onkeydown=preventDefaultForScrollKeys;
}
函数enableScroll(){
if(window.removeEventListener)
removeEventListener('DOMMouseScroll',preventDefault,false);
window.onmouseheel=document.onmouseheel=null;
window.onwheel=null;
window.ontouchmove=null;
document.onkeydown=null;
}
//http://stackoverflow.com/questions/33838956/jquery-full-page-scroll-without-plugin
var currentLocation='firstPage';
//不需要在事件侦听器中设置这些,因为它们总是相同的。
var firstHeight=$('#firstPage').offset().top,
secondHeight=$(“#secondPage”).offset().top,
第三重=$(“#第三页”).offset().top,
第四高度=$(“#第四页”).offset().top,
第五高度=$(“#第五页”).offset().top,
第六高度=$(“#第六页”).offset().top,
第七高度=$(“#第七页”).offset().top,
eightHeight=$(“#eightPage”).offset().top,
ninthHeight=$('#ninthPage').offset().top;
//我们可以检查滚动是由用户还是由动画触发的。
var autoScrolling=false;
$(文档)。滚动(功能(e){
var scrolled=$(窗口).scrollTop();
//仅检查用户是否已滚动
如果(!自动滚动){
如果(滚动>1&¤tLocation=='firstPage'){
滚动页面(第二高度,“第二页”);
}
否则如果(滚动>第二高度+1&¤tLocation==“第二页”){
滚动页(第三页,第三页);
}
else if(滚动>第三页+1&¤tLocation==“第三页”){
滚动页面(第四高,“第四页”);
}
else if(滚动>第四页高度+1&¤tLocation=='fourthPage'){
滚动页面(第五高度,“第五页”);
}
else if(滚动>第五页高度+1&¤tLocation==“第五页”){
滚动页面(第六高度,“第六页”);
}
else if(滚动>第六页高度+1&¤tLocation==“第六页”){
滚动页面(第七高度,“第七页”);
}
否则如果(滚动>第七高度+1&&cu)