以及安卓机里的物理返回键,解决流畅度
分类:巴黎人-前端

选拔h5新特色,轻易监听其余App自带再次回到键

2018/07/03 · HTML5 · H5

原著出处: 云叔_又拍云   

行使html5新特点轻易监听其余App自带重临键的亲自过问,html5app

1、前言

这段日子h5新特点、新标签、新专门的学问等有相当的多,并且正在不断完善中,各大浏览器商对它们的支撑,也是十分给力。作为前端技师,我感觉大家还是有要求积极关心并大胆地加以实行。接下来笔者将和各位分享二个特地好用的h5新特色(近年来亦非专程新),轻易监听别的App自带的重临键,富含安卓机里的物理重回键,进而落成项目支出中国和越南社会主义共和国来越的须要。

2、起因

粗粗6个月前收到pm一要求,用纯h5贯彻多audio的播报、暂停、续播,页面放至驾考宝典App中,与客户端从未别的的互动,所以与客商端相关的js不须要引用。看上去那供给挺轻巧的呗,就算之前也没做过类似的急需。不管三七二十一,撸起袖子正是干。开首了读书之旅。

3、笔者这边根本介绍下自个儿实际是怎么监听其余App自带的再次回到键,以及安卓机里的物理再次来到键。

那干什么小编要去监听呢,这里作者有须求强调重申再重申。苹果手提式有线电电话机无论是微信、QQ、App,依然浏览器里,涉及到audio、video,重回上一页系统会活动行车制动器踏板当前的播音的,但不是负有安卓机都足以。所以大家温馨必需自定义监听。很多有情侣可能率先主张就是百度,然后出来的答案无非是那样

pushHistory(); 
window.addEventListener("popstate", function(e) { 
    alert("我监听到了浏览器的返回按钮事件啦");//根据自己的需求实现自己的功能 
}, false); 
function pushHistory() { 
    var state = { 
        title: "title", 
        url: "#"
    }; 
    window.history.pushState(state, "title", "#"); 
}

是或不是很眼熟?但是器重供给不能够健全兑现,要这段代码有啥用,当时自笔者也是狼狈周章。直到通过大神好朋友教导,复制了这段代码

var hiddenProperty = 'hidden' in document ? 'hidden' :    
    'webkitHidden' in document ? 'webkitHidden' :    
    'mozHidden' in document ? 'mozHidden' :    
    null;
var visibilityChangeEvent = hiddenProperty.replace(/hidden/i, 'visibilitychange');
var onVisibilityChange = function(){
    if (!document[hiddenProperty]) {    
        console.log('页面非激活');
    }else{
        console.log('页面激活')
    }
}
document.addEventListener(visibilityChangeEvent, onVisibilityChange);

抱不平时消除。

这段代码的原理作者个人精通正是经过决断客商浏览的是或不是为当前页,从而举行有关操作。

那是 MDN相关链接:

实际不是说真的能够透过JS监听到App里的自带再次来到键,以致安卓的物理重返键,而是经过变化思路,急忙完结要求。希望那本性子能帮到各位。

以上就是本文的全体内容,希望对我们的学习抱有支持,也愿意大家多多协助帮客之家。

1、前言 前段时间h5新性情、新标签、新职业等有过多,何况正在不断完善中,各大浏...

2.ios下 下拉 上海滑稽剧团 会产出出界情形 浏览器自带丁香紫背景象,应用方案:

html5中 media(播放器)的api使用指南,html5使用指南

那篇文章主要介绍了html5中media(播放器)的api使用指南,需求的对象可以参照下

向来奉上示例代码,废话就相当少说了。

