最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JavaScript自定義瀏覽器滾動條兼容IE、 火狐和chrome

 更新時間:2017年01月05日 14:53:28   作者:依然仰望  
本文主要分享了使用原生JavaScript實(shí)現(xiàn)自定義瀏覽器滾動條兼容IE、 火狐和chrome的思路與方法,具有一定的參考價值,下面跟著小編一起來看下吧

今天為大家分享一下我自己制作的瀏覽器滾動條,我們知道用css來自定義滾動條也是挺好的方式,css雖然能夠改變chrome瀏覽器的滾動條樣式可以自定義,css也能夠改變IE瀏覽器滾動條的顏色。但是css只能是改變IE瀏覽器的顏色,而且CSS不能做到改變火狐瀏覽器的樣式和顏色。所以只能是通過JavaScript來實(shí)現(xiàn)了。也有插件可以做到。我分享一下我自己使用原生JavaScript實(shí)現(xiàn)的思路。先上個圖看下效果:

JavaScript實(shí)現(xiàn)的思路就是模擬瀏覽器自身滾動條。我制作的思路是先將整個文檔放在一個容器里面,然后通過改變?nèi)萜骼锩娴膁iv的top值來實(shí)現(xiàn)滾動效果布局如下:

<style>
 *{
 margin:0;
 padding:0;
 }
 body{
 overflow:hidden;
 }
 #box{
 float:right;
 top:0;
 right:0;
 width:20px;
 background:#ccc;
 position:relative;
 }
 #drag{
 position: absolute;
 top:0
 left:0;
 width:20px;
 background:green;
 }
 #content{
 position:absolute;
 left: 0;
 }
</style>
<body>
 <div id="box">
 <div id="drag"></div>
 </div>
 <div id="content">
 <div style="background:#ccc;width: 100px;">
  Although many people talk about the super performance of quantum computing, such as one second to complete the current supercomputer computing tasks for several years, but so far did not create a true sense of the quantum computer, one of the very important reason is that, The state of particles used in quantum computation is not stable, and any electromagnetic or physical interference can easily disrupt its work. The state of the Mayola fermion is very stable, which makes it a perfect choice for making quantum computers. Six months ago in the laboratory of Shanghai Jiaotong University, Jia Jinfeng successfully captured it.
  Speaking of the scene, Jia Jinfeng said: "In fact, I started to hear the Mayolana fermions, I think this thing may not be done 20 years out.
  Using a special material preparation method, Jia Jinfeng's research team has grown topological insulators on the superconductors with thickness of 5 nanometers. The topological superconductor materials are prepared and finally the Mayolana fermions are found at the interface of the topological superconductors. The mysterious particles were captured 80 years, but also let Jia Jinfeng more firm with its confidence in the manufacture of quantum computers.
  Speaking of the future of the plan, Jia Jinfeng said: "I hope to within a few years to do the topological quantum bit!" (Before) the world has not, so if we cut into this from the point, we are the same with the world The starting line, for our country, this is able to catch up with the footsteps of quantum computing, a starting point.
 <div>
 </div>
</body>

先定義滑塊和滑動條,然后在定義一個裝內(nèi)容的盒子,布局很簡單,body的 overflow設(shè)置成hidden隱藏默認(rèn)滾動條。

實(shí)現(xiàn)主要思路就是:滑塊移動距離/滑塊滾動范圍=內(nèi)容滾動距離/內(nèi)容可滾動高度;滑塊移動距離就是鼠標(biāo)按下后拖動的距離,

內(nèi)容可滾動高度就是內(nèi)容總高度減去可視區(qū)域高度。另外,滾動條的總高度就是可視區(qū)域的高度,滑塊的高度=可視區(qū)域的高度/內(nèi)容的總高度*可視區(qū)域的高度。最后就是判斷瀏覽器是否是火狐。

