1
0
mirror of https://github.com/suk-ws/ph-Bookshelf.git synced 2024-12-05 09:26:52 +08:00

添加左滑关闭侧边栏支持

This commit is contained in:
A.C.Sukazyo Eyre 2021-11-22 23:31:05 +08:00
parent 40f6ebcfdf
commit 6a56ee1730
Signed by: Eyre_S
GPG Key ID: EFB47D98FE082FAD
4 changed files with 119 additions and 1 deletions

View File

@ -57,6 +57,8 @@ function sidebarToggle() {
}
if (window.innerWidth > 1000) { sidebarToggle(); }
EventUtil.bindEvent(itemSidebar, 'swipeleft', sidebarToggle)
window.onload = function () {

View File

@ -0,0 +1,115 @@
/**
* 用touch事件模拟点击左滑右滑上拉下拉等时间
* 是利用touchstart和touchend两个事件发生的位置来确定是什么操作
* 例如
* 1touchstart和touchend两个事件的位置基本一致也就是没发生位移那么可以确定用户是想点击按钮等
* 2touchend在touchstart正左侧说明用户是向左滑动的
* 利用上面的原理可以模拟移动端的各类事件
**/
const EventUtil = (function() {
//支持事件列表
let eventArr = ['eventswipeleft', 'eventswiperight', 'eventslideup', 'eventslidedown', 'eventclick', 'eventlongpress'];
//touchstart事件delta记录开始触摸位置
function touchStart(event) {
this.delta = {};
this.delta.x = event.touches[0].pageX;
this.delta.y = event.touches[0].pageY;
this.delta.time = new Date().getTime();
}
/**
* touchend事件计算两个事件之间的位移量
* 1如果位移量很小或没有位移看做点击事件
* 2如果位移量较大x大于y可以看做平移x>0,向右滑反之向左滑
* 3如果位移量较大x小于y看做上下移动y>0,向下滑反之向上滑
* 这样就模拟的移动端几个常见的时间
* */
function touchEnd(event) {
let delta = this.delta;
delete this.delta;
let timegap = new Date().getTime() - delta.time;
delta.x -= event.changedTouches[0].pageX;
delta.y -= event.changedTouches[0].pageY;
if (Math.abs(delta.x) < 5 && Math.abs(delta.y) < 5) {
if (timegap < 1000) {
if (this['eventclick']) {
this['eventclick'].map(function(fn){
fn(event);
});
}
} else {
if (this['eventlongpress']) {
this['eventlongpress'].map(function(fn){
fn(event);
});
}
}
return;
}
if (Math.abs(delta.x) > Math.abs(delta.y)) {
if (delta.x > 0) {
if (this['eventswipeleft']) {
this['eventswipeleft'].map(function(fn){
fn(event);
});
}
} else {
this['eventswiperight'].map(function(fn){
fn(event);
});
}
} else {
if (delta.y > 0) {
if (this['eventslidedown']) {
this['eventslidedown'].map(function(fn){
fn(event);
});
}
} else {
this['eventslideup'].map(function(fn){
fn(event);
});
}
}
}
function bindEvent(dom, type, callback) {
if (!dom) {
console.error('dom is null or undefined');
}
let flag = eventArr.some(key => dom[key]);
if (!flag) {
dom.addEventListener('touchstart', touchStart);
dom.addEventListener('touchend', touchEnd);
}
if (!dom['event' + type]) {
dom['event' + type] = [];
}
dom['event' + type].push(callback);
}
function removeEvent(dom, type, callback) {
if (dom['event' + type]) {
for(let i = 0; i < dom['event' + type].length; i++) {
if (dom['event' + type][i] === callback) {
dom['event' + type].splice(i, 1);
i--;
}
}
if (dom['event' + type] && dom['event' + type].length === 0) {
delete dom['event' + type];
let flag = eventArr.every(key => !dom[key]);
if (flag) {
dom.removeEventListener('touchstart', touchStart);
dom.removeEventListener('touchend', touchEnd);
}
}
}
}
return {
bindEvent,
removeEvent
}
})();

View File

@ -2,6 +2,6 @@
const APP_NAME = "ph-Bookshelf";
const VERSION = "0.3.0.3";
const VERSION = "0.3.0.6";
const CHANNEL = "suk-ws";
const BRANCH = "master";

View File

@ -46,6 +46,7 @@ class SiteMeta {
// "/assets/gitbook-fix.js",
// "https://cdn.jsdelivr.net/npm/marked/marked.min.js",
// "/assets/ref.js",
"/assets/utils-touchscreen-event.js",
"/assets/main.js",
);
}