代码如下:

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>HTML Audio API</title>
<link rel="stylesheet" href="./style/main.css" />
<script src="./script/audio-controls.js"></script>
</head>
<body>
<header>
<h1>HTML5 Audio API</h1>
<p>HTML5 Audio API demo by <a href="<a href=";" target="_blank">LearnShare</a>.</p>
<p>
Last update @2013-04-23 20:40:00

  • add info table
    update @2013-04-22 14:54:00
  • add DOM events
    update @2013-04-22 12:47:00
  • add getCurrentSrc button
    </p>
    <p>
    View code on <a href="<a href=";" target="_blank">LearnShare.github.io</a>.
    API reference: <a href="<a href=";" target="_blank">HTMLMediaElement</a> and <a href="<a href=";" target="_blank">Audio/Video DOM References</a>
    </p>
    </header>
    <article>
    <section>
    <h2>Audio Element</h2>
    <audio id="audio" src="./media/music1.mp3" controls="controls"></audio>
    <p>Open the <strong>developer tool</strong> to view console logs.</p>
    </section>
    <section>
    <h2>Controls</h2>
    <button id="play">play</button>
    <button id="pause">pause</button>
    <button id="get_paused">getPaused</button>
    <button id="get_ended">getEnded</button>

<button id="volume_down">volume-</button>
<button id="volume_up">volume+</button>
<button id="get_volume">getVolume</button>

<button id="get_src">getSrc</button>
<button id="play_music1">playMusic1</button>
<button id="play_music2">playMusic2</button>
<button id="remove_music">removeMusic</button>
<button id="get_current_src">getCurrentSrc</button>

<button id="get_initial_time">getInitialTime</button>
<button id="get_duration">getDuration</button>
<button id="get_seeking">getSeeking</button>
<button id="jump_to">jumpTo_30s</button>
<button id="get_current_time">getCurrentTime</button>
<button id="get_played">getPlayed</button>

<button id="autoplay_on">autoplay_on</button>
<button id="autoplay_off">autoplay_off</button>
<button id="get_autoplay">getAutoplay</button>

<button id="controls_show">controls_show</button>
<button id="controls_hide">controls_hide</button>
<button id="get_controls">getControls</button>

<button id="loop_on">loop_on</button>
<button id="loop_off">loop_off</button>
<button id="get_loop">getLoop</button>

<button id="preload_metadata">preload_metadata</button>
<button id="get_preload">getPreload</button>

<button id="get_default_muted">getDefaultMuted</button>
<button id="mute">mute</button>
<button id="unmute">unmute</button>
<button id="get_muted">getMuted</button>

<button id="get_default_playback_rate">getDefaultPlaybackRate</button>
<button id="playback_rate_down">playbackRate-</button>
<button id="playback_rate_up">playbackRate+</button>
<button id="get_playback_rate">getPlaybackRate</button>

<button id="get_network_state">getNetworkState</button>
<button id="get_ready_state">getReadyState</button>
<button id="get_buffered">getBuffered</button>
<button id="get_seekable">getSeekable</button>
</section>
</article>
<aside>
<section>
<h3>Player Attrs</h3>
<table>
<tr>
<td>autoplay:</td>
<td id="autoplay"></td>
</tr>
<tr>
<td>controls:</td>
<td id="controls"></td>
</tr>
<tr>
<td>defaultMuted:</td>
<td id="default_muted"></td>
</tr>
<tr>
<td>defaultPlaybackRate:</td>
<td id="default_playback_rate"></td>
</tr>
<tr>
<td>loop:</td>
<td id="loop"></td>
</tr>
<tr>
<td>preload:</td>
<td id="preload"></td>
</tr>
</table>
</section>
<section>
<h3>Player Info</h3>
<table>
<tr>
<td>src:</td>
<td id="src"></td>
</tr>
<tr>
<td>currentSrc:</td>
<td id="current_src"></td>
</tr>
<tr>
<td>duration:</td>
<td id="duration"></td>
</tr>
<tr>
<td>currentTime:</td>
<td id="current_time"></td>
</tr>
<tr>
<td>ended:</td>
<td id="ended"></td>
</tr>
<tr>
<td>paused:</td>
<td id="paused"></td>
</tr>
<tr>
<td>muted:</td>
<td id="muted"></td>
</tr>
<tr>
<td>volume:</td>
<td id="volume"></td>
</tr>
<tr>
<td>playbackRate:</td>
<td id="playback_rate"></td>
</tr>
</table>
</section>
<section>
<h3>Play status</h3>
<table>
<tr>
<td>networkState:</td>
<td id="network_state"></td>
</tr>
<tr>
<td>readyState:</td>
<td id="ready_state"></td>
</tr>
<tr>
<td>buffered:</td>
<td id="buffered"></td>
</tr>
<tr>
<td>seekable:</td>
<td id="seekable"></td>
</tr>
<tr>
<td>played:</td>
<td id="played"></td>
</tr>
<tr>
<td>error:</td>
<td id="error"></td>
</tr>
</table>
</section>
</aside>
</body>
</html>

audio-controls.js

代码如下:

window.onload=function(){
// get autio element
var audio=document.getElementById("audio");
// play()
document.getElementById("play").onclick=function(){
audio.play();
console.log("play");
};
// pause()
document.getElementById("pause").onclick=function(){
audio.pause();
console.log("pause");
};
// get paused
document.getElementById("get_paused").onclick=function(){
console.log("audio.paused: "+audio.paused);
};
// get ended
document.getElementById("get_ended").onclick=function(){
console.log("audio.ended: "+audio.ended);
};
// set volume-
document.getElementById("volume_down").onclick=function(){
audio.volume-=0.2;
console.log("volume-0.2");
};
// set volume+
document.getElementById("volume_up").onclick=function(){
audio.volume+=0.2;
console.log("volume+0.2");
};
// get volume
document.getElementById("get_volume").onclick=function(){
console.log("audio.volume: "+audio.volume);
};
// get src
document.getElementById("get_src").onclick=function(){
console.log("audio.src: "+audio.src);
};
// set src_music1
document.getElementById("play_music1").onclick=function(){
audio.src="./media/music1.mp3";
updateSrc();
updateCurrentSrc();
console.log("play music1");
};
// set src_music2
document.getElementById("play_music2").onclick=function(){
audio.src="./media/music2.mp3";
updateSrc();
updateCurrentSrc();
console.log("play music2");
};
// set remove_music
document.getElementById("remove_music").onclick=function(){
audio.src="";
console.log("remove music");
};
// get currentSrc
document.getElementById("get_current_src").onclick=function(){
console.log("audio.currentSrc: "+audio.currentSrc);
};
// get initialTime
document.getElementById("get_initial_time").onclick=function(){
console.log("audio.initialTime: "+audio.initialTime);
};
// get duration
document.getElementById("get_duration").onclick=function(){
console.log("audio.duration: "+audio.duration);
};
// get seeking
document.getElementById("get_seeking").onclick=function(){
console.log("audio.seeking: "+audio.seeking);
};
// set currentTime
document.getElementById("jump_to").onclick=function(){
audio.currentTime=30;
console.log("jumpTo 30s");
};
// get currentTime
document.getElementById("get_current_time").onclick=function(){
console.log("audio.currentTime: "+audio.currentTime);
};
// get played
document.getElementById("get_played").onclick=function(){
console.log("audio.played:");
var ranges=audio.played;
var n=ranges.length;
for(var i=0;i<n;i++){
console.log("("+ranges.start(i)+","+ranges.end(i)+")");
}
};
// autoplay on
document.getElementById("autoplay_on").onclick=function(){
audio.autoplay=true;
updateAutoplay();
console.log("autoplay on");
};
// autoplay off
document.getElementById("autoplay_off").onclick=function(){
audio.autoplay=false;
updateAutoplay();
console.log("autoplay off");
};
// get autoplay
document.getElementById("get_autoplay").onclick=function(){
console.log("audio.autoplay: "+audio.autoplay);
};
// controls show
document.getElementById("controls_show").onclick=function(){
audio.controls=true;
updateControls();
console.log("controls show");
};
// controls hide
document.getElementById("controls_hide").onclick=function(){
audio.controls=false;
updateControls();
console.log("controls hide");
};
// get controls
document.getElementById("get_controls").onclick=function(){
console.log("audio.controls: "+audio.controls);
};
// loop on
document.getElementById("loop_on").onclick=function(){
audio.loop=true;
updateLoop();
console.log("loop on");
};
// loop off
document.getElementById("loop_off").onclick=function(){
audio.loop=false;
updateLoop();
console.log("loop off");
};
// get loop
document.getElementById("get_loop").onclick=function(){
console.log("audio.loop: "+audio.loop);
};
// preload metadata
document.getElementById("preload_metadata").onclick=function(){
audio.preload="metadata";
updatePreload();
console.log("preload metadata");
};
// get preload
document.getElementById("get_preload").onclick=function(){
console.log("audio.preload: "+audio.preload);
};
// get defaultMuted
document.getElementById("get_default_muted").onclick=function(){
console.log("audio.defaultMuted: "+audio.defaultMuted);
};
// mute
document.getElementById("mute").onclick=function(){
audio.muted=true;
updateMuted();
console.log("audio mute");
};
// unmute
document.getElementById("unmute").onclick=function(){
audio.muted=false;
updateMuted();
console.log("audio unmute");
};
// get muted
document.getElementById("get_muted").onclick=function(){
console.log("audio.muted: "+audio.muted);
};
// get defaultPlaybackRate
document.getElementById("get_default_playback_rate").onclick=function(){
console.log("audio.defaultPlaybackRate: "+audio.defaultPlaybackRate);
};
// set playbackRate-
document.getElementById("playback_rate_down").onclick=function(){
audio.playbackRate-=0.2;
console.log("playbackRate-0.2");
};
// set playbackRate+
document.getElementById("playback_rate_up").onclick=function(){
audio.playbackRate+=0.2;
console.log("playbackRate+0.2");
};
// get playbackRate
document.getElementById("get_playback_rate").onclick=function(){
console.log("audio.playbackRate: "+audio.playbackRate);
};
// get networkState
document.getElementById("get_network_state").onclick=function(){
console.log("audio.networkState: "+audio.networkState);
};
// get readyState
document.getElementById("get_ready_state").onclick=function(){
console.log("audio.readyState: "+audio.readyState);
};
// get buffered
document.getElementById("get_buffered").onclick=function(){
console.log("audio.buffered:");
var ranges=audio.buffered;
var n=ranges.length;
for(var i=0;i<n;i++){
console.log("("+ranges.start(i)+","+ranges.end(i)+")");
}
};
// get seekable
document.getElementById("get_seekable").onclick=function(){
console.log("audio.seekable:");
var ranges=audio.seekable;
var n=ranges.length;
for(var i=0;i<n;i++){
console.log("("+ranges.start(i)+","+ranges.end(i)+")");
}
};

// DOM events

// abort
audio.addEventListener("abort",function(){
console.log("event:abort");
});
// canplay
audio.addEventListener("canplay",function(){
console.log("event:canplay");
});
// canplaythrough
audio.addEventListener("canplaythrough",function(){
console.log("event:canplaythrough");
});
// durationchange
audio.addEventListener("durationchange",function(){
updateDuration();
console.log("event:durationchange");
});
// emptied
audio.addEventListener("emptied",function(){
updateSrc();
updateCurrentSrc();
updateDuration();
updatePaused();
updateNetworkState();
updateReadyState();
updateBuffered();
updateSeekable();
updatePlayed();
console.log("event:emptied");
});
// ended
audio.addEventListener("ended",function(){
updateEnded();
console.log("event:ended");
});
// loadeddata
audio.addEventListener("loadeddata",function(){
updateNetworkState();
updateReadyState();
updateBuffered();
updateSeekable();
console.log("event:loadeddata");
});
// loadedmetadata
audio.addEventListener("loadedmetadata",function(){
console.log("event:loadedmetadata");
});
// loadstart
audio.addEventListener("loadstart",function(){
console.log("event:loadstart");
});
// pause
audio.addEventListener("pause",function(){
updatePaused();
console.log("event:pause");
});
// play
audio.addEventListener("play",function(){
updatePaused();
console.log("event:play");
});
// playing
audio.addEventListener("playing",function(){
console.log("event:playing");
});
// progress
audio.addEventListener("progress",function(){
updateNetworkState();
updateReadyState();
updateBuffered();
updateSeekable();
console.log("event:progress");
});
// ratechange
audio.addEventListener("ratechange",function(){
updatePlaybackRate();
console.log("event:ratechange");
});
// seeked
audio.addEventListener("seeked",function(){
console.log("event:seeked");
});
// seeking
audio.addEventListener("seeking",function(){
console.log("event:seeking");
});
// stalled
audio.addEventListener("stalled",function(){
console.log("event:stalled");
});
// suspend
audio.addEventListener("suspend",function(){
console.log("event:suspend");
});
// timeupdate
audio.addEventListener("timeupdate",function(){
updateCurrentTime();
updateEnded();
updatePlayed();
console.log("event:timeupdate");
});
// volumechange
audio.addEventListener("volumechange",function(){
updateVolume();
console.log("event:volumechange");
});
// waiting
audio.addEventListener("waiting",function(){
console.log("event:waiting");
});
updateAutoplay();
updateControls();
updateDefaultMuted();
updateDefaultPlaybackRate();
updateLoop();
updatePreload();
updateSrc();
updateCurrentSrc();
updateDuration();
updateCurrentTime();
updateVolume();
updatePaused();
updateMuted();
updateEnded();
updatePlaybackRate();
updateNetworkState();
updateReadyState();
updateBuffered();
updateSeekable();
updatePlayed();
updateError();
};
// functions to update info table
// autoplay
function updateAutoplay(){
document.getElementById("autoplay").innerHTML=audio.autoplay;
}
// controls
function updateControls(){
document.getElementById("controls").innerHTML=audio.controls;
}
// defaultMuted
function updateDefaultMuted(){
document.getElementById("default_muted").innerHTML=audio.defaultMuted;
}
// defaultPlaybackRate
function updateDefaultPlaybackRate(){
document.getElementById("default_playback_rate").innerHTML=audio.defaultPlaybackRate;
}
// loop
function updateLoop(){
document.getElementById("loop").innerHTML=audio.loop;
}
// preload
function updatePreload(){
document.getElementById("preload").innerHTML=audio.preload;
}
// src
function updateSrc(){
document.getElementById("src").innerHTML=audio.src;
}
// currentSrc
function updateCurrentSrc(){
document.getElementById("current_src").innerHTML=audio.currentSrc;
}
// duration
function updateDuration(){
document.getElementById("duration").innerHTML=audio.duration;
}
// currentTime
function updateCurrentTime(){
document.getElementById("current_time").innerHTML=audio.currentTime;
}
// ended
function updateEnded(){
document.getElementById("ended").innerHTML=audio.ended;
}
// paused
function updatePaused(){
document.getElementById("paused").innerHTML=audio.paused;
}
// muted
function updateMuted(){
document.getElementById("muted").innerHTML=audio.muted;
}
// volume
function updateVolume(){
document.getElementById("volume").innerHTML=audio.volume;
}
// playbackRate
function updatePlaybackRate(){
document.getElementById("playback_rate").innerHTML=audio.playbackRate;
}
// networkState
function updateNetworkState(){
document.getElementById("network_state").innerHTML=audio.networkState;
}
// readyState
function updateReadyState(){
document.getElementById("ready_state").innerHTML=audio.readyState;
}
// buffered
function updateBuffered(){
var ranges=audio.buffered;
var str="";
var n=ranges.length;
for(var i=0;i<n;i++){
str+="("+ranges.start(i)+","+ranges.end(i)+")";
if(i!=n-1){
str+="
";
}
}
document.getElementById("buffered").innerHTML=str;
}
// seekable
function updateSeekable(){
var ranges=audio.seekable;
var str="";
var n=ranges.length;
for(var i=0;i<n;i++){
str+="("+ranges.start(i)+","+ranges.end(i)+")";
if(i!=n-1){
str+="
";
}
}
document.getElementById("seekable").innerHTML=str;
}
// played
function updatePlayed(){
var ranges=audio.played;
var str="";
var n=ranges.length;
for(var i=0;i<n;i++){
str+="("+ranges.start(i)+","+ranges.end(i)+")";
if(i!=n-1){
str+="
";
}
}
document.getElementById("played").innerHTML=str;
}
// error
function updateError(){
document.getElementById("error").innerHTML=audio.error;
}

1、前言

今昔h5新特点、新标签、新专门的学业等有非常的多,何况正在不断完善中,各大浏览器商对它们的支撑,也是一对一给力。作为前端技术员,笔者以为大家依然有要求积极关心并勇于地加以施行。接下来小编将和各位共享三个蛮好用的h5新本性(近些日子亦非特意新),轻易监听别的App自带的重回键,包罗安卓机里的物理重返键,进而达成项目支出中愈发的供给。

消除流畅度。

2、起因

大约三个月前接到pm一急需,用纯h5落成多audio的广播、暂停、续播,页面放至驾考宝典App中,与客户端从未其他的竞相,所以与客户端相关的js无需引用。看上去那供给挺简单的呗,就算事先也没做过类似的须求。不管三七二十一,撸起袖子正是干。伊始了上学之旅。

5.手提式有线电话机端按住不放 阻止浏览器暗中认可响应事件 调节和测量试验

4、手提式有线电话机包容性

领会今后的安卓机系统4.0等都是都市版了,该属性抢先百分之五十安卓机都能辨识,个人舒享版安卓机不能辨识,原因在于navigator.userAgent内核版本过低,chrome今后无数是64+了,所以遭逢该难点假使想办法协作它就好了。

并非说真的能够由此JS监听到客商对App里的自带重回键的直白操作,乃至安卓的物理再次来到键,而是通过变化思路,火速完结须要。希望以此脾质量帮到各位。