<script type="text/javascript">
window.onload=function(){
 var oBox=document.getElementById('box');
 var oDrag=document.getElementById('drag');
 var content=document.getElementById('content');
 var viewHeight=document.documentElement.clientHeight;
 var conHeight=content.clientHeight
 oBox.style.height=viewHeight+'px';
oDrag.style.height=viewHeight/conHeight*viewHeight+'px';
 window.onresize = function(){
 viewHeight=document.documentElement.clientHeight;
 oBox.style.height=viewHeight+'px';
oDrag.style.height=viewHeight/conHeight*viewHeight+'px';
 oDrag.style.top=-content.offsetTop/(content.clientHeight-viewHeight)*(oBox.clientHeight-oDrag.clientHeight)+'px'; 
 }
 oDrag.onmousedown=function (ev){
 //阻止默認(rèn)事件
 var e=ev||window.event;
 if (e.preventDefault) {
  e.preventDefault();
 } else{
  e.returnValue=false;
 };
  //e.clientY鼠標(biāo)當(dāng)前坐標(biāo)
 var downY=e.clientY-oDrag.offsetTop;
 document.onmousemove=function (ev){
  var e=ev||window.event;
  var top=e.clientY-downY;
  if (top<=0) {
  top=0;
  };
  if (top>=oBox.clientHeight-oDrag.clientHeight) {
  top=oBox.clientHeight-oDrag.clientHeight;
  };
  var scale=top/(oBox.clientHeight-oDrag.clientHeight);
  var contentY=scale*(content.clientHeight-viewHeight);
  oDrag.style.top=top+'px';
  content.style.top=-contentY+'px';
 }
 document.onmouseup=function (){
  document.onmousemove=null;
 }
 }
 var str=window.navigator.userAgent.toLowerCase();
 //火狐瀏覽器
 if (str.indexOf('firefox')!=-1){
  document.addEventListener('DOMMouseScroll',function (e){
  e.preventDefault();//阻止窗口默認(rèn)的滾動事件
  if (e.detail<0) {
  var scrollHei=content.offsetTop+25;
  if (scrollHei>=0) {
   scrollHei=0;
  };
  if (scrollHei<=-(content.clientHeight-viewHeight)) {
   scrollHei=-(content.clientHeight-viewHeight);
  };
  var scale=scrollHei/(content.clientHeight-viewHeight);
  var top=scale*(oBox.clientHeight-oDrag.clientHeight);
  content.style.top=scrollHei+'px';
  oDrag.style.top=-top+'px';
  }
  if (e.detail>0) {
  var scrollHei=content.offsetTop-25;
  if (scrollHei>=0) {
   scrollHei=0;
  };
  if (scrollHei<=-(content.clientHeight-viewHeight)) {
   scrollHei=-(content.clientHeight-viewHeight);
  };
  var scale=scrollHei/(content.clientHeight-viewHeight);
  var top=scale*(oBox.clientHeight-oDrag.clientHeight);
  content.style.top=scrollHei+'px';
  oDrag.style.top=-top+'px';
  };
 },false);
 }
 else{//非火狐瀏覽器
 document.onmousewheel=function (ev){
  var e=ev||window.event;
  if (e.preventDefault) {
  e.preventDefault();
  } else{
  e.returnValue=false;
  };
  if (e.wheelDelta>0) {
  var scrollHei=content.offsetTop+25;
  if (scrollHei>=0) {
   scrollHei=0;
  };
  if (scrollHei<=-(content.clientHeight-viewHeight)) {
   scrollHei=-(content.clientHeight-viewHeight);
  };
  var scale=scrollHei/(content.clientHeight-viewHeight);
  var top=scale*(oBox.clientHeight-oDrag.clientHeight);
  content.style.top=scrollHei+'px';
  oDrag.style.top=-top+'px';
  };
  if (e.wheelDelta<0) {
  var scrollHei=content.offsetTop-25;
  if (scrollHei>=0) {
   scrollHei=0;
  };
  if (scrollHei<=-(content.clientHeight-viewHeight)) {
   scrollHei=-(content.clientHeight-viewHeight);
  };
  var scale=scrollHei/(content.clientHeight-viewHeight);
  var top=scale*(oBox.clientHeight-oDrag.clientHeight);
  content.style.top=scrollHei+'px';
  oDrag.style.top=-top+'px';
  };
 }
 }
}
</script>

以上就是我自己實(shí)現(xiàn)的整個過程,其中也存在不少BUG,比如沒有解決瀏覽器縮放時候的問題。感謝大家的閱讀,如有指正的地方歡迎大家指正糾錯

以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!

