首頁  >  文章  >  web前端  >  js判斷瀏覽器和hack滾動條的實例詳解

js判斷瀏覽器和hack滾動條的實例詳解

零下一度
零下一度原創
2017-07-24 14:48:331345瀏覽

這篇文章主要介紹了JavaScript判斷瀏覽器和hack滾動條的寫法,需要的朋友可以參考下

今天無聊,幫一個網友解決一個很無聊的問題,用JS判斷頁面是否出現捲軸,在網路上看了一些程式碼,經過驗證並不起作用,以下是在網路上搜尋的程式碼:

當視覺區域小於頁面的實際高度時,判定為出現捲軸

判斷各大瀏覽器核心:


var userAgent = navigator.userAgent;                              //取得浏览器的userAgent字符串 
  var isOpera = userAgent.indexOf("Opera") > -1;                         //判断是否Opera浏览器 
  var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera; //判断是否IE浏览器 
  var isEdge = userAgent.indexOf("Edge") > -1 && !isIE;                     //判断是否IE的Edge
  var?isFF?=?userAgent.indexOf("Firefox")>-1                           //判断是否Firefox浏览器??
  var?isSafari?=?userAgent.indexOf("Safari")>-1&&?userAgent.indexOf("Chrome")==-1;        //判断是否Safari浏览器??
  var?isChrome?=?userAgent.indexOf("Chrome")>-1&&?userAgent.indexOf("Edge")==-1;         //判断Chrome浏览器??

處理各個瀏覽器捲軸,以下是我隱藏公司專案中滾動條的寫法,僅供參考:


if(isFF){
    console.log('火狐')
    $('#parent').width((windowWidth -320)*(1.01749));
  }else if(isChrome){ 
    console.log('谷歌')
    $('#parent').width((windowWidth -320)*(1.01749));
  }else if(isIE){ 
    console.log('ie10-ie5')
    $('#parent').width((windowWidth -320)*(1.01720));
  }else if(isEdge){
    console.log('edge')
    $('#parent').width((windowWidth -320)*(1.02224));
  }else{
    console.log('ie11和其他浏览器')
    $('#parent').width((windowWidth -320)*(1.01816));
  }

以上是js判斷瀏覽器和hack滾動條的實例詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn