滑动水平响应式布局及鼠标滚轮输入

9
我是一名帮助翻译的助手。下面是你需要翻译的内容:

我想实现一个带有头部横幅的滑动水平布局

HTML 结构如下:

<body>
  <div id="header">
    <div><a href="#one"> one </a></div>
    <div><a href="#two"> two </a></div>
    <div><a href="#thr"> thr </a></div>
  </div>

  <div id="one" class="panel"> </div>
  <div id="two" class="panel"> </div>
  <div id="thr" class="panel"> </div>
</body>
是固定的,面板的背景色已经设置为渐变颜色(中间的面板有一个不同的颜色,用于调试目的)。 以下是CSS代码:

    body {
      width: 6000px;
      overflow: hidden;
    }

    .panel {
      width: 33.3%;
      float: left;
      padding-left: 30px;
      padding-right: 1040px;
      margin-top: -75px;
      height: 960px;
      background-image: linear-gradient(to bottom, #0B88FF, #95EEFF);
    }

    #header {
      position: fixed;
      height: 75px;
      margin-top: 25px;
    }

    #two{
      background-image: linear-gradient(to bottom, #0B8800, #9E5EFF);
    }

最后是管理面板动画的函数:
$(document).ready(function() {
  $("#header a").bind("click", function(event) {
    event.preventDefault();
    var target = $(this).attr("href");
    $("html, body").stop().animate({
      scrollLeft: $(target).offset().left,
      scrollTop: $(target).offset().top
    }, 1200);
  });
});

我所面临的问题如下:
1)我尝试使用jQuery函数实现当用户使用鼠标滚轮时运行幻灯片动画,但我的所有测试都无法正常工作...结构始终保持不变:
$(window).scroll(function() {
      if ($(this).scrollTop() > 0) {
        var target // still not able to figure out who should i target
        $("html, body").stop().animate({
            //to the target >,<
       }
});

2) 当我的浏览器窗口大小为100%时,一切都运行良好,但如果我缩小或增大缩放比例,一切都会混乱>,< 我注意到这是可以处理的,这里有一个例子:

2个回答

2
为了达到你的目标,你可以用一个数组填充具有“panel”类的元素,然后使用索引来遍历面板。
最后,如果你在窗口调整大小时遇到滚动问题,你可以绑定这个事件并做任何你想做的事情。
查看MouseWheelEvent。
尝试使用你的代码运行这个示例:

$(document).ready(function() {
  $("#header a").bind("click", function(event) {
    event.preventDefault();
    var target = $(this).attr("href");
    $("html, body").stop().animate({
      scrollLeft: $(target).offset().left,
      scrollTop: $(target).offset().top
    }, 1200);
  });
  
  var scroll_targets = $(".panel");
  var scroll_targets_index = 0;
  $(window).on('DOMMouseScroll mousewheel', function (e) {    
    if (e.originalEvent.wheelDelta < 0) {
      if(scroll_targets_index < scroll_targets.length-1){
        var where = ++scroll_targets_index;
        $("html, body").stop().animate({
          scrollLeft: $(scroll_targets[where]).offset().left,
          scrollTop: $(scroll_targets[where]).offset().top
        }, 1200);
      }
    } 
    else {
     var where;
     if(scroll_targets_index > 0){
        where = --scroll_targets_index;
      }
      else{
       where = 0;
      }
       $("html, body").stop().animate({
          scrollLeft: $(scroll_targets[where]).offset().left,
          scrollTop: $(scroll_targets[where]).offset().top
        }, 1200);
      
    }
  });
  
  $(window).resize(function () {
    $('html,body').scrollTop($(scroll_targets[where]).offset().top);
    $('html,body').scrollLeft($(scroll_targets[where]).offset().left);
  });
});
#body {
      width: 6000px;
      overflow: hidden;
    }

    .panel {
      width: 33.3%;
      float: left;
      padding-left: 30px;
      padding-right: 1040px;
      margin-top: -75px;
      height: 960px;
      background-image: linear-gradient(to bottom, #0B88FF, #95EEFF);
    }

    #header {
      position: fixed;
      height: 75px;
      margin-top: 25px;
    }

    #two{
      background-image: linear-gradient(to bottom, #0B8800, #9E5EFF);
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="body">
  <div id="header">
    <div><a href="#one"> one </a></div>
    <div><a href="#two"> two </a></div>
    <div><a href="#thr"> thr </a></div>
  </div>

  <div id="one" class="panel"> </div>
  <div id="two" class="panel"> </div>
  <div id="thr" class="panel"> </div>
</div>


1
在Unravel中,我认为这是使用transform3d和其他一些CSS技巧使用CSS和JavaScript制作的。如果你想获取滚动事件,当你在body上有overflow:hidden时,它不会发生,因为你刚告诉浏览器隐藏所有滚动条!所以,我尝试解决你现在遇到的问题的方法是使用mousewheel事件,它的作用如下:
$(window).on('wheel', function (event) {      
    console.log(event); // Here you can see all of its events properties and functions in the console
});

有一个wheelDelta属性,如果你向前滚动鼠标滚轮,它返回120,如果向后滚动,返回-120,因此我添加了一个计数器,以便我知道鼠标滚轮事件触发了多少次:

$(window).on('wheel', function (event) {      

    if (event.originalEvent.wheelDelta / 120 > 0 ) {
        count++;
        console.log(count)  ;
        if(count > 30 ){

            $("html, body").stop().animate({
              scrollLeft: $('#two').offset().left,
              scrollTop: $('#two').offset().top
            }, 1200);   
        }
        if(count > 60 ){

            $("html, body").stop().animate({
              scrollLeft: $('#thr').offset().left,
              scrollTop: $('#thr').offset().top
            }, 1200);               
        }
        if(count > 90 ){

            $("html, body").stop().animate({
              scrollLeft: $('#two').offset().left,
              scrollTop: $('#two').offset().top
            }, 1200);   
            count = 0;          
        }


    }
});

这仅供您继续创建逻辑以构建当鼠标滚轮更改到另一个面板等等,祝你好运!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接