• 页面PC端 / 移动端整屏纵向翻页,点击/触摸滑动效果功能代码非插件



    页面翻页,滑动功能示范代码

    
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scale=no, maximum-scale=1.0">
      <meta http-equiv="X-UA-Compatible" content="ie=edge">
      <title>Pic_Test</title>
      <style>
        * {
          margin: 0;
          padding: 0;
        }
    
        html,
        body {
          height: 100%;
        }
    
        #pages-wrap {
           100%;
          height: 100%;
          overflow: hidden;
        }
    
        .pages-content {
          margin: 0 auto;
          height: 100%;
          background: rgba(7, 17, 27, 0.4);
        }
    
        .pages {
          height: 25%;
          text-align: center;
          font-size: 30px;
          color: #FFF;
        }
    
        .p1 {
          background: rgba(7, 17, 27, 0.2);
        }
    
        .p2 {
          background: rgba(174, 238, 238, 0.2);
        }
    
        .p3 {
          background: rgba(238, 162, 173, 0.2);
        }
    
        .p4 {
          background: rgba(152, 251, 152, 0.2);
        }
    
        @media screen and (min- 1000px) {
          .pages-content {
             600px;
          }
        }
      </style>
    </head>
    
    <body>
      <div id="pages-wrap">
        <div class="pages-content" style="height: 100%; transform: translate3d(0px, 0px, 0px); transition-duration: 0.3s">
          <div class="pages p1">第一页</div>
          <div class="pages p2">第二页</div>
          <div class="pages p3">第三页</div>
          <div class="pages p4">第四页</div>
        </div>
      </div>
      <script src="jquery-3.3.1.min.js"></script>
      <script type="text/javascript">
    
        // ==========================全局变量/函数==================================
        var start_y = 0,
          total_distance = 0,
          move_y = 0;
        var pages_wrap = $("#pages-wrap");
        var timer;
        var curr_index = 0; // 当前页
        var screen_height = $(document).height(); // 当前网页可以浏览区域高度
        var wrap = $(".pages-content:last-child");
        var pages = $(".pages");
        var total = pages.length; // 总页面数
        wrap.css("height", total * screen_height + "px") // 修改父容器初始高度
    
        // 下一页
        function nextPage() {
          curr_index++;
          if (curr_index <= total - 1) {
            var move_y = -(curr_index * screen_height);
            wrap.css("transform", "translate3d(0px," + move_y + "px, 0px)");
          } else {
            curr_index = total - 1
            return
          }
        }
    
        // 上一页
        function prevPage() {
          curr_index--;
          if (curr_index >= 0) {
            var move_y = -(curr_index * screen_height);
            wrap.css("transform", "translate3d(0px," + move_y + "px, 0px)");
          } else {
            curr_index = 0
            return
          }
        }
    
        // 鼠标/手指拖动、翻页功能
        function movePage(client) {
          var start_time = null,
            end_time = null;
          var down = client == "pc" ? "mousedown" : "touchstart";
          var move = client == "pc" ? "mousemove" : "touchmove";
          var up = client == "pc" ? "mouseup" : "touchend";
          var obj = client == "pc" ? $(window) : wrap;
          obj.on(down, function (e) {
            e.preventDefault();
            total_distance = 0;
            wrap.css("transition-duration", "0s");  // 修改动画时间避免造成拖动延迟不流畅
            start_y = client == "pc" ? e.clientY : e.changedTouches[0].clientY;
            start_time = new Date().getTime();   // 移动端判断手指停留屏幕开始时间
            obj.on(move, function (e) {
              e.preventDefault();
              var clientY = client == "pc" ? e.clientY : e.changedTouches[0].clientY;
              var distance_y = clientY - start_y;
              total_distance += distance_y;
              var arr = wrap.css("transform").split(",")
              var y = user_agent.indexOf('rv') > 0 ? parseInt(arr[13].match(/-?d+/g)) : parseInt(arr[5].match(/-?d+/g));   // 利用正则,获取样式内Y轴距离
              move_y = y + distance_y;
              start_y = clientY;   // 修改开始Y轴为当前Y轴坐标,为下一个移动距离做基准
              wrap.css("transform", "translate3d(0px," + move_y + "px, 0px)");
            });
          });
          obj.on(up, function (e) {
            e.preventDefault();
            end_time = new Date().getTime();   // 移动端判断手指停留屏幕结束时间
            wrap.css("transition-duration", "0.3s");
            wrap.off(move);
            if (client == "pc") { obj.off(move); }
            function isMovePage() {   // 判断上翻、下翻、回弹
              if (total_distance < 0 && move_y >= -(screen_height * (total - 1))) {
                nextPage();
              } else if (total_distance > 0 && move_y < 0) {
                prevPage();
              } else {
                move_y = -(curr_index * screen_height);
                wrap.css("transform", "translate3d(0px," + move_y + "px, 0px)");
              }
            }
            if (end_time - start_time <= 200 && Math.abs(total_distance) >= 100) {   // 判断鼠标/手指滑动时间和距离,符合要求直接翻页
              isMovePage();
            } else {
              if (Math.abs(total_distance) >= screen_height / 2) {
                isMovePage();
              } else {
                move_y = -(curr_index * screen_height);
                wrap.css("transform", "translate3d(0px," + move_y + "px, 0px)"); // 定义移动距离不够,回弹当前页面
              }
            }
          });
        }
        // 判断打开页面的平台
        var user_agent = navigator.userAgent.toLowerCase();
    
        // 判断是否微信浏览器打开
        function isWeixn(ua) {
          if (ua.match(/MicroMessenger/i) == "micromessenger") {
            return true;
          } else {
            return false;
          }
        }
    
        // =========================PC端事件===================================
    
        // 判断为windows环境
        if (user_agent.indexOf('windows') > 0) {
          // PC端滚轮滑动翻页
          pages_wrap.on("mousewheel", function (e) {
            clearTimeout($.data(this, "timer"));
            $.data(this, "timer", setTimeout(function () {
              if (e.originalEvent.wheelDelta < 0) {
                nextPage();
              }
              if (e.originalEvent.wheelDelta > 0) {
                prevPage();
              }
            }, 100))
          });
    
          // PC端鼠标拖动翻页
          wrap.on({   // 利用冒泡,达到鼠标在window对象也可以拖动页面滚动
            "mousedown": function () {
              wrap.on("mousemove", function () { });
            },
            "mouseup": function () { }
          })
          movePage("pc");
    
        }
    
        // ==========================手机端事件==================================
    
        // 判断为移动端
        if (user_agent.indexOf("iphone") > 0 || user_agent.indexOf("ipad") > 0 || user_agent.indexOf("ipod") > 0 || user_agent.indexOf("android") > 0) {
          movePage("mobile");
          if (isWeixn(user_agent)) {
    
          } else {
    
          }
        }
    
      </script>
    </body>
    
    </html>
    
    
  • 相关阅读:
    iOS开发 关于启动页和停留时间的设置
    iOS应用开发,全局强制竖屏,部分页面允许旋转的处理
    iOS利用Application Loader打包提交到App Store时遇到错误The filename 未命名.ipa in the package contains an invalid character(s). The valid characters are:A-Z ,a-z,0-9,dash,period,underscore,but the name cannot start w
    iOS之加载Gif图片
    Node以数据块的形式读取文件
    Nodejs日志管理包
    Java操作SFTP
    Nginx+Nodejs搭建图片服务器
    使用Atlas实现MySQL读写分离
    MySQL-(Master-Slave)配置
  • 原文地址:https://www.cnblogs.com/lovellll/p/10208223.html
Copyright © 2020-2023  润新知