【问题标题】:Long html page scroll snapping长 html 页面滚动捕捉
【发布时间】:2012-10-24 08:07:11
【问题描述】:

我正在开发一个很长的 html 页面,并且该设计在垂直方向的内容上有块,就像页面一样。我想要做的是当用户向下滚动页面以“捕捉”到每个页面正确放置在屏幕中心的点时,如果用户的位置在正确位置的一小段距离内。

我们将 jquery 用于页面上的其他内容,如果它有相关内容,我们很乐意使用它。

任何建议当然都非常感谢。

【问题讨论】:

  • 那么现在,你的问题是什么?
  • 欢迎来到 StackOverflow。通常,如果您提供一些具体信息会有所帮助:您尝试了什么,遇到了什么问题,诸如此类。你刚刚发布了一个 UI 概念,人们不会开始为你编写代码 ;-)
  • 请注意 - 如果您开始实现我的代码,我会对其进行重新设计,使其功能更好。

标签: javascript jquery html


【解决方案1】:

未对此进行测试,但应该可以。有关其工作原理的说明,请参阅 cmets。

我在捕捉后禁用该点,以便在尝试滚动远离它时不会重复捕捉它。 leniancy 变量既是距用户在再次激活之前必须滚动的点的距离,也是捕捉点之前的距离。

var points = [250, 675, 1225, $("#someDiv")]; // y coordinates or jQuery objects you want to centre against.
var disabledPoints = []; // Points that you won't snap to (leave empty) this allows user to scroll from a point
var wMid = ($(window).height() / 2); // Pixels to centre of window
var leniancy = 100; // Number of pixels ether side of a point before it is snapped

$.each(points, function(key, value) { // Loop through points
   if (value instanceof jQuery) { // If value is a jQuery object
      function setObjectPos(){
         var offset = value.offset();
         var centerPoint = offset.top + (value.height() /2); // Calculate object centre relative to document
         value.data('centerPoint', centerPoint); // Store it for later
      });
      value.bind('DOMSubtreeModified', setObjectPos); // If div changes update  center position.
   }
}

$(window).resize(function () {  // If user resizes window update window mid-point variable
   wMid = ($(window).height() / 2);
});

$(window).scroll(function () { 
   var centerPos = $(window).scrollTop() + wMid; // Position of screen center relative to document
   $.each(points, function(key, value) { // Loop through points
      if (value instanceof jQuery) {
         value = value.data('centerPoint');
      }
      var offset = value - centerPos;
      if(checkDistance(offset, leniancy)) {
         snapToPoint(key); // Success! snap that point
         return false; // Kill the loop
      }
   });
   $.each(disabledPoints, function(key, value) { // Loop through disabled points
      if (value instanceof jQuery) {
         value = value.data('centerPoint');
      }
      var offset = value - centerPos;
      if(!checkDistance(offset, leniancy)) {
         enablePoint(key); // Success! enable that point
      }
   });
});

function checkDistance(offset, margin) {
   if (offset > 0) { 
      if (offset <= margin) { 
         return true;
      }
   } else {
      if (offset >= (margin * -1)) {
         return true;
      }
   }
   return false;
}

function snapToPoint(index) {
   var offset = (points[index] instanceof jQuery) ? points[index].data('centerPoint') - wMid : points[index] - wMid;
   $(window).scrollTop(offset); 
   var point = points.splice(index, 1); 
   disabledPoints.push(point[0]); 
}

function enablePoint(index) {
   var point = disabledPoints.splice(index, 1);
   points.push(point[0]); 
}​

【讨论】:

  • 非常感谢您抽出宝贵时间回复,非常感谢。我去看看。
  • @DanGrant 没问题,如果您遇到任何问题,请告诉我。就像我说的那样,它没有经过测试,所以你可能会遇到错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-10
  • 1970-01-01
  • 2019-10-15
  • 1970-01-01
  • 2019-07-14
  • 2021-12-04
相关资源
最近更新 更多