zoukankan      html  css  js  c++  java
  • 禁止橡皮筋效果

    
    
    let startY = 0
    let enabled = false
    let supportsPassiveOption = false
    try {
    let opts = Object.defineProperty({}, 'passive', {
    get: function () {
    supportsPassiveOption = true
    }
    })
    window.addEventListener('test', null, opts)
    } catch (e) {
    // console.log('false')
    }
    const handleTouchmove = function (evt) {
    let el = evt.target
    let zoom = window.innerWidth / window.document.documentElement.clientWidth
    if (evt.touches.length > 1 || zoom !== 1) {
    return
    }
    while (el !== document.body && el !== document) {
    let style = window.getComputedStyle(el)
    if (!style) {
    break
    }
    if (el.nodeName === 'INPUT' && el.getAttribute('type') === 'range') {
    return
    }
    let scrolling = style.getPropertyValue('-webkit-overflow-scrolling')
    let overflowY = style.getPropertyValue('overflow-y')
    // let height = parseInt(style.getPropertyValue('height'), 10)
    let isScrollable = scrolling === 'touch' && (overflowY === 'auto' || overflowY === 'scroll')
    let canScroll = el.scrollHeight > el.offsetHeight
    if (isScrollable && canScroll) {
    let height = el.clientHeight
    let curY = evt.touches ? evt.touches[0].screenY : evt.screenY
    let isAtTop = (startY <= curY && el.scrollTop === 0)
    let isAtBottom = (startY >= curY && el.scrollHeight - el.scrollTop === height)
    if (isAtTop || isAtBottom) {
    evt.preventDefault()
    }
    return
    }
    el = el.parentNode
    }
    evt.preventDefault()
    }
    const handleTouchstart = function (evt) {
    startY = evt.touches ? evt.touches[0].screenY : evt.screenY
    }
    const enable = function () {
    window.addEventListener('touchstart', handleTouchstart, supportsPassiveOption ? { passive: false } : false)
    window.addEventListener('touchmove', handleTouchmove, supportsPassiveOption ? { passive: false } : false)
    enabled = true
    }
    const disable = function () {
    window.removeEventListener('touchstart', handleTouchstart, false)
    window.removeEventListener('touchmove', handleTouchmove, false)
    enabled = false
    }
    const isEnabled = function () {
    return enabled
    }
    let testDiv = document.createElement('div')
    document.documentElement.appendChild(testDiv)
    testDiv.style.WebkitOverflowScrolling = 'touch'
    let scrollSupport = 'getComputedStyle' in window && window.getComputedStyle(testDiv)['-webkit-overflow-scrolling'] === 'touch'
    document.documentElement.removeChild(testDiv)
    if (scrollSupport) {
    enable()
    }
    let preventOverScroll = {
    enable: enable,
    disable: disable,
    isEnabled: isEnabled
    }

    export default preventOverScroll
    
    
  • 相关阅读:
    POJ1125-Stockbroker Grapevine【Floyd】(模板题)
    Codeforces 862B (二分图染色)
    hdu3047 Zjnu Stadium【带权并查集】
    HDU1532 网络流最大流【EK算法】(模板题)
    hdu 2063 过山车【匈牙利算法】(经典)
    HDU-2066-一个人的旅行 【Dijkstra】
    HDU 3625 Examining the Rooms【第一类斯特灵数】
    HDU4372-Count the Buildings【第一类Stirling数】+【组合数】
    HDU 6114 Chess【逆元+组合数】(组合数模板题)
    HDU1211 密文解锁 【扩展欧几里得】【逆元】
  • 原文地址:https://www.cnblogs.com/QQPrincekin/p/11541211.html
Copyright © 2011-2022 走看看