问题
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