本文主要是介绍js 判断是否是手机or电脑浏览做出其他动作,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
记得在页面调用JS:
<script type="text/javascript" src="js/publicLHW.js"></script>
把下面js 保存到 publicLHW.js
function browserRedirect() {var sUserAgent = navigator.userAgent.toLowerCase();var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";var bIsMidp = sUserAgent.match(/midp/i) == "midp";var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";var bIsAndroid = sUserAgent.match(/android/i) == "android";var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";if (bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) {console.log("Mob");return true;} else {console.log("PC");return false;}
};
调用方法——栗子1:
$(function() {if (browserRedirect() == false) {$(window).scroll(function() {if ($(window).scrollTop() > $("#banner").height()) {$(".header").css("background", "rgba(0,0,0,0.6)");} else if ($(window).scrollTop() > $(".nbanner").height() * 0.75) {$(".header").css("background", "rgba(0,0,0,0.6)");} else {$(".header").css("background", "rgba(0,0,0,0)");}});}})
栗子2:
if(browserRedirect() == false){// PC 导航按钮$(".nav_btn").click(function(){$(this).toggleClass("nav_btn_close");// $("#nav").slideToggle();});}else{//手机端导航按钮$(".nav_btn").click(function(){$(this).toggleClass("nav_btn_close");$("#nav").slideToggle();});};
这篇关于js 判断是否是手机or电脑浏览做出其他动作的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!