search

Home  >  Q&A  >  body text

css3 - 淘宝网上这种抵抗反弹的效果如何做?

https://jhs.m.taobao.com/m/index.htm?locate=icon-2&spm=a215s.7406091.1.icon-2&scm=2027.1.3.1001#!3000

上面的横向滚动条,滑到边界就会有个拉伸的效果,然后再反弹回去
用css3和js该怎么实现

怪我咯怪我咯2872 days ago1345

reply all(5)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 11:40:59

    For local scrolling, the easiest way is to use IScroll. Just instantiate it directly. new IScroll("",) ""Inside is the selector.

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 11:40:59

    The following JS code can be used as a reference, which can not only achieve rebound, but also reflect the rapid sliding of gestures.

    <script type="text/javascript">
            //得到各种元素
            var nav = document.querySelector("nav");
            var navul = document.querySelector("nav ul");
            var navullis = document.querySelectorAll("nav ul li");
            var navW = parseInt(window.getComputedStyle(nav , null)['width']);
    
            //宽度
            navul.style.width = navullis.length * 82 +"px";
    
            nav.addEventListener("touchstart",touchstartHandler);
            nav.addEventListener("touchmove",touchmoveHandler);
            nav.addEventListener("touchend",touchendHandler);
    
            var startX;
            var nowX = 0;
            var dX;
    
            var lastTwoPoint = [0,0];
    
            //开始滑动
            function touchstartHandler(event){
                navul.style.webkitTransition = "none";    //去掉过渡
                navul.style.transition = "none";    //去掉过渡
                startX = event.touches[0].pageX;    //记录起点
            }
    
            //滑动过程
            function touchmoveHandler(event){
                event.preventDefault();
                dX = event.touches[0].pageX - startX;    //差值
    
                //反映差值
                navul.style.webkitTransform = "translateX(" + (nowX + dX) + "px)";
                navul.style.transform = "translateX(" + (nowX + dX) + "px)";
    
                //记录最后两点的x值
                lastTwoPoint.shift();
                lastTwoPoint.push(event.touches[0].pageX);
            }
    
            //结束滑动
            function touchendHandler(event){
                nowX += dX;
    
                //多走最后两点路程的5倍路程
                nowX += (lastTwoPoint[1] - lastTwoPoint[0]) * 5;
                if(nowX > 0){
                    nowX = 0;
                }
    
                if(nowX < -parseInt(navul.style.width) + navW){
                    nowX = -parseInt(navul.style.width) + navW;
                }
    
                console.log(-parseInt(navul.style.width) + navW)
                //过渡时间
                //非线性衰减
                var t = Math.sqrt(Math.abs(lastTwoPoint[1] - lastTwoPoint[0])) / 10;
    
                navul.style.webkitTransition = "all " + t + "s cubic-bezier(0.1, 0.85, 0.25, 1) 0s"; 
                navul.style.transition = "all " + t + "s cubic-bezier(0.1, 0.85, 0.25, 1) 0s"; 
    
                //反映多走的5倍路程:
                navul.style.webkitTransform = "translateX(" + nowX + "px)";
                navul.style.transform = "translateX(" + nowX + "px)";
            }
        </script>

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 11:40:59

    isscroll can do this effect. If you are asking about the native implementation, you can take a look at the source code.
    Portal

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 11:40:59

    Do you mean elastic scrolling

    body{
         overflow:scroll;
         -webkit-overflow-scrolling:touch
    }
    

    reply
    0
  • 黄舟

    黄舟2017-04-17 11:40:59

    Recently I am also using IScroll to do projects, and I find that more and more mobile phones after Android 5.0 are incompatible, especially when making single pages. Recently I am using this Swiper [1]: http://www .swiper.com.cn/, the document is in Chinese and has been published for several versions. So far, we have not encountered any compatibility issues

    reply
    0
  • Cancelreply