简体   繁体   English

滚动带有触摸事件的页面

[英]Scroll a page with touch events

I have a website page and I've added to the body of the page touch events. 我有一个网站页面,并且已将页面触摸事件添加到该页面的正文中。 More exactly for swipe right and swipe left. 更精确地向右滑动和向左滑动。 Since the event listener is added to the body of the page and I have added event.preventDefault(); 由于事件侦听器已添加到页面的正文中,因此我添加了event.preventDefault();。 i can't scroll the page any more. 我无法再滚动页面。

How can i scroll the page in the browser ? 如何在浏览器中滚动页面?

PS The code is pure javascript / library agnostic. PS该代码是纯javascript /库不可知的。

Edit #1. 编辑#1。 This site viewed in mobile seems to do it http://swipejs.com/ . 在移动设备中查看过的该网站似乎做到了http://swipejs.com/ It slides the tabs right to left and back as well as scroll the website. 它使选项卡从右向左和向后滑动以及滚动网站。 I just can't seen in the code how :( 我只是在代码中看不到:(

Use iscroll plugin. 使用iscroll插件。 it's help to you. 对您有帮助。

see example : http://cubiq.org/dropbox/iscroll4/examples/simple/ 参见示例: http : //cubiq.org/dropbox/iscroll4/examples/simple/

Unfortunately there is no easy answer. 不幸的是,没有简单的答案。 The best way is to build smart gesture recognizers. 最好的方法是构建智能手势识别器。 Or use something like this (for Safari Mobile): 或使用以下方式(对于Safari Mobile):

http://mud.mitplw.com/JSGestureRecognizer/#single-gesture http://mud.mitplw.com/JSGestureRecognizer/#single-gesture

You will notice that when you are touching a gesture recognizer, there is no scrolling. 您会注意到,当您触摸手势识别器时,不会滚动。 However, you could make the callback of a recognizer scroll the page. 但是,您可以使识别器的回调滚动页面。

Wondering why it only says it supports Safari mobile? 想知道为什么它只说它支持Safari移动版? That's because Safari mobile has its own set of touch events. 这是因为Safari移动版具有自己的一组触摸事件。 However, you can use it as a start and try to add support for other platforms. 但是,您可以将其用作开始并尝试添加对其他平台的支持。

I have the same problem that swiping without "preventDefault()". 我有没有“ preventDefault()”刷卡的同样问题。 Because I want to achieve a pulltorefresh's effect, I can only prevent the pulldown event but not pullup. 因为我想实现pulltorefresh的效果,所以只能阻止pulldown事件,而不能阻止pullup事件。 The code like this: 像这样的代码:

function touchBindMove(evt){
    //evt.preventDefault();
    try{
        var deviceHeight = window.innerHeight;
        var touch = evt.touches[0]; //获取第一个触点  
        var x = Number(touch.pageX); //页面触点X坐标  
        var y = Number(touch.pageY); //页面触点Y坐标  
        //记录触点初始位置  

        if((y - offsetStart) > 0 && document.body.scrollTop == 0){
            evt.preventDefault();
            var page = document.getElementsByClassName('tweet-page')[0];
            var rate = 0;

            end = x;
            offsetEnd = y;
            rate = (offsetEnd - offsetStart) / (2 * deviceHeight);

            //tool.print(rate);
            easing.pullMotion(page, rate);
        }

    }catch(e){
        alert(e.message);
    }
}

"y - offsetStart" judges whether the event is pulldown and "document.body.scrollTop == 0" judges the scrollbar is in the middle or not. “ y-offsetStart”判断事件是否为下拉事件,“ document.body.scrollTop == 0”判断滚动条是否在中间。 Maybe it can help you a little bit. 也许它可以帮助您一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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