บทความนี้อธิบายวิธีการของ JS เพื่อตรวจสอบว่าโทรศัพท์มือถือและพีซีสามารถเลือกกิจกรรมการดำเนินการที่แตกต่างกันได้หรือไม่ แบ่งปันสำหรับการอ้างอิงของคุณ รายละเอียดมีดังนี้:
ตรวจสอบว่าเป็นโทรศัพท์มือถือ:
ฟังก์ชั่น ismobile () {var suseragent = navigator.useragent.toLowercase (), bisipad = suseragent.match (/iPad/i) == "iPad", bisiphoneos = suseragent.match (/iiphone os/i) == "iphone os" bisuc7 = suseragent.match (/rv:1.2.3.4/i) == "rv: 1.2.3.4", bisuc = suseragent.match (/ucweb/i) == "ucweb", bisandroid = suseragent.match (/android/i) "Windows CE", biswm = suseragent.match (/windows mobile/i) == "windows mobile", biswebview = suseragent.match (/webview/i) == "WebView"; return (bisipad || bisiphoneos || bismidp || bisuc7 || bisuc || bisandroid || bisce || biswm);}เพื่อกำหนดเหตุการณ์ที่จะใช้:
var touchstart, touchmove, touchend; touchstart = ismobile ()? 'TouchStart': 'mousedown'; touchMove = isMobile ()? 'TouchMove': 'Mousemove'; touchend = ismobile ()? 'touchend': 'Mouseup';
การจัดการที่สอดคล้องกันของสามเหตุการณ์:
TouchStart: ฟังก์ชั่น (e) {var e = e || window.event; // เพื่อพิจารณาว่าเหตุการณ์ stopdefault (E) ใด; // เบราว์เซอร์ที่แตกต่างกันวิธีการเริ่มต้นของเหตุการณ์สำหรับการป้องกันไม่ให้เบราว์เซอร์แตกต่างกันถ้า (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; }}, touchend: ฟังก์ชั่น (e) {var e = e || window.event; stopdefault (e); if (this.y2 == 0) {return; } var diffy = this.y2-this.y1; if (diffy> 50) {this.donext (); } อื่นถ้า (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 ของทุกคน