You need to make it so that you can swipe the phone to the next article, wordpress, there is a fixed switch <?php previous_post_link('<a>%link</a>')?> function, how to implement it?

    1 answer 1

    Here is the answer to your question with pure JS with native functions, so that you do not accidentally invent a bicycle :)

     // Вешаем на прикосновение функцию handleTouchStart document.addEventListener('touchstart', handleTouchStart, false); // А на движение пальцем по экрану - handleTouchMove document.addEventListener('touchmove', handleTouchMove, false); var xDown = null; var yDown = null; function handleTouchStart(evt) { xDown = evt.touches[0].clientX; yDown = evt.touches[0].clientY; }; function handleTouchMove(evt) { if ( ! xDown || ! yDown ) { return; } var xUp = evt.touches[0].clientX; var yUp = evt.touches[0].clientY; var xDiff = xDown - xUp; var yDiff = yDown - yUp; // немного поясню здесь. Тут берутся модули движения по оси абсцисс и ординат (почему модули? потому что если движение сделано влево или вниз, то его показатель будет отрицательным) и сравнивается, чего было больше: движения по абсциссам или ординатам. Нужно это для того, чтобы, если пользователь провел вправо, но немного наискосок вниз, сработал именно коллбэк для движения вправо, а ни как-то иначе. if ( Math.abs( xDiff ) > Math.abs( yDiff ) ) {/*most significant*/ if ( xDiff > 0 ) { /* left swipe */ } else { /* right swipe */ } } else { // Это вам, в общем-то, не надо, вы ведь только влево-вправо собираетесь двигать if ( yDiff > 0 ) { /* up swipe */ } else { /* down swipe */ } } /* reset values */ xDown = null; yDown = null; }; 
    • one
      Although the link can find the answer to the question, it is better to provide an answer extract here, and the link as a source. Link references may become invalid in case of changing the page to which the link is given. - VenZell
    • @VenZell wanted to do it right away, but lost the link to this page. Now corrected the answer. - smellyshovel