translate3d模拟滚动条

translate3d模拟滚动条做移动端页面,通常是不用原生的scroll,而是用translate3d来模拟,原因主要是原生的scroll对移动端的支持并不是很好,样式也不好看(有滚动条出现),用translate3d来模拟还可以调用GPU来加速,提高性能。html:

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全家桶1年46,售后保障稳定

做移动端页面,通常是不用原生的scroll,而是用translate3d来模拟,原因主要是原生的scroll对移动端的支持并不是很好,样式也不好看(有滚动条出现),用translate3d来模拟还可以调用GPU来加速,提高性能。

html:

   <div class="wrap">
        <div id="listContent">
                <ul id="listContentUl">
                    <li style="background: hotpink; height: 50px">我是header</li>
                    <li style="background: yellowgreen; height: 600px">我是content</li>
                    <li style="background: aqua; height: 50px">我是footer</li>
                </ul>
        </div>
    </div>

Jetbrains全家桶1年46,售后保障稳定

css:

        * { margin: 0; padding: 0; }
        htmlbody{ width:100%; height:100%; }
        body { background: grey }
        .wrap { margin: 100px auto 0; width: 400px; height: 500px; border: 1px solid #000; font-size: 30px; }
        #listContent { position: relative; width: 400px; height: 500px; overflow: hidden; }
        #listContentUl { position:absolute; top:0; left:0; transform:translate3d(0,0,0); }
        #listContentUl li{ list-style:none; width: 400px; height: 80px; border-bottom: 1px solid grey; }

js(基于jQuery):

$(function (){ 
   

            var viewWidth = $(window).width();
            var viewHeight = $(window).height();
            var desWidth = 640;
            var touchstart = 'touchstart';
            var touchmove = 'touchmove';
            var touchend = 'touchend';

            var $listContent = $('#listContent');
            var $listContentUl = $('#listContentUl');            

            var downY = 0;
            var prevY = 0;
            var downT = 0;
            var parentH = $listContent.height();
            var childH = $listContentUl.height();
            var onoff1 = true;
            var onoff2 = true;
            var timer = null;
            var speed = 0;

        function device(){ 
     
            var isMobile = /Mobile/i.test(navigator.userAgent);
            if(viewWidth > desWidth){
                $listContent.css('width','640px');
            }
            if(!isMobile){
                touchstart = 'mousedown';
                touchmove = 'mousemove';
                touchend = 'mouseup';
            }
        }

        function moveScroll(){ 
      
            $(document).on(touchmove,function(ev){ 
   
                ev.preventDefault();       //苹果手机滑动时 整个页面都滑动,阻止其默认事件
            });        
            $listContentUl.on(touchstart,function(ev){ 
   
                if(parentH > childH){
  
  return false;}
                var touch = ev.originalEvent.changedTouches ? ev.originalEvent.changedTouches[0] : ev;
                var This = this;
                downY = touch.pageY;
                prevY = touch.pageY;
                downT = $(this).position().top;
                onoff1 = true;
                onoff2 = true;
                clearInterval(timer);
                $(document).on(touchmove+'.move',function(ev){ 
   
                    var touch = ev.originalEvent.changedTouches ? ev.originalEvent.changedTouches[0] : ev;
                    var iTop = $(This).position().top;

                    speed = touch.pageY - prevY;
                    prevY = touch.pageY;

                    if(iTop >= 0){   
                        if(onoff1){
                            onoff1 = false;
                            downY = touch.pageY;
                        }
                        $(This).css('transform','translate3d(0,'+(touch.pageY - downY)/3+'px,0)');
                    }
                    else if(iTop <= parentH - childH){  
                        if(onoff2){
                            onoff2 = false;
                            downY = touch.pageY;
                        }
                        $(This).css('transform','translate3d(0,'+((touch.pageY - downY)/3 + (parentH - childH))+'px,0)');
                    }
                    else{
                        $(This).css('transform','translate3d(0,'+(touch.pageY - downY + downT)+'px,0)');
                    }

                });
                $(document).on(touchend+'.move',function(){ 
   
                    $(this).off('.move');

                    clearInterval(timer);
                    timer = setInterval(function(){ 
   
                        var iTop = $(This).position().top;
                        if(Math.abs(speed) <= 1 || iTop > 50 || iTop < parentH - childH - 50){
                            clearInterval(timer);
                            if(iTop >= 0){
                                $(This).css('transition','.2s');
                                $(This).css('transform','translate3d(0,0,0)');
                            }
                            else if(iTop <= parentH - childH){
                                $(This).css('transition','.2s');
                                $(This).css('transform','translate3d(0,'+(parentH - childH)+'px,0)');
                            }
                        }
                        else{
                            speed *= 0.9;
                            $(This).css('transform','translate3d(0,'+(iTop + speed)+'px,0)');
                        }

                    },13);

                });
                return false;
            });
            $listContentUl.on('transitonend webkitTransitionEnd',function(){ 
   
                $(this).css('transition','');
            });
        }

            device();
            moveScroll();

pc端:
这里写图片描述

移动端:
这里写图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/215883.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 自动化测试之超厉害自动化录制工具介绍

    自动化测试之超厉害自动化录制工具介绍

    2021年9月17日
    149
  • idea 正则表达式替换「建议收藏」

    idea 正则表达式替换「建议收藏」例如,现在有一个函数getView(o,’m’,’orderNum’,’s’),这个函数的调用有很多地方,有一天我们想给这个函数加一个默认参数,让它变成getView(o,’m’,’orderNum’,’s’,’*’),一个一个加太麻烦了,我们就用idea自带的正则表达式替换功能来实现。首先按Ctrl+R,打开替换对话框,然后在上面输入getView\((.+)\),下面输…

    2022年5月17日
    316
  • anaconda和python版本对照表

    anaconda和python版本对照表python2 python3 anaconda2/3 2.7.14 3.6.5 5.2.0 2.7.14 3.6.4 5.1.0 2.7.14 3.6.3 5.0.1 2.7.13 3.6.2 5.0.0 2.7.13 3.6.1 4.4.0 2.7.13 3.6.0 4.3.1 2….

    2022年5月28日
    484
  • httprunner3源码解读(2)models.py「建议收藏」

    httprunner3源码解读(2)models.py「建议收藏」源码目录结构我们首先来看下models.py的代码结构我们可以看到这个模块中定义了12个属性和22个模型类,我们依次来看属性源码分析importosfromenumimportEnu

    2022年7月29日
    18
  • windows下怎么安装laravel框架

    windows下怎么安装laravel框架windows下怎么安装laravel框架

    2022年4月24日
    42
  • 【用户画像】从0到1掌握用户画像知识体系

    【用户画像】从0到1掌握用户画像知识体系一、初始用户画像1.1用户画像随着用户的一切行为数据可以被企业追踪到,企业的关注点日益聚焦在如何利用大数据为经营分析和精准营销服务,而要做精细化运营,首先要建立本企业的用户画像。提到用户画像的概念,我们区分下用户角色(Persona)和用户画像(Profile):1.1.1用户角色用户角色本质是一个用以沟通的工具,当我们讨论产品、需求、场景、用户体验的时候,为了避免在目标用户理解上的分歧,用户角色应运而生。用户角色建立在对真实用户深刻理解,及高精准相关数据的概括之上,虚构的包含典型用

    2022年5月25日
    97

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号