最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Allow touchmove for child element - Stack Overflow

programmeradmin1浏览0评论

I want to disable scrolling for my entire jqm web app but if you try to scroll on a div with a certain class, then scrolling is allowed

So my scrolling div, .info has overflow:scroll applied to it and I have this script to try to detect if you're touching it

$('.info').bind('touchstart', function (e) {
    if (e.type == "touchstart") {
       return true; 
    } else {
        event.preventDefault();
    }
});
$(document).bind('touchmove', function () {
    event.preventDefault();
})

Right now, scrolling is turned off for the document but when I try to scroll my info div, it won't scroll.

Now I've seen a few post around this same idea, but mostly about to pinning an objects position so that when the document is scrolled, it stays in the same place.

I want to disable scrolling for my entire jqm web app but if you try to scroll on a div with a certain class, then scrolling is allowed

So my scrolling div, .info has overflow:scroll applied to it and I have this script to try to detect if you're touching it

$('.info').bind('touchstart', function (e) {
    if (e.type == "touchstart") {
       return true; 
    } else {
        event.preventDefault();
    }
});
$(document).bind('touchmove', function () {
    event.preventDefault();
})

Right now, scrolling is turned off for the document but when I try to scroll my info div, it won't scroll.

Now I've seen a few post around this same idea, but mostly about to pinning an objects position so that when the document is scrolled, it stays in the same place.

Share Improve this question asked Jul 29, 2013 at 16:59 mhartingtonmhartington 7,0155 gold badges43 silver badges79 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

This is how I've been doing it:

$(document).on('touchmove', function(ev) {
 if ( !$(ev.target).closest('.is-scrollable').length ) {
    ev.preventDefault();
  }
})

Where .is-scrollable would be your class, .info in this case.

EDIT: to fix scrolling at top and bottom of scrollable div:

$('.is-scrollable').on('touchstart', function() {
    var el = $(this);
    if ( el.scrollTop() <= 0 ) {
      el.scrollTop(1);
    }
    if ( el.scrollTop() >= el[0].scrollHeight ) {
      el.scrollTop(el[0].scrollHeight - 1);
    }
});
发布评论

评论列表(0)

  1. 暂无评论