资讯专栏INFORMATION COLUMN

手机端滑屏翻页,页面元素动画

YancyYe / 2256人阅读

摘要:引入文件页面结构控制翻屏逻辑直接跳到第页设置滑动页面的统一样式

引入touch.js文件
//     Zepto.js
//     (c) 2010-2014 Thomas Fuchs
//     Zepto.js may be freely distributed under the MIT license.

;(function($){
    var touch = {},
        touchTimeout, tapTimeout, swipeTimeout, longTapTimeout,
        longTapDelay = 750,
        gesture

    function swipeDirection(x1, x2, y1, y2) {
        return Math.abs(x1 - x2) >=
        Math.abs(y1 - y2) ? (x1 - x2 > 0 ? "Left" : "Right") : (y1 - y2 > 0 ? "Up" : "Down")
    }

    function longTap() {
        longTapTimeout = null
        if (touch.last) {
            touch.el.trigger("longTap")
            touch = {}
        }
    }

    function cancelLongTap() {
        if (longTapTimeout) clearTimeout(longTapTimeout)
        longTapTimeout = null
    }

    function cancelAll() {
        if (touchTimeout) clearTimeout(touchTimeout)
        if (tapTimeout) clearTimeout(tapTimeout)
        if (swipeTimeout) clearTimeout(swipeTimeout)
        if (longTapTimeout) clearTimeout(longTapTimeout)
        touchTimeout = tapTimeout = swipeTimeout = longTapTimeout = null
        touch = {}
    }

    function isPrimaryTouch(event){
        return (event.pointerType == "touch" ||
            event.pointerType == event.MSPOINTER_TYPE_TOUCH)
            && event.isPrimary
    }

    function isPointerEventType(e, type){
        return (e.type == "pointer"+type ||
        e.type.toLowerCase() == "mspointer"+type)
    }

    $(document).ready(function(){
        var now, delta, deltaX = 0, deltaY = 0, firstTouch, _isPointerType

        if ("MSGesture" in window) {
            gesture = new MSGesture()
            gesture.target = document.body
        }

        $(document)
            .bind("MSGestureEnd", function(e){
                var swipeDirectionFromVelocity =
                    e.velocityX > 1 ? "Right" : e.velocityX < -1 ? "Left" : e.velocityY > 1 ? "Down" : e.velocityY < -1 ? "Up" : null;
                if (swipeDirectionFromVelocity) {
                    touch.el.trigger("swipe")
                    touch.el.trigger("swipe"+ swipeDirectionFromVelocity)
                }
            })
            .on("touchstart MSPointerDown pointerdown", function(e){
                if((_isPointerType = isPointerEventType(e, "down")) &&
                    !isPrimaryTouch(e)) return
                //console.log(e);
                firstTouch = _isPointerType ? e : e.originalEvent.touches[0]
                if (e.touches && e.touches.length === 1 && touch.x2) {
                    // Clear out touch movement data if we have it sticking around
                    // This can occur if touchcancel doesn"t fire due to preventDefault, etc.
                    touch.x2 = undefined
                    touch.y2 = undefined
                }
                now = Date.now()
                delta = now - (touch.last || now)
                touch.el = $("tagName" in firstTouch.target ?
                    firstTouch.target : firstTouch.target.parentNode)
                touchTimeout && clearTimeout(touchTimeout)
                touch.x1 = firstTouch.pageX
                touch.y1 = firstTouch.pageY
                if (delta > 0 && delta <= 250) touch.isDoubleTap = true
                touch.last = now
                longTapTimeout = setTimeout(longTap, longTapDelay)
                // adds the current touch contact for IE gesture recognition
                if (gesture && _isPointerType) gesture.addPointer(e.pointerId);
            })
            .on("touchmove MSPointerMove pointermove", function(e){
                // e.preventDefault()
                if((_isPointerType = isPointerEventType(e, "move")) &&
                    !isPrimaryTouch(e)) return
                firstTouch = _isPointerType ? e : e.originalEvent.touches[0]
                cancelLongTap()
                touch.x2 = firstTouch.pageX
                touch.y2 = firstTouch.pageY

                deltaX += Math.abs(touch.x1 - touch.x2)
                deltaY += Math.abs(touch.y1 - touch.y2)
            })
            .on("touchend MSPointerUp pointerup", function(e){
                if((_isPointerType = isPointerEventType(e, "up")) &&
                    !isPrimaryTouch(e)) return
                cancelLongTap()

                // swipe
                if ((touch.x2 && Math.abs(touch.x1 - touch.x2) > 30) ||
                    (touch.y2 && Math.abs(touch.y1 - touch.y2) > 30))

                    swipeTimeout = setTimeout(function() {
                        touch.el.trigger("swipe")
                        touch.el.trigger("swipe" + (swipeDirection(touch.x1, touch.x2, touch.y1, touch.y2)))
                        touch = {}
                    }, 0)

                // normal tap
                else if ("last" in touch)
                // don"t fire tap when delta position changed by more than 30 pixels,
                // for instance when moving to a point and back to origin
                    if (deltaX < 30 && deltaY < 30) {
                        // delay by one tick so we can cancel the "tap" event if "scroll" fires
                        // ("tap" fires before "scroll")
                        tapTimeout = setTimeout(function() {

                            // trigger universal "tap" with the option to cancelTouch()
                            // (cancelTouch cancels processing of single vs double taps for faster "tap" response)
                            var event = $.Event("tap")
                            event.cancelTouch = cancelAll
                            touch.el.trigger(event)

                            // trigger double tap immediately
                            if (touch.isDoubleTap) {
                                if (touch.el) touch.el.trigger("doubleTap")
                                touch = {}
                            }

                            // trigger single tap after 250ms of inactivity
                            else {
                                touchTimeout = setTimeout(function(){
                                    touchTimeout = null
                                    if (touch.el) touch.el.trigger("singleTap")
                                    touch = {}
                                }, 250)
                            }
                        }, 0)
                    } else {
                        touch = {}
                    }
                deltaX = deltaY = 0

            })
            // when the browser window loses focus,
            // for example when a modal dialog is shown,
            // cancel all ongoing events
            .on("touchcancel MSPointerCancel pointercancel", cancelAll)

        // scrolling the window indicates intention of the user
        // to scroll, not tap or swipe, so cancel all ongoing events
        $(window).on("scroll", cancelAll)

    });
    
    ["swipe", "swipeLeft", "swipeRight", "swipeUp", "swipeDown",
        "doubleTap", "tap", "singleTap", "longTap"].forEach(function(eventName){
            $.fn[eventName] = function(callback){ return this.on(eventName, callback) }
        })

    document.addEventListener("touchmove", function(event) {
        event.preventDefault();
    }, { passive: false });
})(jQuery)
html页面结构
js 控制翻屏逻辑
var pageLength = $(".page").length;
$(".wrapper").bind("swipeUp", function () {
    pageNum++;
    if (pageNum > pageLength) {
        pageNum = pageLength;
        return
    }
    toPage(pageNum - 1, pageNum);
}).bind("swipeDown", function () {
    pageNum--;
    if (pageNum <= 0) {
        pageNum = 1;
        return
    }
    toPage(pageNum + 1, pageNum);
});
$(".page2-btn").click(function(){
    directTo(3); // 直接跳到第3页
});
function toPage(currentNum, nextNum) {
    if (swipeFlag) {
        return
    }
    swipeFlag = true;
    setTimeout(function () {
        $(".page").removeClass("active");
        $(".page" + nextNum).addClass("active");
        // if (nextNum == 2) {
        //     setTimeout(function () {
        //         $(".hand").fadeIn();
        //     }, 2000)
        // } else {
        //     $(".hand").fadeOut();
        // }
        swipeFlag = false;
    } , 1400);  
}
function directTo(pageNum) {
    $(".page").removeClass("active");
    $(".page" + pageNum).addClass("active");
}
css设置
$speed: 1;
$durations: 1s;
html,
body {
    width: 100%;
    height: 100%;
    overflow: hidden;
    background: #141a28;
}
.pages {
    width: 100%;
    height: 100%;
    position: relative;
}

