參考 http://iscrolljs.com/ 這個網站網站
代碼以下:spa
function isPassive () { var supportsPassiveOption = false try { addEventListener('test', null, Object.defineProperty({}, 'passive', { get: function () { supportsPassiveOption = true } })) } catch (e) {} return supportsPassiveOption }
function preventDefaultFunc (e) { e.preventDefault() }
export function preventTouchmove (onoff) { if (onoff) { document.body.addEventListener('touchmove', preventDefaultFunc, isPassive() ? { capture: false, passive: false } : false) } else { console.log('wawawwaawa') document.body.removeEventListener('touchmove', preventDefaultFunc, isPassive() ? { capture: false, passive: false } : false) } }
而後經過 code
import { preventTouchmove } from 'utils/common'
來導入這個方法,而後使用,onoff 是開關,來傳 布爾值blog
preventTouchmove(true/false)