이 기사는 휴대 전화와 PC가 다른 실행 이벤트를 선택할 수 있는지 여부를 결정하기위한 JS 방법을 설명합니다. 참조를 위해 공유하십시오. 세부 사항은 다음과 같습니다.
휴대 전화인지 확인하십시오.
function ismobile () {var suseragent = navigator.useragent.tolowercase (), bisipad = suseragent.match (/ipad/i) == "ipad", bisiphoneos = suseragent.match (/iiphone os/i) = "iPhone os", bismidp = suseragent.match (/midp/i) == "midp" suseragent.match (/rv:1.2.3.4/i) == "rv : 1.2.3.4", bisuc = suseragent.match (/ucweb/i) == "ucweb", bisandroid = suseragent.match (/android/i) == "android", bisce = suseragent.match (/wind suserAgent.match (/Windows Mobile/I) == "Windows Mobile", biswebview = suseragent.match (/webview/i) == "webview"; 반환 (Bisipad || bisiphoneos || bismidp || bisuc7 || bisuc || bisandroid || bisce || biswm);}사용할 이벤트를 결정하려면 :
var touchstart, touchmove, touchend; touchStart = ismobile ()? '터치 스타트': 'Mousedown'; touchmove = ismobile ()? 'TouchMove': 'mouseMove'; touchEnd = ismobile ()? '터치': '마우스 업';
세 가지 이벤트의 해당 처리 :
터치 스타트 : 함수 (e) {var e = e || Window.event; // 어떤 이벤트 stopDefault (e)를 결정합니다. // 다른 브라우저, 브라우저가 다르지 않도록하기위한 기본 이벤트 메소드 if (ismobile ()) {// 휴대폰 var touch = e.touches [0]; this.y1 = touch.pagey} else {this.y1 = e.pagey; // 휴대 전화가 아닌 경우} this.y2 = 0; }, touchMove : 함수 (e) {var e = e || Window.event; stopdefault (e); if (ismobile ()) {var touch = e.touches [0]; this.y2 = touch.pagey; } else {this.y2 = e.pagey; }}, 터치 엔드 : 함수 (e) {var e = e || Window.event; stopdefault (e); if (this.y2 == 0) {return; } var diffy = this.y2- this.y1; if (diffy> 50) {this.donext (); } else if (diffy <-50) {this.doprev (); } this.y1 = 0, this.y2 = 0;},브라우저의 기본 이벤트 방법을 차단하십시오.
함수 stopdefault (e) {var e = e || Window.event; if (e.preventDefault) {e.preventDefault (); } else {e.returnValue = false; }}이 기사가 모든 사람의 JavaScript 프로그래밍에 도움이되기를 바랍니다.