.page {
    display: none;
    /*滑动页面的统一样式 */
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
    // transform: translate3d(0px, 100%, 0px);
    // -webkit-transform: translate3d(0px, 100%, 0px);
    transition: all .5s ease-out;
    -webkit-transition: all .5s ease-out;
}
.active {
    display: block !important;
}
.active{
    animation: pageFadeIn .5s ease-out both;
    -webkit-animation: pageFadeIn .5s ease-out both;
}
.page1.active{
    .page1-img1{
        -webkit-animation: pulse $durations 1s * $speed ease-out backwards;
        animation: pulse $durations 1s * $speed ease-out backwards;
    }
    .page1-img2{
        -webkit-animation: fadeIn $durations .5s * $speed ease-out backwards;
        animation: fadeIn $durations .5s * $speed ease-out backwards;
    }
    .page1-img3{
        -webkit-animation: fadeIn $durations 1.5s * $speed ease-out backwards;
        animation: fadeIn $durations 1.5s * $speed ease-out backwards;
    }
}
@keyframes pulse {
    0% {
        opacity: 0;
        transform: scaleX(1)
    }

    50% {
        transform: scale3d(1.05,1.05,1.05)
    }

    to {
        opacity: 1;
        transform: scaleX(1)
    }
}
@keyframes fadeIn {
    0% {
        opacity: 0
    }

    to {
        opacity: 1
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/89821.html

相关文章

  • 分享一个移动端滑到底部翻页的代码

    摘要:如果有其他需求,比如返回例子中直接返回,可以修改中的代码,就不赘述了 今天在技术群看到有朋友有需求,就随手写了一个,大家随便看看~demo地址:http://www.dtzhuanjia.com/pri...(备注:这个主要用在移动端,所以用rem随便写了写样式= =PC看着不舒服用模拟器看吧) html: 下一页 ...

    heartFollower 评论0 收藏0
  • 分享一个移动端滑到底部翻页的代码

    摘要:如果有其他需求,比如返回例子中直接返回,可以修改中的代码,就不赘述了 今天在技术群看到有朋友有需求,就随手写了一个,大家随便看看~demo地址:http://www.dtzhuanjia.com/pri...(备注:这个主要用在移动端,所以用rem随便写了写样式= =PC看着不舒服用模拟器看吧) html: 下一页 ...

    hzc 评论0 收藏0
  • 分享一个移动端滑到底部翻页的代码

    摘要:如果有其他需求,比如返回例子中直接返回,可以修改中的代码,就不赘述了 今天在技术群看到有朋友有需求,就随手写了一个,大家随便看看~demo地址:http://www.dtzhuanjia.com/pri...(备注:这个主要用在移动端,所以用rem随便写了写样式= =PC看着不舒服用模拟器看吧) html: 下一页 ...

    kumfo 评论0 收藏0
  • 随手记 - 疯狂触发滚轮事件的Mac触控板

    摘要:这个来的莫名其妙,问了一圈人也没什么思路,后来自己上网搜,在一个页面上找到一段关于的触控板的手势滑动会疯狂触发滚轮事件的记录,但是轮到具体的解决方案就语焉不详了。 头几天官网刚上线,就接到投诉说有问题。过去一看,我靠什么鬼?!Mac下用触控板一滑到底,——首页上用iscroll写的翻页效果直接全军覆没。 这个bug来的莫名其妙,问了一圈人也没什么思路,后来自己上网搜,在一个页面上找到一...

    RobinTang 评论0 收藏0
  • 第六天 移动端Web开发注意事项

    摘要:随着移动互联网的发展,移动已经逐渐成为互联网的主要入口,随之而来的是前端在移动开发上面临的各种机遇与挑战,本文就一些常见移动端问题对移动开发需要注意的事项进行一下总结,必然不可能涉及方方面面,但会随着笔者的积累持续更新。 随着移动互联网的发展,移动Web已经逐渐成为互联网的主要入口,随之而来的是前端在移动Web开发上面临的各种机遇与挑战,本文就一些常见移动端问题对移动Web开发需要注意...

    妤锋シ 评论0 收藏0

发表评论

0条评论

YancyYe

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<