简体   繁体   中英

How to enable touch events on pjax library?

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)

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM