移動端 局部dom實現(xiàn)滾動

移動端局部dom添加滑動事件

源碼


/**
 * moveTopLimit: 移動過程中扶踊,隨著手指的滑動 dom的偏移量
 * modifyTopLimit: 觸發(fā)touchEnd的時候 我們需要進行吸頂這樣的一個交互 設置吸頂?shù)姆秶? */
export default class TouchMove {
    constructor(params) {
        this.props = params;
        this.checkParams(params);
        this.startY = 0;
        this.moveEndY = 0;
        this.oldMoveY = 0;
        this.offsetSum = 0;
        this.curDistance = 0;
        this.init();
    }
    checkParams() {
        const objectKey = [
            'Dom',
            'slider',
        ];
        const baseTypeKey = [
            'moveTopLimit',
            'moveBottomLimit',
            'modifyTopLimit',
            'modifyBottomLimit',
        ];
        objectKey.forEach((item) => {
            if (!this.props[item]) {
                throw new Error(`${item}傳參錯誤,請檢查之后重試`);
            }
        });
        baseTypeKey.forEach((item) => {
            if (this.props[item] === undefined ||
                (Object.prototype.toString.call(this.props[item]) !== '[object Number]'
                    && Object.prototype.toString.call(this.props[item]) !== '[object String]')) {
                throw new Error(`${item}傳參錯誤黍少,請傳入數(shù)字或者字符類型的數(shù)字`);
            }
        });
    }
    init() {
        this.Dom = this.props.Dom;
        this.slider = this.props.slider;
        this.moveTopLimit = parseInt(this.props.moveTopLimit, 10) || 0;
        this.moveBottomLimit = parseInt(this.props.moveBottomLimit, 10) || 0;
        this.modifyTopLimit = parseInt(this.props.modifyTopLimit, 10) || 0;
        this.modifyBottomLimit = parseInt(this.props.modifyBottomLimit, 10) || 0;
        this.addListenerWheel();
    }
    addListenerWheel() {
        this.Dom.addEventListener('touchstart', (e) => {
            this.touchStart(e);
        });
        this.Dom.addEventListener('touchmove', (e) => {
            this.touchMove(e);
        });
        this.Dom.addEventListener('touchend', (e) => {
            this.touchEnd(e);
        });
    }
    touchStart(event) {
        this.startY = parseInt(event.touches[0].clientY, 10);
        this.oldMoveY = this.startY;
    }
    touchMove(event) {
        // 當屏幕上有多個touch或者頁面被縮放過 就不執(zhí)行move操作
        if (event.targetTouches.length > 1 || (event.scale && event.scale !== 1)) return;
        event.preventDefault();
        this.moveY = event.touches[0].clientY;
        this.offset = (this.moveY - this.oldMoveY);
        // 獲取dom當前滾動值
        this.getCurDistance();
        this.offsetSum = this.offset + this.curDistance;
        // 設置滑動范圍
        if (this.offsetSum > 0) {
            this.offsetSum = Math.min(this.offsetSum, this.moveTopLimit);
        } else if (this.offsetSum < 0) {
            this.offsetSum = Math.max(this.offsetSum, this.moveBottomLimit);
        }
        this.oldMoveY = this.moveY;
        this.movePosition(this.offsetSum);
    }
    touchEnd(event) {
        this.moveEndY = parseInt(event.changedTouches[0].clientY, 10);
        this.offset = (this.moveEndY - this.startY);
        this.getCurDistance();
        this.offsetSum = this.offset + this.curDistance;
        this.movePosition(this.offsetSum, 'end');
        // 修正位置 回彈效果
        this.modifyPosition(this.offsetSum);
    }
    getCurDistance() {
        if (this.slider.style.transform) {
            this.curDistance = parseInt(
                this.slider.style.transform.split(',')[1], 10) || 0;
        } else {
            this.curDistance = parseInt(
                this.slider.style.webkitTransform.split(',')[1], 10) || 0;
        }
    }
    movePosition(distance) {
        this.slider.style.webkitTransform = `translate3d(0,${distance}px, 0)`;
        this.slider.style.transform = `translate3d(0,${distance}px, 0)`;
    }
    modifyPosition(distance) {
        if (distance > this.modifyTopLimit) {
            this.curDistance = this.modifyTopLimit;
            this.movePosition(this.modifyTopLimit);
        } else if (distance < this.modifyBottomLimit) {
            this.curDistance = this.modifyBottomLimit;
            this.movePosition(this.modifyBottomLimit);
        }
    }
}


如何使用

    const dom = getElementsByClassName('lose-lottery-content')[0];
            const slider = getElementsByClassName('lose-lottery-container')[0];
            const itemHeight = getElementsByClassName('lottery-item')[0].clientHeight;
            const bottomlimit =
        getElementsByClassName('lose-lottery-container')[0].clientHeight -
        getElementsByClassName('lose-lottery-content')[0].clientHeight;
            // eslint-disable-next-line no-new
            new TouchMove({
                Dom: dom,
                slider,
                moveTopLimit: itemHeight,
                moveBottomLimit: -(bottomlimit + itemHeight),
                modifyTopLimit: 0,
                modifyBottomLimit: -(bottomlimit + 20),
            });

參考鏈接

https://github.com/surmon-china/vue-awesome-swiper/issues/423

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末批幌,一起剝皮案震驚了整個濱河市仍秤,隨后出現(xiàn)的幾起案子蛋褥,更是在濱河造成了極大的恐慌,老刑警劉巖浴捆,帶你破解...
    沈念sama閱讀 219,427評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異稿械,居然都是意外死亡选泻,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,551評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來页眯,“玉大人梯捕,你說我怎么就攤上這事∥涯欤” “怎么了傀顾?”我有些...
    開封第一講書人閱讀 165,747評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長碌奉。 經(jīng)常有香客問我短曾,道長,這世上最難降的妖魔是什么赐劣? 我笑而不...
    開封第一講書人閱讀 58,939評論 1 295
  • 正文 為了忘掉前任嫉拐,我火速辦了婚禮,結果婚禮上魁兼,老公的妹妹穿的比我還像新娘婉徘。我一直安慰自己,他們只是感情好咐汞,可當我...
    茶點故事閱讀 67,955評論 6 392
  • 文/花漫 我一把揭開白布盖呼。 她就那樣靜靜地躺著,像睡著了一般化撕。 火紅的嫁衣襯著肌膚如雪几晤。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,737評論 1 305
  • 那天侯谁,我揣著相機與錄音锌仅,去河邊找鬼。 笑死墙贱,一個胖子當著我的面吹牛热芹,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播惨撇,決...
    沈念sama閱讀 40,448評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼伊脓,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了魁衙?” 一聲冷哼從身側響起报腔,我...
    開封第一講書人閱讀 39,352評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎剖淀,沒想到半個月后纯蛾,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,834評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡纵隔,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,992評論 3 338
  • 正文 我和宋清朗相戀三年翻诉,在試婚紗的時候發(fā)現(xiàn)自己被綠了炮姨。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,133評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡碰煌,死狀恐怖舒岸,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情芦圾,我是刑警寧澤蛾派,帶...
    沈念sama閱讀 35,815評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站个少,受9級特大地震影響洪乍,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜稍算,卻給世界環(huán)境...
    茶點故事閱讀 41,477評論 3 331
  • 文/蒙蒙 一典尾、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧糊探,春花似錦钾埂、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,022評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至瞪慧,卻和暖如春髓考,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背弃酌。 一陣腳步聲響...
    開封第一講書人閱讀 33,147評論 1 272
  • 我被黑心中介騙來泰國打工氨菇, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人妓湘。 一個月前我還...
    沈念sama閱讀 48,398評論 3 373
  • 正文 我出身青樓查蓉,卻偏偏與公主長得像,于是被迫代替她去往敵國和親榜贴。 傳聞我的和親對象是個殘疾皇子豌研,可洞房花燭夜當晚...
    茶點故事閱讀 45,077評論 2 355

推薦閱讀更多精彩內(nèi)容

  • 簡說Vue (組件庫) https://github.com/ElemeFE/element" 餓了么出品的VUE...
    Estrus丶閱讀 1,662評論 0 1
  • 基于Vue的一些資料 內(nèi)容 UI組件 開發(fā)框架 實用庫 服務端 輔助工具 應用實例 Demo示例 element★...
    嘗了又嘗閱讀 1,154評論 0 1
  • 1-------- 走進前端 2-------- jQuery 3-------- CSS 4-------- A...
    依依玖玥閱讀 2,325評論 0 34
  • 時光走過了那么久,一轉眼就高三了唬党。其實心里都明白鹃共,對于未來的定義連自己都不知道。突然就得自己竟這樣傻傻的即將度過我...
    無塵詩人閱讀 167評論 0 3
  • 姓名:張嘉令 公司:上海中英閥門管件有限公司 【日精進打卡第43天 】 【知~學習】 《六項精進》1遍共47遍 《...
    天空_62f7閱讀 83評論 0 0