页面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 {
width: 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-width: 1000px) {
.pages-content {
width: 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>
页面PC端 / 移动端整屏纵向翻页,点击/触摸滑动效果功能代码非插件的更多相关文章
- 使用scrollpagination实现页面底端自动加载无需翻页功能
当阅读到页面最底端的时候,会自动显示一个"加载中"的功能,并自动从服务器端无刷新的将内容下载到本地浏览器显示. 这样的自动加载功能是如何实现的?jQuery的插件 ScrollPa ...
- 简易数据分析 08 | Web Scraper 翻页——点击「更多按钮」翻页
这是简易数据分析系列的第 8 篇文章. 我们在Web Scraper 翻页--控制链接批量抓取数据一文中,介绍了控制网页链接批量抓取数据的办法. 但是你在预览一些网站时,会发现随着网页的下拉,你需要点 ...
- pc端vue 滚动到底部翻页
html: <div class="list" ref="scrollTopList"> <div class="listsmall ...
- Atitit.列表页面and条件查询的实现最佳实践(2)------翻页 分页 控件的实现java .net php
)------翻页 分页 控件的实现java .net php 1. 关于翻页有关的几大控件::搜索框控件,显示表格控件,翻页器,数据源控件.. 1 2. 翻页的显示格式:: 1 2.1. 通常ui- ...
- 通过js实现整屏滑动+全屏翻页+动画展示+线性图
技术:html+css+jquery+jquery-ui.js+jquery.fullPage.js 概述 本demo主要通过html+css+js实现整屏滑动,全屏翻页并带动画的功能效果,借助于 ...
- turn.js中文API 写一个翻页效果的参数详细解释
$('.flipbook').turn({ width: 922, height: 600, elevation: 50, gradients: true, a ...
- webapp应用--模拟电子书翻页效果
前言: 现在移动互联网发展火热,手机上网的用户越来越多,甚至大有超过pc访问的趋势.所以,用web程序做出仿原生效果的移动应用,也变得越来越流行了.这种程序也就是我们常说的单页应用程序,它也有一个英文 ...
- Android水平(横向)翻页列表,类似水平GridVIew
Android水平(横向)翻页列表,类似于水平方向的GridView,行列自定义,但要翻页切换,考虑加载性能,当Item数据很多时加载和翻页要流畅,翻页时要有动画效果,效果图如下: 实现方式: 1:翻 ...
- vue2.X简单翻页/分页
由于业务需要 公司把后台所有数据一次性给前端,数据过多,所以前端需要做一些分页的处理,比较简单的翻页. html代码 <table class="three_td"> ...
随机推荐
- VS2010-MFC(对话框:创建对话框类和添加控件变量)
转自:http://www.jizhuomi.com/software/153.html 前两讲中讲解了如何创建对话框资源.创建好对话框资源后要做的就是生成对话框类了.生成对话框类主要包括新建对话框类 ...
- 33 N皇后问题
原题网址:https://www.lintcode.com/zh-cn/old/problem/n-queens/# n皇后问题是将n个皇后放置在n*n的棋盘上,皇后彼此之间不能相互攻击. 给定一个整 ...
- System.Web.Mvc.FileStreamResult.cs
ylbtech-System.Web.Mvc.FileStreamResult.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neutral, P ...
- <scrapy爬虫>爬取腾讯社招信息
1.创建scrapy项目 dos窗口输入: scrapy startproject tencent cd tencent 2.编写item.py文件(相当于编写模板,需要爬取的数据在这里定义) # - ...
- Asp.net Core Jenkins Docker 实现一键化部署
写在前面 在前段时间尝试过用Jenkins来进行asp.net core 程序在IIS上面的自动部署.大概的流程是Jenkins从git上获取代码 最开始Jenkins是放在Ubuntu的Docker ...
- js格式化数字为金额
/** * * @param num * @param precision * @param separator * @returns {*} *=========================== ...
- 对话框处理Enter,Esc键相应问题
在类视图里面选择你要实现的类,右键属性,在属性里面找到函数PreTranslateMessage,然后添加PreranslateMessage的消息函数,在PreTranslateMessage的消息 ...
- linux支持大容量硬盘
1.fdisk使用msdos格式分区,最大支持2T硬盘,要使用大于2T硬盘需使用parted命令使用GPT格式分区. 2.除修改分区格式外,linux内核需添加GPT分区格式支持,修改如下: CONF ...
- Centos7解决在同一局域网内无法使用ssh连接
参考: https://www.cnblogs.com/liyuanhong/articles/5785368.html 一.修改网卡设置 nano /etc/sysconfig/network-sc ...
- Ubuntu 14.04 Ruby 2.3.3 安装
在Ubuntu 14.04通过下载Ruby源码包进行安装. 第一步,更新apt-get sudo apt-get update 通过apt-get安装ruby依赖 sudo apt-get insta ...