移动WEB-使用hammer.js实现滑动门(carousel )

1.首先需要引入jQuery文件和hammerJS的插件:

http://www.bootcdn.cn/jquery/

 

2.文档结构设计

    <div id="carousel">
        <ul>
            <li class="panel1">
                <h2>左滑下一页</h2>
            </li>

            <li class="panel2">
                <h2>向左拖拽也可以</h2>
            </li>

            <li class="panel3">
                <h2>右滑上一页</h2>
            </li>

            <li class="panel4">
                <h2>凑页数</h2>
            </li>
            <li class="panel5">
                <h2>再凑一页</h2>
            </li>
        </ul>
    </div>

3.样式设计:

    <style type="text/css">
        html,body,ul,li{
            padding: 0;
            margin: 0;
        }
        #carousel,#carousel ul,#carousel li{
            min-height: 400px;
            position: relative;
            overflow:hidden;
        }
        #carousel{
            background-color: silver;
            width:100%;
            -webkit-backface-visibility:hidden;
            -webkit-transform:translate3d(0,0,0) scale3d(1,1,1);
            -webkit-transform-style:preserve-3d;
        }
        #carousel ul.animate{
            -webkit-transition:all .3s;
        }
        #carousel ul{
            -webkit-transform:translate3d(0%,0,0) scale3d(1,1,1);
            -webkit-transform-style:preserve-3d;
            -webkit-backface-visibility:hidden;
            box-shadow: 0 0 20px rgba(0,0,0,2);
        }
        #carousel li{
            float: left;
            -webkit-transform-style:preserve-3d;
            -webkit-transform:translate3d(0,0,0);
        }
        #carousel li h2{
            color:#fff;
            font-size: 30px;
            text-align: center;
            position:absolute;
            top:40%;
            left:0;
            width:100%;
            text-shadow:-1px -1px 0 rgba(0,0,0,2);
        }
        #carousel li.panel1{background-color: #42d692;}
        #carousel li.panel2{background-color: #4986e7;}
        #carousel li.panel3{background-color: #d06b64;}
        #carousel li.panel4{background-color: #cd74e6;}
        #carousel li.panel5{background-color: #9fe1e7;}
    </style>

4.JS操作:

主要是通过监听手指移动的变化,包括drag,swipe和release的判断,再根据所在面板与面板总宽度的比值,对面板的位置属性进行改变。

    <script type="text/javascript">
        function Carousel(selector){
            var self=this,
                element=$(selector),
                container=$(">ul",element),
                panes=$(">ul>li",element),
                paneWidth=0,
                paneCount=panes.length,
                currentPane=0;

            this.init = function(){
                setPaneDimensions();
                $(window).on("load resize orientationchange",function(){
                    setPaneDimensions();
                })
            }

            function setPaneDimensions(){
                paneWidth=element.width();
                console.log(paneWidth);
                panes.each(function(){
                    $(this).width(paneWidth);
                })
                container.width(paneWidth*paneCount);
            }
            this.showPane=function(index){
                index=Math.max(0,Math.min(index,paneCount-1));
                currentPane=index;
                var offset=-((100/paneCount)*currentPane);
                setContainerOffset(offset,true);
            }
            function setContainerOffset(percent,animate){
                container.removeClass('animate');
                if(true){
                    container.addClass("animate")
                }
                container.css("transform","translate3d("+percent+"%,0,0) scale3d(1,1,1)")
            }
            this.next=function(){
                return this.showPane(currentPane+1,true);
            }
            this.prev=function(){
                return this.showPane(currentPane-1,true);
            }
            function handleHammer(e){
                e.gesture.preventDefault();
                switch (e.type){
                    case 'dragleft':
                    case 'dragright':
                        var paneOffset=-(100/paneCount)*currentPane;
                        var dragOffet=-(100/paneWidth)*e.gesture.deltaX/paneCount;
                        if((currentPane===0&&e.gesture.direction==='right')||(currentPane===paneCount-1&&e.gesture.direction==='left')){
                            dragOffet*=0.4;
                        }
                        setPaneDimensions(paneOffset+dragOffet);
                        break
                    case 'swipeleft':
                        self.next();
                        e.gesture.stopDetect()
                        break

                    case 'swiperight':
                        self.prev();
                        e.gesture.stopDetect()
                        break
                    case 'release':
                        if(Math.abs(e.gesture.deltaX)>paneWidth/2){
                            if(e.gesture.direction==='right'){
                                self.prev();
                            }
                            else{
                                self.next();
                            }
                        }
                        else{
                            self.showPane(currentPane,true)
                        }
                        break
                }
            }
            element.hammer({
                drag_lock_to_axis:true
            }).on("release dragleft dragright swipeleft swiperight",handleHammer)
        }
        var carousel=new Carousel("#carousel");
        carousel.init();
    </script>

 

转载于:https://www.cnblogs.com/YutaoZhou/p/6691750.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值