How to enable touch events on pjax library?

◇◆丶佛笑我妖孽 提交于 2019-12-12 05:48:59

问题


I am developing a site where I use Pjax library (a port of jquery pjax). However the touch events don't go through. I am using Pjax like so:

var pjax = new Pjax({ selectors: ["head title", "body"] })

and also have some animations:

document.addEventListener('pjax:send', function(){
  var $main = document.querySelector('main')
  $main.style.opacity = 0
})

document.addEventListener('pjax:complete', function(){
  var $main = document.querySelector('main')
  $main.style.visibility = 'hidden'
  $main.style.opacity = 0
  setTimeout(function(){
    document.querySelector('main').style.visibility = 'visible'
    document.querySelector('main').style.opacity = 1
    attach_menu_control()
  }, 10)
})

I need it to work on mobile. The site is www.saulesinterjerai.lt (can be buggy)


回答1:


I found the solution, it works by redirecting touch event to click event like this:

if (is_touch_device()) {
  var all_links = document.querySelectorAll('a[href]')
  var event = new Event('click');

  for (var index = 0 ; index < all_links.length ; ++index) {
    all_links[index].addEventListener("touchend", function() {
      all_links[index].dispatchEvent(event)
    });
  }
}

function is_touch_device() {
  return 'ontouchstart' in window        // works on most browsers 
      || navigator.maxTouchPoints;       // works on IE10/11 and Surface
}


来源:https://stackoverflow.com/questions/44723508/how-to-enable-touch-events-on-pjax-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!