相關(guān)文章

  • 下拉列表select 由左邊框移動到右邊示例

    下拉列表select 由左邊框移動到右邊示例

    select由左邊框移動到右邊,下面有個不錯的示例,大家可以參考下
    2013-12-12
  • 微信小程序?qū)崿F(xiàn)列表滾動頭部吸頂?shù)氖纠a

    微信小程序?qū)崿F(xiàn)列表滾動頭部吸頂?shù)氖纠a

    這篇文章主要介紹了微信小程序?qū)崿F(xiàn)列表滾動頭部吸頂?shù)氖纠a,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • es6和commonJs的區(qū)別解析

    es6和commonJs的區(qū)別解析

    這篇文章主要介紹了es6和commonJs的區(qū)別,ES6的模塊化規(guī)范更加先進(jìn)、靈活,能夠適應(yīng)更多的應(yīng)用場景,而CommonJS則更加簡單、易用,廣泛應(yīng)用于Node.js開發(fā)中,在實(shí)際應(yīng)用中,可以根據(jù)具體情況選擇使用不同的模塊化方案,需要的朋友可以參考下
    2023-03-03
  • JavaScript實(shí)現(xiàn)飛舞的泡泡效果

    JavaScript實(shí)現(xiàn)飛舞的泡泡效果

    這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)飛舞的泡泡效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • JavaScript中for..in循環(huán)陷阱介紹

    JavaScript中for..in循環(huán)陷阱介紹

    for...in循環(huán)中的循環(huán)計數(shù)器是字符串,而不是數(shù)字它包含當(dāng)前屬性的名稱或當(dāng)前數(shù)組元素的索引,下面有個不錯的示例大家可以參考下
    2013-11-11
  • JavaScript對IE操作的經(jīng)典代碼(推薦)

    JavaScript對IE操作的經(jīng)典代碼(推薦)

    本篇文章主要是對JavaScript對IE操作的經(jīng)典代碼進(jìn)行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助
    2014-03-03
  • 微信小程序新手入門之自定義組件的使用

    微信小程序新手入門之自定義組件的使用

    最近在用自定義組件搭建小程序,簡單記錄下步驟,所以這篇文章主要給大家介紹了關(guān)于微信小程序新手入門之自定義組件使用的相關(guān)資料,需要的朋友可以參考下
    2021-05-05
  • echarts餅圖指示器文字顏色設(shè)置不同實(shí)例詳解

    echarts餅圖指示器文字顏色設(shè)置不同實(shí)例詳解

    在默認(rèn)的餅狀圖里面,圖例legend顏色是黑色的,有時候根據(jù)ui需要,根據(jù)不同的背景色,需要將圖例文字調(diào)成白色或者其他顏色,下面這篇文章主要給大家介紹了關(guān)于echarts餅圖指示器文字顏色設(shè)置不同的相關(guān)資料,需要的朋友可以參考下
    2022-07-07
  • JavaScript中使用import 和require打包后實(shí)現(xiàn)原理分析

    JavaScript中使用import 和require打包后實(shí)現(xiàn)原理分析

    這篇文章主要介紹了JavaScript中使用import 和require打包后實(shí)現(xiàn)原理分析,需要的朋友可以參考下
    2018-03-03
  • JS和canvas實(shí)現(xiàn)俄羅斯方塊

    JS和canvas實(shí)現(xiàn)俄羅斯方塊

    本文主要介紹了JS和canvas實(shí)現(xiàn)俄羅斯方塊的實(shí)例。具有很好的參考價值。下面跟著小編一起來看下吧
    2017-03-03

最新評論

右玉县| 克什克腾旗| 肇源县| 资源县| 中山市| 勐海县| 阳江市| 满洲里市| 衡山县| 临朐县| 宁晋县| 天祝| 南宁市| 吉隆县| 丹阳市| 蒙山县| 昭苏县| 新巴尔虎右旗| 黑龙江省| 金秀| 涡阳县| 惠东县| 台北县| 锡林浩特市| 延长县| 保定市| 岗巴县| 南江县| 广水市| 荣成市| 获嘉县| 新化县| 天门市| 巴林右旗| 文水县| 静海县| 克什克腾旗| 游戏| 晋中市| 广元市| 大厂|