Prevent scrolling on mobile browser, without preventing input focusing

前端 未结 3 1354
轻奢々
轻奢々 2021-01-02 22:41

I use preventDefault() on touchstart on the document to prevent scrolling on a page. Unfortunately this prevents too much. The user can no longer give focus to an input (a

3条回答
  •  没有蜡笔的小新
    2021-01-02 23:07

    I actually solved this problem on another project, forgot about it, and remembered it most of the way through typing this up.

    They key is to just do it on touchmove.

    $(document).on('touchmove', function(e) {
        e.preventDefault();
    });
    

    However, preventDefault on touchstart does all kinds of nice things like preventing the image save menu on a gesture enabled slideshow. My projects also include this.

    $(document).on('touchstart', function(e) {
        if (e.target.nodeName !== 'INPUT') {
            e.preventDefault();
        }
    });
    

    If anyone has some suggestions on additional content or how to reformat this so that it can reach a greater audience that would be great. I haven't ever seen the content I have here all in one place, so I felt that it needed to be on SO.

提交回复
热议问题