1 赞 1 收藏 评论

图片 1

 -webkit-overflow-scrolling: touch; 

3、小编那边关键介绍下小编具体是怎么监听别的App自带的再次回到键,以及安卓机里的物理再次回到键。

这干什么笔者要去监听呢,这里本身有须要重申重申再强调。苹果手提式有线电话机无论是微信、QQ、App,还是浏览器里,涉及到audio、video,重回上一页系统会活动行车制动器踏板当前的播音的,但不是负有安卓机都足以。所以大家和睦必得自定义监听。非常多有恋人概率先设法便是百度,然后出来的答案无非是这么

pushHistory(); window.addEventListener("popstate", function(e) { alert("笔者监听到了浏览器的归来按键事件啦");//依照本人的供给实现和煦的功能}, false); function pushHistory() { var state = { title: "title", url: "#" }; window.history.pushState(state, "title", "#"); }

1
2
3
4
5
6
7
8
9
10
11
pushHistory();
window.addEventListener("popstate", function(e) {
    alert("我监听到了浏览器的返回按钮事件啦");//根据自己的需求实现自己的功能
}, false);
function pushHistory() {
    var state = {
        title: "title",
        url: "#"
    };
    window.history.pushState(state, "title", "#");
}

是或不是很熟识?但是根本须求无法健全兑现,要这段代码有啥用,当时自己也是挖空心思。直到通过大神基友指点,复制了这段代码

var hiddenProperty = 'hidden' in document ? 'hidden' : 'webkitHidden' in document ? 'webkitHidden' : 'mozHidden' in document ? 'mozHidden' : null; var visibilityChangeEvent = hiddenProperty.replace(/hidden/i, 'visibilitychange'); var onVisibilityChange = function(){ if (document[hiddenProperty]) { console.log('页面非激活'); }else{ console.log('页面激活') } } document.addEventListener(visibilityChangeEvent, onVisibilityChange);

1
2
3
4
5
6
7
8
9
10
11
12
13
var hiddenProperty = 'hidden' in document ? 'hidden' :    
    'webkitHidden' in document ? 'webkitHidden' :    
    'mozHidden' in document ? 'mozHidden' :    
    null;
var visibilityChangeEvent = hiddenProperty.replace(/hidden/i, 'visibilitychange');
var onVisibilityChange = function(){
    if (document[hiddenProperty]) {    
        console.log('页面非激活');
    }else{
        console.log('页面激活')
    }
}
document.addEventListener(visibilityChangeEvent, onVisibilityChange);

抱有标题消除。
这段代码的规律小编个人驾驭正是通过决断客户浏览的是或不是为当前页,进而进行连锁操作。
那是 MDN相关链接:https://developer.mozilla.org…。

选取 scrollfix.js 组件 举行安装。引进后参预 所要滑动的id模块。

本文由巴黎人手机版发布于巴黎人-前端,转载请注明出处:以及安卓机里的物理返回键,解决流畅度

上一篇:书写的时候不能省略结束标签,模态框(Modal)是 下一篇:没有了
猜你喜欢
热门排行
精彩图文