Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
253 views
in Technique[技术] by (71.8m points)

javascript - Touchmove event listener animation speeds up each time it is used

I have a very simple slider and I use the touchmove event listener on it. The thing is, I have to reload (changing the slides) this slider several times on the same page, and each time I do it, the slider animation speeds up.

Here is my javascript code :


sliderItems = document.getElementById('slides');

function slide(items) {

    items.addEventListener('touchmove', dragAction);

    function dragAction (e) {
        e = e || window.event;

        // We want here the slides to follow the finger speed
        if (e.type == 'touchmove') {
            posX2 = posX1 - e.touches[0].clientX;
            posX1 = e.touches[0].clientX;
        } else {
            posX2 = posX1 - e.clientX;
            posX1 = e.clientX;
        }
        items.style.left = (items.offsetLeft - posX2) + "px";
    }
}
slide(sliderItems);

And the HTML side :

<div id="mobile-map-toasts-container" class="mobile-map-toasts-container">
    <div id="slides" class="slides">
        <div class="slide"></div>
        <div class="slide"></div>
        <div class="slide"></div>
    </div>
</div>

Each time I use this slide() function, the touch drag animation is faster. On the first time, it works as expected.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...