【问题标题】:Won't scroll up when I use this swiping listener当我使用这个滑动监听器时不会向上滚动
【发布时间】:2017-10-06 21:02:21
【问题描述】:
if(window.location.pathname == '/'){
  var direc; //swipe direction var
  function detectswipe(el,func) {
    swipe_det = new Object();
    swipe_det.sX = 0; swipe_det.sY = 0; swipe_det.eX = 0; swipe_det.eY = 0;
    var min_x = 40;  //min x swipe for horizontal swipe
    var max_y = 60;  //max y difference for horizontal swipe
    direc = "";
    ele = document.getElementById(el);
    ele.addEventListener('touchstart',function(e){
      var t = e.touches[0];
      swipe_det.sX = t.screenX;
      swipe_det.sY = t.screenY;
    },false);
    ele.addEventListener('touchmove',function(e){
      e.preventDefault();
      var t = e.touches[0];
      swipe_det.eX = t.screenX;
      swipe_det.eY = t.screenY;
    },false);
    ele.addEventListener('touchend',function(e){
      //horizontal detection
      if ((((swipe_det.eX - min_x > swipe_det.sX) || (swipe_det.eX + min_x < swipe_det.sX)) && ((swipe_det.eY < swipe_det.sY + max_y) && (swipe_det.sY > swipe_det.eY - max_y) && (swipe_det.eX > 0)))) {
        if(swipe_det.eX > swipe_det.sX) direc = "r";
        else direc = "l";
      }

      if (direc != "") {
        if(typeof func == 'function') func(el,direc);
      }
      direc = "";
      swipe_det.sX = 0; swipe_det.sY = 0; swipe_det.eX = 0; swipe_det.eY = 0;
    },false);
  }

  jQuery(document).ready(function ($) {
    var timer = setInterval(function () {
        moveRight();
    }, 5500); //keeps the slideshow moving

使用此功能滑动时,我无法让页面向上或向下滚动。它应该是左右滑动检测。当它不听触摸/滑动时,它显然仍然滚动。任何帮助将不胜感激!

【问题讨论】:

  • 我应该提到,只有当我触摸所选元素时,我才能通过滑动来滚动。任何其他地方都可以正常工作。

标签: javascript jquery


【解决方案1】:

试试这个,它很简单,但可能会有所帮助。 https://jsfiddle.net/kyb3Lkqd/

var startingLocation = {x: 0, y:0};

$(document).on("touchstart", "#swp", function(e){
    console.log(e);
  startingLocation = {
    x: e.touches[0].clientX,
    y: e.touches[0].clientY
  };
});

$(document).on("touchmove", "#swp", function(e){
    console.log(e, e.touches[0].clientX, e.touches[0].clientY);
  if (startingLocation.x < e.touches[0].clientX){
     console.log('swiping right!');
  } else if (startingLocation.x > e.touches[0].clientX){
     console.log('swiping left!');
  }

});

【讨论】:

  • 谢谢!实际上,我只是让它工作-即将发布解决方案。这太简单了。我在那里有 e.preventDefault() - 这会阻止采取默认操作。就我而言,我不需要它在这里(因为防止溢出)。现在效果很好!
【解决方案2】:

这非常简单;我几乎很尴尬,一开始我没有看到它。在原来我有 e.preventDefault();滑动元素时禁用水平滚动。我不再需要那个(我认为)因为我有溢出:隐藏启用;这使该行变得多余。删除它使其完美运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-15
    • 2014-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多