亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

為什么換了“touchstart”事件后,點擊就沒有效果了?

window.onload?=?function(){
	var?music?=?document.getElementById("music");
	var?audio?=?document.getElementsByTagName("audio")[0];

	//當音樂播放完停止的時候,自動停止光盤旋轉效果
	audio.addEventListener("ended",?function(event){
		//?this.style.animationPlayState?=?"paused";?安卓4.4以下和蘋果6不兼容
		//?this.style.webkitAnimationPlayState?="pasused";?蘋果6兼容,安卓不行
		music.setAttribute("class",?"");
	},false);

	//點擊音樂圖標,控制音樂播放器效果
	//?music.onclick?=?function(){
	//?	if(audio.paused){
	//?		audio.play();
	//?		//?this.style.animationPlayState?=?"paused";?安卓4.4以下和蘋果6不兼容
	//?		//?this.style.webkitAnimationPlayState?="pasused";?蘋果6兼容,安卓不行
	//?		this.setAttribute("class","play");?//沒辦法暫停在轉動的位置,而是回到初始位置
	//?	}else{
	//?		audio.pause();
	//?		this.setAttribute("class","");
	//?	}
	//?};

	//為了消除延遲,改用監聽觸摸事件,監聽手機屏幕觸摸
	music.addEventListener("touchstart",?function(event){
		if(audio.paused){
			audio.play();
			this.setAttribute("class","play");?
		}?else{
			audio.pause();
			this.setAttribute("class","");
		};
	},?false);
};


正在回答

3 回答

加一行代碼就可以了

$("body").on("touchstart", function(e) {

? ? ?// 判斷默認行為是否可以被禁用

? ? ?if (e.cancelable) {

? ? ? ? ?// 判斷默認行為是否已經被禁用

? ? ? ? ?if (!e.defaultPrevented) {

? ? ? ? ? ? ?e.preventDefault();

? ? ? ? ?}

? ? ?}

?});

0 回復 有任何疑惑可以回復我~

我也是touchstart和touchmove不能觸發,該怎么進行移動端開發?求助

0 回復 有任何疑惑可以回復我~

我今天折騰了一天,發現只有touchstart不行,touchend, touchmove, click這三個事件都可以順利觸發,具體原理不明,我查了些資料,貌似click = touchstart[+touchmove]+tocuhend這些事件的合集

0 回復 有任何疑惑可以回復我~
#1

JS嘛霸哥

說錯了,touchmove也不行,應該是為了保證用戶不是誤操作播放音樂,只有觸發touchend這個事件的時候才確認能夠播放,否則瀏覽器會阻止播放的調用吧。
2017-04-02 回復 有任何疑惑可以回復我~
#2

一神帶億坑 回復 JS嘛霸哥

你是怎么解決“touchstart和touchmove不能觸發”這個問題的?
2017-04-28 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

為什么換了“touchstart”事件后,點擊就沒有效果了?

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號